Home »
Operating System
Thread Implementation in Operating System
In this tutorial, we will learn about thread implementation in the Operating System, states of threads, and different ways to implement a thread package.
By Mahak Jain Last updated : May 07, 2023
Overview
Each process has an address space. There is one thread of control in every traditional OS. Sometimes, it is viable to have multiple threads of control in the similar address space which is running in quasi-parallel. Though they were separate processes they have same shared address space.
Threads are used in case of multiple applications running at the same particular time few activities might block from one point of time to another. By decomposition into multiple threads that are running in quasi-parallel, the programming model becomes simpler and easier.
The new element can only be added with threads. This ability to share the same address space and data is essential for some applications.
There are no resources attached to threads. Processes are difficult to create and destroy but threads, on the other hand, can be easily created and destroyed. Creating a thread isabout100x faster than creating a process.
The thread has program counter(pc)to keep the track of the instruction to be executed next. It also has registers to hold the presently working variables. There is a stack to store the execution history there is one frame for one procedure called but not still returned from.
Threads are scheduled for the implementation or execution on CPU.
What are the different states of a thread?
The following are the four states of a thread:
- Running
- Blocked
- Read
- Terminated
The stack of each thread is as follows:
What are the different ways to implement a thread package?
There are two ways of implementing a thread package:
- In user space
- In kernel
1. Threads implementation in the user space
In this model of implementation, the threads package entirely in user space, the kernel has no idea about it. A user-level threads package can be executed on an operating system that doesn't support threads and this is the main advantage of this implementation model i.e. Threads package in user space.
2. Threads implementation in the kernel
In this method of implementation model, the threads package completely in the kernel. There is no need for any runtime system. To maintain the record of all threads in the system a kernel has a thread table.
A call to the kernel is made whenever there is a need to create a new thread or destroy an existing thread. In this, the kernel thread table is updated.
Other ways to implement a thread package
The following are the other two ways are as follows:
- Hybrid implementation
- Scheduler activation
i) Hybrid Implementation
In this implementation, there is some set of user-level threads for each kernel level thread that takes turns by using it.
ii) Scheduler Activation
The objective of this scheduler activation work is to replicate the working or function of kernel threads, but with higher performance and better flexibility which are usually related to threads packages which are implemented in userspace.
Pop-up Threads
In this system, a new thread is created just to handle the message as soon as the arrival of a message takes place and thus it is called pop up thread.
The benefit of this pop-up thread is that they are brand new and hence don’t need any stacks or history registers, etc. that must be restored. Each pop-up thread is fresh and new and is similar to all other pop up threads. This is the reason why a pop-up thread can be created very quickly. The incoming message to be processed is given to the new thread.
The result of using such a thread is mainly that the latency between the arrival of the message and the start of processing is made very short.