SlideShare a Scribd company logo
1 of 18
Multi threaded programming
-Presented by
Pranav anyapu
318126510006
2/4 CSE-A
Thread:
 A thread is a flow of execution through the process code, with its
own program counter that keeps track of which instruction to
execute next, system registers which hold its current working
variables, and a stack which contains the execution history.
 A thread is also called a lightweight process. Threads provide a
way to improve application performance through parallelism.
Threads represent a software approach to improving performance
of operating system by reducing the execution time of the
process.
 Each thread belongs to exactly one process and no thread can
exist outside a process. Each thread represents a separate flow of
control.
 Multithreaded processes allow the execution of multiple parts
of a program at the same time.
 Single threaded processes contain the execution of
instructions in a single sequence.
 The following figure shows the working of a single-threaded
and a multithreaded process.
Single process P with single thread single process P with multiple threads
Example program for multithreading (JAVA Threads)
class MyThread extends Thread{
public void run() //* run() belongs to class Thread. here we are just over riding
the run() function.
{
for(int i=0;i<10;i++)
System.out.println("childthread");
}}
class A{
public static void main(String args[]){
MyThread t=new MyThread();
t.start(); //* If we use t.run() here no extra thread will be created.
for(int i=0;i<10;i++)
System.out.println("MainThread");
}}
ADVANTAGES OF THREADS
 Threads minimize the context switching time.
 Use of threads provides concurrency within a process.
 Efficient communication.
 It is more economical to create and context switch threads.
 Threads allow utilization of multiprocessor architectures to a
greater scale and efficiency
TYPES OF THREADS
 User Level Threads (User managed threads)
 Kernel Level Threads (Operating System managed threads
acting on kernel, an operating system core)
User Level Thread
 In this case, the thread management kernel is not aware of
the existence of threads.
 The thread library contains code for creating and destroying
threads, for passing message and data between threads, for
scheduling thread execution and for saving and restoring
thread contexts.
 The application starts with a single thread.
Advantages
 Thread switching does not require Kernel mode privileges.
 User level thread can run on any operating system.
 Scheduling can be application specific in the user level
thread.
 User level threads are fast to create and manage.
Disadvantages
 In a typical operating system, most system calls are blocking.
 Multithreaded application cannot take advantage of
multiprocessing.
Kernel Level Thread
 In this case, thread management is done by the Kernel.
 There is no thread management code in the application area.
 Kernel threads are supported directly by the operating
system.
 Any application can be programmed to be multithreaded. All
of the threads within an application are supported within a
single process.
 The Kernel maintains context information for the process as
a whole and for individuals threads within the process.
 Scheduling by the Kernel is done on a thread basis. The
Kernel performs thread creation, scheduling and
management in Kernel space.
 Kernel threads are generally slower to create and manage
than the user threads.
Advantages
 Kernel can simultaneously schedule multiple threads from
the same process on multiple processes.
 If one thread in a process is blocked, the Kernel can
schedule another thread of the same process.
 Kernel routines themselves can be multithreaded.
Disadvantages
 Kernel threads are generally slower to create and manage
than the user threads.
 Transfer of control from one thread to another within the
same process requires a mode switch to the Kernel.
Difference between User-Level & Kernel-
Level Thread
s.no
User-Level Threads Kernel-Level Thread
1 User-level threads are faster to create
and manage.
Kernel-level threads are slower to
create and manage.
2 Implementation is by a thread library at
the user level.
Operating system supports creation of
Kernel threads.
3 User-level thread is generic and can run
on any operating system.
Kernel-level thread is specific to the
operating system.
4 Multi-threaded applications cannot take
advantage of multiprocessing.
Kernel routines themselves can be
multithreaded.
Multi-Threaded program
 A multi-threaded program contains two or more parts that can
run concurrently and each part can handle a different task at
the same time making optimal use of the available resources
specially when the computer has multiple CPU’s.
Multithreading Models
 Many to many relationship.
 Many to one relationship.
 One to one relationship.
Many to Many model
 The many-to-many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads.
 The following diagram shows the many-to-many threading model
where 6 user level threads are multiplexing with 6 kernel level
threads. In this model, developers can create as many user threads
as necessary and the corresponding Kernel threads can run in
parallel on a multiprocessor machine. This model provides the best
accuracy on concurrency and when a thread performs a blocking
system call, the kernel can schedule another thread for execution.
Many to one model
 Many-to-one model maps many user level threads to one
