SlideShare a Scribd company logo
1 of 25
MULTITHREADING
1
What Is a Thread?
• A thread is a sequence or stream of executable
code within a process that is scheduled for
execution by the operating system on a processor
or core.
• All processes have a primary thread.
• The primary thread is a process ’ s flow of control
or thread of execution.
• A process with multiple threads has as many
flows of controls as there are threads. Each
thread executes independently and concurrently
with its own sequence of instructions.
user - level threads and kernel - level
threads.
• A process with multiple threads is multithreaded.
• There are user - level threads and kernel - level
threads.
• Kernel - level threads are a lighter burden to
create, maintain, and manage on the operating
system as compared to a process because very
little information is associated with a thread.
A kernel thread is called a lightweight process
because it has less overhead than a process.
User - and Kernel - Level Threads
• There are three implementation models for
threads:
• User - or application - level threads
• Kernel - level threads
• Hybrid of user - and kernel - level threads
user - level threads
kernel - level threads
Hybrid threads
user or kernel space or mode.
• These threads run in user or kernel space or
mode.
• In user mode , a process or thread is executing
instructions in the program or linked library.
• They are not making any calls to the operating
system kernel.
• In kernel mode , the process or thread is making
system calls such as accessing resources or
throwing exceptions. Also, in kernel mode, the
process or thread can access objects that are
defined in kernel space.
User - level threads
• User - level threads reside in user space or mode.
• The runtime library, also in user space, manages these threads.
• They are not visible to the operating system and, therefore,
cannot be scheduled to a processor core.
• Each thread does not have its own thread context.
• there is only one thread per process that will be running at any
given time and only a single processor core allocated to that
process.
• The runtime library schedules and dispatches these threads.
• User - level threads are considered a “ many - to - one ” thread
mapping.
Kernel - level threads
• Kernel - level threads reside in kernel space and are kernel
objects.
• With kernel threads, each user thread is mapped to or
bound to a kernel thread.
• The user thread is bound to that kernel thread for the life of
the user thread. Once the user thread terminates, both
threads leave the system. This is called a “ one - to - one ”
thread mapping
• The operating system scheduler manages, schedules, and
dispatches these threads. The runtime library requests a
kernel - level thread for each of the user – level threads.
• The operating system creates a context for each thread.
A hybrid thread
• A hybrid thread implementation is a cross between user
and kernel threads and allows both the library and the
operating system to manage the threads.
• The operating system selects a user thread and maps it to
one of the available kernel threads in the pool.
• More than one user thread may be assigned to the same
kernel thread.
Thread Context
• When one process is removed from a core and another
process becomes active, a context switch takes place between
those processes.
• The operating system must keep track of all the information
that is needed to restart that process and start the new
process in order for it to become active.
• This information is called the context and describes the present
state of the process. When the process becomes active, it can
continue execution right where it was preempted.
The information or context of the process includes:
• Process id
• Pointer to executable
• The stack
• Memory for static and dynamically allocated variables
• Processor registers
Hardware Threads and Software
Threads
• Chip manufacturers implement cores that have multiple hardware
threads that serve as logical cores
• Cores with multiple hardware threads are called simultaneous
multithreaded (SMT) cores.
• SMT brings to hardware the concept of multithreading
• SMT - enabled processors execute many software threads or
processes simultaneously within the processor cores
Key Similarities and Differences
between
Threads and Processes
Threads & Processes
Both have id, set of registers, state, priority, and scheduling
policy.
Both have attributes that describe the entity to the OS.
Both have an information block.
Both share resources with the parent process
Both function as independent entities from the parent
process.
The creator can exercise some control over the thread or
process.
Both can change their attributes.
Both can create new resources.
Neither can access the resources of another process.
Threads Processes
Threads share the address
space of the process that
created it
processes have their own
address.
Threads have direct access to
the data segment
of their process
processes have their own copy
of
the data segment of the
parent process.
Threads can directly
communicate with
other threads of their process;
processes must use
Interprocess Communication
to communicate
with sibling processes.
Threads have almost no
overhead
processes
have considerable overhead.
Threads Processes
New threads are easily
created
new processes
require duplication of the
parent process.
Threads can exercise
considerable control over
threads of the same process
processes can
exercise control only over
child processes.
Changes to the main thread
(cancellation, priority
change, and so on) may affect
the behavior of the
other threads of the process
changes to the
parent process do not affect
child processes.
Setting Thread Attributes
The POSIX thread library defines a thread attribute object that
encapsulates a subset of the properties of the thread.
These attributes are accessible and modifiable by the creator of
the thread.
These are the thread attributes that are modifiable:
• Contention scope
• Stack size
• Stack address
• Detached state
• Priority
• Scheduling policy and parameters
The scope attribute describes which threads a
particular thread competes with for resources.
Threads contend for resources within two
contention scopes:
1. Process scope
2. System scope
• Threads with process scope compete with threads
within the same process
• threads with system wide contention scope compete
for resources with threads of other processes allocated
across the system.
• A thread that has system scope is prioritized and
scheduled with respect to all of the system wide
threads.
• The scheduling policy also determines when a thread is
assigned the processor. FIFO, round robin (RR), and
other scheduling policies are available.
The Architecture of a Thread
Thread States
• The thread is the unit of execution when a
process is scheduled to be executed.
• If the process has only one thread, it is the
primary thread that is assigned to a processor
core.
• If a process has multiple threads and there
are multiple processors available to the
process, all of the threads are assigned to
processors.
Thread States
• When a thread is scheduled to execute on a processor core, it
changes its state.
• A thread state is the mode or condition that a thread is in at
any given time.
• Threads have the same states and transitions as for processes.
• There are four commonly implemented states:
 Runnable
 Running (active)
 Stopped
 Sleeping (blocked)
