Operating System - Multi-Threading
Operating System - Multi-Threading
tutorialspoint.com/operating_system/os_multi_threading.htm
What is 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 shares with its peer threads few information like code segment, data segment and
open files. When one thread alters a code segment memory item, all other threads see that.
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 overhead thread is equivalent to a
classical process.
Each thread belongs to exactly one process and no thread can exist outside a process. Each
thread represents a separate flow of control. Threads have been successfully used in
implementing network servers and web server. They also provide a suitable foundation for
parallel execution of applications on shared memory multiprocessors. The following figure
shows the working of a single-threaded and a multithreaded process.
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 1/7
30/9/2018 Operating System - Multi-Threading
1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser
resources than a process.
2 Process switching needs interaction with operating Thread switching does not need to
system. interact with operating system.
3 In multiple processing environments, each process All threads can share same set of
executes the same code but has its own memory and open files, child processes.
file resources.
4 If one process is blocked, then no other process can While one thread is blocked and
execute until the first process is unblocked. waiting, a second thread in the same
task can run.
5 Multiple processes without using threads use more Multiple threaded processes use
resources. fewer resources.
6 In multiple processes each process operates One thread can read, write or change
independently of the others. another thread's data.
Advantages of Thread
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 Thread
Threads are implemented in following two ways −
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 2/7
30/9/2018 Operating System - Multi-Threading
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.
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
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 3/7
30/9/2018 Operating System - Multi-Threading
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.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level thread facility.
Solaris is a good example of this combined approach. 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. Multithreading models are three types
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.
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 4/7
30/9/2018 Operating System - Multi-Threading
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.
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 5/7
30/9/2018 Operating System - Multi-Threading
Disadvantage of this model is that creating user thread requires the corresponding Kernel
thread. OS/2, windows NT and windows 2000 use one to one relationship model.
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 6/7
30/9/2018 Operating System - Multi-Threading
1 User-level threads are faster to create and manage. Kernel-level threads are slower to create
and manage.
3 User-level thread is generic and can run on any Kernel-level thread is specific to the
operating system. operating system.
https://www.tutorialspoint.com/operating_system/os_multi_threading.htm 7/7