Kernel-level thread. Thread management is done in user space
by the thread library. When thread makes a blocking system
call, the entire process will be blocked. Only one thread can
access the Kernel at a time, so multiple threads are unable to
run in parallel on multiprocessors.
 If the user-level thread libraries are implemented in the
operating system in such a way that the system does not
support them, then the Kernel threads use the many-to-one
relationship modes.
One to one model
 There is one-to-one relationship of user-level thread to the
kernel-level thread. This model provides more concurrency
than the many-to-one model. It also allows another thread to
run when a thread makes a blocking system call. It supports
multiple threads to execute in parallel on microprocessors.
Thread Libraries
 Thread libraries provide programmers with Application
programming interface (API) for creation and management of
threads.
 Thread libraries may be implemented either in user space or
in kernel space. The user space involves Application
programming interface (API) functions implemented solely
within the user space, with no kernel support. The kernel
space involves system calls, and requires a kernel with
thread library support.
Most commonly used Thread Libraries are
 POSIX Pitheads, may be provided as either a user or kernel
library, as an extension to the POSIX standard.
 Win32 threads, are provided as a kernel-level library on
Windows systems.
 Java threads: Since Java generally runs on a Java Virtual
Machine, the implementation of threads is based upon
whatever OS and hardware the JVM is running on, i.e. either
Pitheads or Win32 threads depending on the system.
Multithreading Issues
Thread Cancellation
 Thread cancellation means terminating a thread before it has
finished working. There can be two approaches for this, one
is Asynchronous cancellation, which terminates the target
thread immediately. The other is Deferred cancellation allows
the target thread to periodically check if it should be cancelled.
Signal Handling
 Signals are used in UNIX systems to notify a process that a
particular event has occurred. Now in when a Multithreaded
process receives a signal, to which thread it must be delivered?
It can be delivered to all, or a single thread.
fork() System Call
 fork() is a system call executed in the kernel through which a
process creates a copy of itself. Now the problem in
Multithreaded process is, if one thread forks, will the entire
process be copied or not?
Security Issues
 Yes, there can be security issues because of extensive sharing
of resources between multiple threads.
References:
 Tutourialspoint.com
 Geeksforgeeks.com
 Howtogeek.com
 Wikipedia.org
THANKYOU

More Related Content

What's hot

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-GalvinSonali Chauhan
 
Multi Processors And Multi Computers
 Multi Processors And Multi Computers Multi Processors And Multi Computers
Multi Processors And Multi ComputersNemwos
 
Multithreading
Multithreading Multithreading
Multithreading WafaQKhan
 
Memory management
Memory managementMemory management
Memory managementcpjcollege
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
Paging and Segmentation in Operating System
Paging and Segmentation in Operating SystemPaging and Segmentation in Operating System
Paging and Segmentation in Operating SystemRaj Mohan
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecturePankaj Kumar Jain
 
distributed shared memory
 distributed shared memory distributed shared memory
distributed shared memoryAshish Kumar
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memorysgpraju
 
Memory consistency models
Memory consistency modelsMemory consistency models
Memory consistency modelspalani kumar
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 
Multithreading
MultithreadingMultithreading
MultithreadingA B Shinde
 
System interconnect architecture
System interconnect architectureSystem interconnect architecture
System interconnect architectureGagan Kumar
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OSMsAnita2
 
Os structure
Os structureOs structure
Os structureMohd Arif
 

What's hot (20)

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Multi Processors And Multi Computers
 Multi Processors And Multi Computers Multi Processors And Multi Computers
Multi Processors And Multi Computers
 
Multithreading
Multithreading Multithreading
Multithreading
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Memory management
Memory managementMemory management
Memory management
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Paging and Segmentation in Operating System
Paging and Segmentation in Operating SystemPaging and Segmentation in Operating System
Paging and Segmentation in Operating System
 
Parallelism
ParallelismParallelism
Parallelism
 
program flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architectureprogram flow mechanisms, advanced computer architecture
program flow mechanisms, advanced computer architecture
 
Parallel programming model
Parallel programming modelParallel programming model
Parallel programming model
 
distributed shared memory
 distributed shared memory distributed shared memory
distributed shared memory
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Memory consistency models
Memory consistency modelsMemory consistency models
Memory consistency models
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Parallel processing
Parallel processingParallel processing
Parallel processing
 
Multithreading
MultithreadingMultithreading
Multithreading
 
System interconnect architecture
System interconnect architectureSystem interconnect architecture
System interconnect architecture
 
Deadlock Avoidance - OS
Deadlock Avoidance - OSDeadlock Avoidance - OS
Deadlock Avoidance - OS
 