• There are several transitions:
 Preempt
 Signaled
 Dispatch
 Timer runout
Thread States cont.
• The primary thread can determine the state of an
entire process.
• The state of the primary thread is that same as the
state of the process, if it ’ s the only thread.
• If the primary thread is sleeping, the process is
sleeping.
• If the primary thread is running, the process is running.
• For a process that has multiple threads,
all threads of the process have to be in a sleeping or
stopped state in order for the whole process to be
considered sleeping or stopped.

More Related Content

Similar to 1 Multithreading basics.pptx

Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfHarika Pudugosula
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxpiyushlohia1857
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxAmanuelmergia
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programmingAnyapuPranav
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threadsVaibhav Khanna
 
Hardware Multithreading.pdf
Hardware Multithreading.pdfHardware Multithreading.pdf
Hardware Multithreading.pdfrajaratna4
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.pptshreesha16
 
Unit 2 part 2(Process)
Unit 2 part 2(Process)Unit 2 part 2(Process)
Unit 2 part 2(Process)WajeehaBaig
 
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Kasun Gajasinghe
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentationchnrketan
 
THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid noman zahid
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptxbleh23
 

Similar to 1 Multithreading basics.pptx (20)

OS Thr schd.ppt
OS Thr schd.pptOS Thr schd.ppt
OS Thr schd.ppt
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
 
threads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptxthreads-ppfldkgsh;reghuiregiuhrughet.pptx
threads-ppfldkgsh;reghuiregiuhrughet.pptx
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptxLecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
 
Threads .ppt
Threads .pptThreads .ppt
Threads .ppt
 
Ch4 threads
Ch4   threadsCh4   threads
Ch4 threads
 
Multi threaded programming
Multi threaded programmingMulti threaded programming
Multi threaded programming
 
Thread
ThreadThread
Thread
 
Threads.ppt
Threads.pptThreads.ppt
Threads.ppt
 
Operating system 20 threads
Operating system 20 threadsOperating system 20 threads
Operating system 20 threads
 
Thread
ThreadThread
Thread
 
Hardware Multithreading.pdf
Hardware Multithreading.pdfHardware Multithreading.pdf
Hardware Multithreading.pdf
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
 
Module2 MultiThreads.ppt
Module2 MultiThreads.pptModule2 MultiThreads.ppt
Module2 MultiThreads.ppt
 
Unit 2 part 2(Process)
Unit 2 part 2(Process)Unit 2 part 2(Process)
Unit 2 part 2(Process)
 
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
Scheduler Activations - Effective Kernel Support for the User-Level Managemen...
 
Chapter -2 operating system presentation
Chapter -2 operating system presentationChapter -2 operating system presentation
Chapter -2 operating system presentation
 
THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid THREADS of Operating System by Noman Zahid
THREADS of Operating System by Noman Zahid
 
OS Module-2.pptx
OS Module-2.pptxOS Module-2.pptx
OS Module-2.pptx
 
Threads
ThreadsThreads
Threads
 

Recently uploaded

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 

