0% found this document useful (0 votes)
26 views26 pages

OS UNIT 3 Threads

The document provides an overview of threads in operating systems, highlighting their characteristics, advantages, and challenges compared to processes. It discusses single-threaded versus multi-threaded processes, the benefits of multi-threading such as improved CPU utilization and responsiveness, and the different threading models (many-to-one, one-to-one, many-to-many). Additionally, it includes examples of thread programming in Linux and references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views26 pages

OS UNIT 3 Threads

The document provides an overview of threads in operating systems, highlighting their characteristics, advantages, and challenges compared to processes. It discusses single-threaded versus multi-threaded processes, the benefits of multi-threading such as improved CPU utilization and responsiveness, and the different threading models (many-to-one, one-to-one, many-to-many). Additionally, it includes examples of thread programming in Linux and references for further reading.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit 3

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)

C) Threads are more secure and isolated than processes.


Explanation:
Threads are not more secure and isolated than processes because threads share the same address
space and resources of their parent process, which makes them less isolated. Processes, on the other
hand, run in separate memory spaces, offering better isolation and security.
Single
Threaded
Process V/S
Multithreaded
Process
Which of the following is a primary advantage of multi-
threading over single-threading in an operating system?
A) Increased CPU idle time
B) Simplified debugging process
C) Improved CPU utilization through concurrency
D) More complex process management
Ans. C
C) Improved CPU utilization through concurrency
Explanation: Multi-threading allows multiple threads to run concurrently within a single process,
improving CPU utilization by enabling tasks to be executed in parallel, especially when there are
multiple cores available. This leads to better overall system performance compared to single-
threaded processes that can only execute one task at a time.
 Responsiveness – multi-threading increase the responsiveness of the
process. For example, in MSWord while one thread does the spelling check
the other thread allows you to keep tying the input. Therefore, you feel that
Adv. Word is always responding.
 Resource sharing – All the threads share the code and data of the process.

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?

 a) To dispatch interrupt requests to threads


 b) To provide better performance by reducing kernel involvement
 c) To automatically handle concurrency at the hardware level
Ans. B
 b) To provide better performance by reducing kernel
involvement
Example of
Thread Program
 The first parameter is the buffer which will contain the ID of the new thread, if
 Exampl pthread_create is successful.

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);
}
}

 void *thread_function(void *arg) {


// the work to be done by the thread is defined in this function
printf("Inside Thread\n");
for(i=0;i<5;i++)
{
printf("%d\n",i);
sleep(1);
}
}
 // How to  program is named “Thread.c”, then to compile write
Save,Compile and run $gcc Thread.c -lpthread
Program To run the command remains same
$./a.out
 0
 1
 2

//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

You might also like