Os structure
Os structureOs structure
Os structure
 
6.distributed shared memory
6.distributed shared memory6.distributed shared memory
6.distributed shared memory
 

Similar to Multi threaded programming

Similar to Multi threaded programming (20)

Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Threads ppt
Threads pptThreads ppt
Threads ppt
 
dos slide share.pptx
dos slide share.pptxdos slide share.pptx
dos slide share.pptx
 
Concept of thread, multi thread, tcb
Concept of thread, multi thread, tcbConcept of thread, multi thread, tcb
Concept of thread, multi thread, tcb
 
Networking threads
Networking threadsNetworking threads
Networking threads
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Threads
ThreadsThreads
Threads
 
Threads (operating System)
Threads (operating System)Threads (operating System)
Threads (operating System)
 
thread os.pptx
thread os.pptxthread os.pptx
thread os.pptx
 
Thread (Operating System)
Thread  (Operating System)Thread  (Operating System)
Thread (Operating System)
 
Treads
TreadsTreads
Treads
 
Sucet os module_2_notes
Sucet os module_2_notesSucet os module_2_notes
Sucet os module_2_notes
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptx
 
Lecture 3 threads
Lecture 3   threadsLecture 3   threads
Lecture 3 threads
 
4.Process.ppt
4.Process.ppt4.Process.ppt
4.Process.ppt
 
Epc 3.ppt
Epc 3.pptEpc 3.ppt
Epc 3.ppt
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 

Recently uploaded

TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsssuserddc89b
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxFarihaAbdulRasheed
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaPraksha3
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxpriyankatabhane
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxdharshini369nike
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxEran Akiva Sinbar
 
Welcome to GFDL for Take Your Child To Work Day
Welcome to GFDL for Take Your Child To Work DayWelcome to GFDL for Take Your Child To Work Day
Welcome to GFDL for Take Your Child To Work DayZachary Labe
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Patrick Diehl
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 

Recently uploaded (20)

TOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physicsTOPIC 8 Temperature and Heat.pdf physics
TOPIC 8 Temperature and Heat.pdf physics
 
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptxRESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
RESPIRATORY ADAPTATIONS TO HYPOXIA IN HUMNAS.pptx
 
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tantaDashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
Dashanga agada a formulation of Agada tantra dealt in 3 Rd year bams agada tanta
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
 
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Hauz Khas Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Speech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptxSpeech, hearing, noise, intelligibility.pptx
Speech, hearing, noise, intelligibility.pptx
 
TOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptxTOTAL CHOLESTEROL (lipid profile test).pptx
TOTAL CHOLESTEROL (lipid profile test).pptx
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
 
Welcome to GFDL for Take Your Child To Work Day
Welcome to GFDL for Take Your Child To Work DayWelcome to GFDL for Take Your Child To Work Day
Welcome to GFDL for Take Your Child To Work Day
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort ServiceHot Sexy call girls in  Moti Nagar,🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Moti Nagar,🔝 9953056974 🔝 escort Service
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 
Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?Is RISC-V ready for HPC workload? Maybe?
Is RISC-V ready for HPC workload? Maybe?
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 