1 Multithreading basics.pptx

  • 2. What Is a Thread? • A thread is a sequence or stream of executable code within a process that is scheduled for execution by the operating system on a processor or core. • All processes have a primary thread. • The primary thread is a process ’ s flow of control or thread of execution. • A process with multiple threads has as many flows of controls as there are threads. Each thread executes independently and concurrently with its own sequence of instructions.
  • 3. user - level threads and kernel - level threads. • A process with multiple threads is multithreaded. • There are user - level threads and kernel - level threads. • Kernel - level threads are a lighter burden to create, maintain, and manage on the operating system as compared to a process because very little information is associated with a thread. A kernel thread is called a lightweight process because it has less overhead than a process.
  • 4. User - and Kernel - Level Threads • There are three implementation models for threads: • User - or application - level threads • Kernel - level threads • Hybrid of user - and kernel - level threads
  • 5. user - level threads
  • 6. kernel - level threads
  • 8. user or kernel space or mode. • These threads run in user or kernel space or mode. • In user mode , a process or thread is executing instructions in the program or linked library. • They are not making any calls to the operating system kernel. • In kernel mode , the process or thread is making system calls such as accessing resources or throwing exceptions. Also, in kernel mode, the process or thread can access objects that are defined in kernel space.
  • 9. User - level threads • User - level threads reside in user space or mode. • The runtime library, also in user space, manages these threads. • They are not visible to the operating system and, therefore, cannot be scheduled to a processor core. • Each thread does not have its own thread context. • there is only one thread per process that will be running at any given time and only a single processor core allocated to that process. • The runtime library schedules and dispatches these threads. • User - level threads are considered a “ many - to - one ” thread mapping.
  • 10. Kernel - level threads • Kernel - level threads reside in kernel space and are kernel objects. • With kernel threads, each user thread is mapped to or bound to a kernel thread. • The user thread is bound to that kernel thread for the life of the user thread. Once the user thread terminates, both threads leave the system. This is called a “ one - to - one ” thread mapping • The operating system scheduler manages, schedules, and dispatches these threads. The runtime library requests a kernel - level thread for each of the user – level threads. • The operating system creates a context for each thread.
  • 11. A hybrid thread • A hybrid thread implementation is a cross between user and kernel threads and allows both the library and the operating system to manage the threads. • The operating system selects a user thread and maps it to one of the available kernel threads in the pool. • More than one user thread may be assigned to the same kernel thread.
  • 12. Thread Context • When one process is removed from a core and another process becomes active, a context switch takes place between those processes. • The operating system must keep track of all the information that is needed to restart that process and start the new process in order for it to become active. • This information is called the context and describes the present state of the process. When the process becomes active, it can continue execution right where it was preempted. The information or context of the process includes: • Process id • Pointer to executable • The stack • Memory for static and dynamically allocated variables • Processor registers
  • 13. Hardware Threads and Software Threads • Chip manufacturers implement cores that have multiple hardware threads that serve as logical cores • Cores with multiple hardware threads are called simultaneous multithreaded (SMT) cores. • SMT brings to hardware the concept of multithreading • SMT - enabled processors execute many software threads or processes simultaneously within the processor cores
  • 14. Key Similarities and Differences between Threads and Processes
  • 15. Threads & Processes Both have id, set of registers, state, priority, and scheduling policy. Both have attributes that describe the entity to the OS. Both have an information block. Both share resources with the parent process Both function as independent entities from the parent process. The creator can exercise some control over the thread or process. Both can change their attributes. Both can create new resources. Neither can access the resources of another process.
  • 16. Threads Processes Threads share the address space of the process that created it processes have their own address. Threads have direct access to the data segment of their process processes have their own copy of the data segment of the parent process. Threads can directly communicate with other threads of their process; processes must use Interprocess Communication to communicate with sibling processes. Threads have almost no overhead processes have considerable overhead.
  • 17. Threads Processes New threads are easily created new processes require duplication of the parent process. Threads can exercise considerable control over threads of the same process processes can exercise control only over child processes. Changes to the main thread (cancellation, priority change, and so on) may affect the behavior of the other threads of the process changes to the parent process do not affect child processes.
  • 18. Setting Thread Attributes The POSIX thread library defines a thread attribute object that encapsulates a subset of the properties of the thread. These attributes are accessible and modifiable by the creator of the thread. These are the thread attributes that are modifiable: • Contention scope • Stack size • Stack address • Detached state • Priority • Scheduling policy and parameters
  • 19. The scope attribute describes which threads a particular thread competes with for resources. Threads contend for resources within two contention scopes: 1. Process scope 2. System scope
  • 20. • Threads with process scope compete with threads within the same process • threads with system wide contention scope compete for resources with threads of other processes allocated across the system. • A thread that has system scope is prioritized and scheduled with respect to all of the system wide threads. • The scheduling policy also determines when a thread is assigned the processor. FIFO, round robin (RR), and other scheduling policies are available.
  • 22.
  • 23. Thread States • The thread is the unit of execution when a process is scheduled to be executed. • If the process has only one thread, it is the primary thread that is assigned to a processor core. • If a process has multiple threads and there are multiple processors available to the process, all of the threads are assigned to processors.
  • 24. Thread States • When a thread is scheduled to execute on a processor core, it changes its state. • A thread state is the mode or condition that a thread is in at any given time. • Threads have the same states and transitions as for processes. • There are four commonly implemented states:  Runnable  Running (active)  Stopped  Sleeping (blocked) • There are several transitions:  Preempt  Signaled  Dispatch  Timer runout
  • 25. Thread States cont. • The primary thread can determine the state of an entire process. • The state of the primary thread is that same as the state of the process, if it ’ s the only thread. • If the primary thread is sleeping, the process is sleeping. • If the primary thread is running, the process is running. • For a process that has multiple threads, all threads of the process have to be in a sleeping or stopped state in order for the whole process to be considered sleeping or stopped.