OS Threads Unit 3 Copy 1
OS Threads Unit 3 Copy 1
Chapter : Threads
· Overview
· Benefits of Threads vs Processes
· Single and Multithreaded Processes
· Types of Threads
· Multithreading Models
· Thread Libraries
Process Characteristics
Concept of Process has two facets.
▪A Process is:
• A Unit of resource ownership:
▪ a virtual address space for the process image
▪ control of some resources (files, I/O devices...)
• A Unit of execution - process is an execution path
through one or more programs
▪ may be interleaved with other processes
▪ execution state (Ready, Running, Blocked...) and
dispatching priority the kernel threads
These two characteristics are treated separately:
• The unit of resource ownership is usually referred
to as a process or task
• The unit of execution is usually referred to a
thread or a “lightweight process”
Process and Thread
Single Thread Multiple Thread
Threads
Threads
▪ It is single sequential (flow of) execution of tasks of process
▪ A thread (or lightweight process ) is a basic unit of CPU utilization;
it consists of:
▪ program counter
▪ Thread id
▪ register set
▪ stack space
▪ A thread shares with its peer threads its:
▪ code section
▪ data section
▪ operating-system resources
Single and Multithreaded Processes
Threads contd..
Advantages
▪ Thread switching does not involve the kernel: no mode
switching
▪ Therefore fast
▪ Scheduling can be application specific: choose the best
algorithm for the situation.
▪ Can run on any OS. We only need a thread library
Disadvantages
▪ Most system calls are blocking for processes. So all threads
within a process will be implicitly blocked
▪ The kernel can only assign processors to processes. Two
threads within the same process cannot run simultaneously on
two processors
Kernel Level Threads
▪ All thread management is done by kernel
▪ No thread library; system call used for kernel thread facility
▪ Kernel maintains context information for the process and the
threads
▪ Switching between threads requires the kernel
▪ Kernel does Scheduling on a thread basis
Advantages
▪ The kernel can schedule multiple threads of the same process on
multiple processors
▪ Blocking at thread level, not process level, If a thread blocks, the CPU
can be assigned to another thread in the same process
Disadvantages
▪ Thread switching always involves the kernel. This means 2 mode
switches per thread switch
▪ So it is slower compared to User Level Threads
•
Combined ULT/KLT Approaches
▪ Thread creation done in the user space
▪ Bulk of thread scheduling and synchronization done in user
space
▪ ULT’s mapped onto KLT’s
• The programmer may adjust the number of KLTs
▪ KLT’s may be assigned to processors
▪ Combines the best of both approaches
(e.g. Solaris)
Solaris
▪ Process includes the user’s address space, stack, and
process control block
▪ User-level threads (threads library)
• invisible to the OS &are the interface for application parallelism
▪ Kernel threads
• the unit that can be dispatched on a processor
▪ Lightweight processes (LWP)
• each LWP supports one or more ULTs and maps to exactly one
KLT
Solaris
Solaris: Kernel Level Thread
▪ Only objects scheduled within the system
▪ May be multiplexed on the CPU’s or tied to a specific CPU
▪ Each LWP is tied to a kernel level threadUser-level threads
(threads library)
Multithreading Models
▪ One-to -One
▪ Many-to -One
▪ Many-to -Many
One -to -one Model
Each user -level thread maps to one kernel thread
Advantage :
facilitates the running of multiple threads in parallel/ greater
concurrency.
new user thread must include the creation of a corresponding kernel thread
Many-to -One