Os Threads Lecture 7
Os Threads Lecture 7
Chapter : Threads
• Overview
• Benefits of Threads vs Processes
• Single and Multithreaded Processes
• Types of Threads
• Multithreading Models
• Thread Libraries
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
Threads (Cont.)
• In a multiple threaded task, while one thread is blocked and
waiting, a second thread in the same task can run.
8
Types of Threads
1. User Level Threads: (ULT)
• Threads of user application process.
• ULT are supported above the kernel and managed
without kernel support
• These are implemented in user space in main
memory. And managed bu user level library.
• The kernel is not aware of the existence of threads
• User Level Library is used for – Thread Creation,
Scheduling and Management
Threads (Cont.)
• ULT require a kernel system call to operate
Disadvantages
•In a typical operating system, most system calls are
blocking.
•Multithreaded application cannot take advantage of
multiprocessing.
Kernel Level Threads(KLT)
• Threads of processes defined by OS itself
• KLT are supported and managed directly by OS.
• Kernel performs Thread Creation, Scheduling and
Management in kernel space.
• No thread library but system calls to the kernel
facility exists.
Kernel Level Threads(KLT)
• Kernel level threads are managed by the OS
• thread operations (ex. Scheduling) are
implemented in the kernel code.
• Kernel level threads may favor thread heavy
processes.
• they can also utilize multiprocessor systems by
splitting threads on different processors or cores.
• If one thread blocks it does not cause the entire
process to block.
• KLT are not portable because the implementation
is operating system dependent.
Kernel Level Threads(KLT)
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.
Combined ULT/KLT Approaches
• Thread creation done in the user space
• In a combined system, multiple threads within the
same application can run in parallel on multiple
processors and a blocking system call need not block
the entire process
• The programmer may adjust the number of KLTs
• Example is Solaris
Lightweight processes (LWP) each LWP supports one or
more ULTs and maps to exactly one KLT
Thread library
The interface to multithreading support is through a library.
1. User Levels
-Actual code and data structures are held within the user space
- The actual code and data structures are held within the kernel
space
• Many-to-One
• Many-to-Many
One-to-One
• Each user-level thread maps to one kernel thread
• The one-to-one model associates a single user-level thread to a
single kernel-level thread.
• kernel level threads follow the one to one model
Advantage:
facilitates the running of multiple threads in parallel.
Drawback:
Generation of every new user thread must include the creation of a
corresponding kernel thread causing an overhead
One-to-one Model