Multi threaded programming

  • 1. Multi threaded programming -Presented by Pranav anyapu 318126510006 2/4 CSE-A
  • 2. Thread:  A thread is a flow of execution through the process code, with its own program counter that keeps track of which instruction to execute next, system registers which hold its current working variables, and a stack which contains the execution history.  A thread is also called a lightweight process. Threads provide a way to improve application performance through parallelism. Threads represent a software approach to improving performance of operating system by reducing the execution time of the process.  Each thread belongs to exactly one process and no thread can exist outside a process. Each thread represents a separate flow of control.
  • 3.  Multithreaded processes allow the execution of multiple parts of a program at the same time.  Single threaded processes contain the execution of instructions in a single sequence.  The following figure shows the working of a single-threaded and a multithreaded process. Single process P with single thread single process P with multiple threads
  • 4. Example program for multithreading (JAVA Threads) class MyThread extends Thread{ public void run() //* run() belongs to class Thread. here we are just over riding the run() function. { for(int i=0;i<10;i++) System.out.println("childthread"); }} class A{ public static void main(String args[]){ MyThread t=new MyThread(); t.start(); //* If we use t.run() here no extra thread will be created. for(int i=0;i<10;i++) System.out.println("MainThread"); }}
  • 5. ADVANTAGES OF THREADS  Threads minimize the context switching time.  Use of threads provides concurrency within a process.  Efficient communication.  It is more economical to create and context switch threads.  Threads allow utilization of multiprocessor architectures to a greater scale and efficiency TYPES OF THREADS  User Level Threads (User managed threads)  Kernel Level Threads (Operating System managed threads acting on kernel, an operating system core)
  • 6. User Level Thread  In this case, the thread management kernel is not aware of the existence of threads.  The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts.  The application starts with a single thread.
  • 7. Advantages  Thread switching does not require Kernel mode privileges.  User level thread can run on any operating system.  Scheduling can be application specific in the user level thread.  User level threads are fast to create and manage. Disadvantages  In a typical operating system, most system calls are blocking.  Multithreaded application cannot take advantage of multiprocessing.
  • 8. Kernel Level Thread  In this case, thread management is done by the Kernel.  There is no thread management code in the application area.  Kernel threads are supported directly by the operating system.  Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.  The Kernel maintains context information for the process as a whole and for individuals threads within the process.  Scheduling by the Kernel is done on a thread basis. The Kernel performs thread creation, scheduling and management in Kernel space.  Kernel threads are generally slower to create and manage than the user threads.
  • 9. Advantages  Kernel can simultaneously schedule multiple threads from the same process on multiple processes.  If one thread in a process is blocked, the Kernel can schedule another thread of the same process.  Kernel routines themselves can be multithreaded. Disadvantages  Kernel threads are generally slower to create and manage than the user threads.  Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.
  • 10. Difference between User-Level & Kernel- Level Thread s.no User-Level Threads Kernel-Level Thread 1 User-level threads are faster to create and manage. Kernel-level threads are slower to create and manage. 2 Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads. 3 User-level thread is generic and can run on any operating system. Kernel-level thread is specific to the operating system. 4 Multi-threaded applications cannot take advantage of multiprocessing. Kernel routines themselves can be multithreaded.
  • 11. Multi-Threaded program  A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when the computer has multiple CPU’s. Multithreading Models  Many to many relationship.  Many to one relationship.  One to one relationship.
  • 12. Many to Many model  The many-to-many model multiplexes any number of user threads onto an equal or smaller number of kernel threads.  The following diagram shows the many-to-many threading model where 6 user level threads are multiplexing with 6 kernel level threads. In this model, developers can create as many user threads as necessary and the corresponding Kernel threads can run in parallel on a multiprocessor machine. This model provides the best accuracy on concurrency and when a thread performs a blocking system call, the kernel can schedule another thread for execution.
  • 13. Many to one model  Many-to-one model maps many user level threads to one Kernel-level thread. Thread management is done in user space by the thread library. When thread makes a blocking system call, the entire process will be blocked. Only one thread can access the Kernel at a time, so multiple threads are unable to run in parallel on multiprocessors.  If the user-level thread libraries are implemented in the operating system in such a way that the system does not support them, then the Kernel threads use the many-to-one relationship modes.
  • 14. One to one model  There is one-to-one relationship of user-level thread to the kernel-level thread. This model provides more concurrency than the many-to-one model. It also allows another thread to run when a thread makes a blocking system call. It supports multiple threads to execute in parallel on microprocessors.
  • 15. Thread Libraries  Thread libraries provide programmers with Application programming interface (API) for creation and management of threads.  Thread libraries may be implemented either in user space or in kernel space. The user space involves Application programming interface (API) functions implemented solely within the user space, with no kernel support. The kernel space involves system calls, and requires a kernel with thread library support. Most commonly used Thread Libraries are  POSIX Pitheads, may be provided as either a user or kernel library, as an extension to the POSIX standard.  Win32 threads, are provided as a kernel-level library on Windows systems.  Java threads: Since Java generally runs on a Java Virtual Machine, the implementation of threads is based upon whatever OS and hardware the JVM is running on, i.e. either Pitheads or Win32 threads depending on the system.
  • 16. Multithreading Issues Thread Cancellation  Thread cancellation means terminating a thread before it has finished working. There can be two approaches for this, one is Asynchronous cancellation, which terminates the target thread immediately. The other is Deferred cancellation allows the target thread to periodically check if it should be cancelled. Signal Handling  Signals are used in UNIX systems to notify a process that a particular event has occurred. Now in when a Multithreaded process receives a signal, to which thread it must be delivered? It can be delivered to all, or a single thread. fork() System Call  fork() is a system call executed in the kernel through which a process creates a copy of itself. Now the problem in Multithreaded process is, if one thread forks, will the entire process be copied or not? Security Issues  Yes, there can be security issues because of extensive sharing of resources between multiple threads.