OS UNIT 3 Threads
OS UNIT 3 Threads
Thread
s
By:
Amandeep Kaur
A thread is the smallest unit of execution which has its own thread ID,
program counter, register set and stack.
Thread
All the threads that belong to the same process share the code, data
section and other resources like open files belonging to the process.
So, remember thread is a part of the process.
A thread can not exist without a process.
Which of the following is NOT an advantage of using
threads over processes in an operating system?
A) Threads share the same address space, making context switching faster.
B) Threads can communicate more easily with each other than processes.
C) Threads are more secure and isolated than processes.
D) Creating and managing threads is typically more efficient than creating and managing processes.
Ans. C)
Of
Therefore, this allows several threads to exist within the same address space
Economy – For the same reason as mentioned above it is convenient to
Thread
create threads. Since they share resources they are less costly
Scalability – Having a multiprocessor system greatly increases the benefits
s
of multithreading. As a result, each thread can run in a separate processor in
parallel.
Dividing activities – It involves finding the functions within the job
Challenges that can be run in parallel on separate processors.
Balance – The task assigned to each processor must also be equal.
for Now there can be different parameters for that. One parameter can
be, assign equal tasks to each processor. But tasks assigned to more
Programme processor may require higher execution time thus overloading one
processor. Thus, simply assigning equal tasks to each processor may
not work.
rs while Data splitting – Another challenge is to split the data required for
each task.
creating Data dependency – sometimes the data required by one thread (T1)
might be produced by another (T2). Thus, T1 can not run before T2.
Threads
Therefore, it becomes difficult for programmers to code.
Testing and debugging – Multiple threads running in parallel on
multiple cores poses another challenge in the testing of applications.
Multi- There are two kinds of threads in the system – user
threads and kernel threads.
threading User threads are supported above the kernel and managed without
Models kernel support.
Kernel threads are supported and managed directly by OS.
Three common ways of establishing such a relationship.
Many to Many
Models(3 Many to one
Types)
One to One
Many to One Model
Many user-level threads mapped to a single kernel thread
Thread library is in user space
Drawback: The Entire process will block if a thread makes a blocking
system call
Examples:
Solaris Green Threads
GNU Portable Threads
One to One
Model
Each user-level thread maps to kernel thread
Drawback: creating a user thread requires creating a corresponding kernel
thread. Since the user can develop a malicious application to create unlimited
threads. Therefore, the system too have to create those many kernel-level
threads. As a result, the system might slow down.
Examples:
Windows NT/XP/2000
Linux Solaris 9 and later
Many to Many Model
Allows many user-level threads to be mapped to many kernel threads
Allows the operating system to create a sufficient number of kernel threads. The
operating system creates a pool of kernel-level threads called the thread-pool. As
long as a kernel-level thread is available in the thread-pool it is allocated to a user-
level thread.
Example: Solaris prior to version 9
Scheduler Activations is a technique used in operating systems to efficiently
Scheduler
manage kernel-level and user-level threading. It is designed to bridge the gap
Activation between kernel threads and user threads, allowing user-level thread
schedulers to interact effectively with the kernel while maintaining the
flexibility of user-level threading.
What is the role of scheduler activations in thread
management?
e of The second parameter specifies the attributes of the thread. This parameter is
generally NULL until you want to change the default settings.
Thread The third parameter is the name the function which the thread will execute.
Progra Hence, everything that you want the thread to do should be defined in this
function.
m
Lastly, the fourth parameter is the input to the function in the third
parameter. If the function in the third parameter does not take any input then the
fourth parameter is NULL.
//Program to create threads in linux. Thread prints 0-4 while the
main process prints 20-24
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
void *thread_function(void *arg);
int i,j;
int main() {
pthread_t a_thread; //thread declaration
pthread_create(&a_thread, NULL, thread_function, NULL);
//thread is created
pthread_join(a_thread, NULL); //process waits for thread to finish . //Comment this line to see the difference
printf("Inside Main Program\n");
for(j=20;j<25;j++)
{
printf("%d\n",j);
sleep(1);
}
}
//Output
3
4
Inside Main Program
20
21
22
23
24
pthread_create() creates a new thread which starts to execute thread_function.
How This function creates a loop which prints 0-4. The sleep function makes the
thread go to sleep after each digit is printed.
it pthread_join() makes the main function wait until the newly created thread
finishes its execution. So the control returns to the main function only when the
work thread finishes. Then the main function prints “Inside Main program” and
executes the loop from 20-24.
s?
How a thread returns a pthread_exit() is used to return a value from the thread back to
the main process. The program below shows this. The program
value to the main also shows how to pass value to a thread from the main
process? process.
References
OPERATING SYSTEM CONCEPTS by ABRAHAM SILBERSCHATZ, PETER B. GALVIN, GERG GAGNE,
WILEY
DESIGN OF THE UNIX OPERATING SYSTEM by MAURICE J. BACH, Pearson Education India
REAL-TIME SYSTEMS by JANE W. S. LIU, Pearson Education India
Program to create Threads in Linux - Dextutor Programs
Threads in Operating System - Dextutor Operating System
Thank You