Operating System
What are Threading
Thread is an execution unit that consists of its own program counter,
a stack, and a set of registers where the program counter mainly keeps
track of which instruction to execute next, a set of registers mainly
hold its current working variables, and a stack mainly contains the
history of execution
Threads are also known as Lightweight processes. Threads are a
popular way to improve the performance of an application through
parallelism.
As each thread has its own independent resource for process
execution, thus Multiple processes can be executed parallelly by
increasing the number of threads.
It is important to note here that each thread belongs to exactly one
process and outside a process no threads exist. Each thread basically
represents the flow of control separately. In the implementation of
network servers and web servers threads have been successfully used.
The given below figure shows the working of a single-threaded and a
multithreaded process:
registers
code data files code data files
register stack reg... reg... reg..
stack stack stack
thread
thread
single-threaded process multithreaded process
Types of Thread
There are two types of threads:
1. User Threads
2. Kernel Threads
User threads are above the kernel and without kernel support. These
are the threads that application programmers use in their programs.
Kernel threads are supported within the kernel of the OS itself. All
modern OSs support kernel-level threads, allowing the kernel to
perform multiple simultaneous tasks and/or to service multiple kernel
system calls simultaneously.
Let us now understand the basic difference between User level
Threads and Kernel level threads:
User Level threads Kernel Level Threads
These threads are implemented These threads are implemented by
by users. Operating systems
These threads are not
These threads are recognized by
recognized by operating
operating systems,
systems,
In User Level threads, the
In Kernel Level threads, hardware
Context switch requires no
support is needed.
hardware support.
These threads are mainly These threads are mainly designed
designed as dependent threads. as independent threads.
In User Level threads, if one On the other hand, if one kernel
user-level thread performs a thread performs a blocking
blocking operation then the operation then anotherthread can
entire process will be blocked. continue the execution.
User Level threads Kernel Level Threads
Example of User Level threads: Example of Kernel level threads:
Java thread, POSIX threads. Window Solaris.
Implementation of User Level While the Implementation of the
thread is done by a thread kernel-level thread is done by the
library and is easy. operating system and is complex.
This thread is generic in nature
This is specific to the operating
and can run on any operating
system.
system.
Multithreading Models
The user threads must be mapped to kernel threads, by one of the
following strategies:
Many to One Model
.One to One Model
Many to Many Model
Many to One Model
• In the many to one model, many user-level threads are all
mapped onto a single kernel thread.
. Thread management is handled by the thread library in user
space, which is efficient in nature.
In this case, if user-level thread libraries are implemented in the
operatingsystem in some way that the system does not support
them,then the Kernel threads use this many-to-one relationship
model.
user thread
K kernel thread
One to One Model
The one to one model creates a separate kernel thread to
handle each and every user thread.
Most implementations of this model place a limit on how many
threads can be created.
• Linuxand Windows from 95 to XP implement the one-to-one
model for threads.
This model provides more concurrencythan that of many to one
Model.
user thread
T T
K K
kernel thread
Many to Many Model
The many to many model multiplexes any number of user
threads onto an equal or smaller number of kernel threads,
combining the best features of the one-to-one and many-to-one
models.
Users can create any number of threads.
Blocking the kernel system calls does not block the entire
process.
Processes can be split across multiple processors.
What are Thread Libraries?
Thread libraries provide programmers with APIfor the creation and
management of threads.
Thread libraries may be implemented either in user space or in kernel
space. The user space involves 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.
Three typesof Thread
1. POSIX Pitheads may be provided as either a user or kernel
library, as an extension to the POSIX standard.
2. Win32 threads are provided as a kernel-level library on
Windows systems.
3. 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.