A thread is a basic unit of CPU utilization consisting of a program counter, a stack, and a set of registers. A thread is a path of execution within a program. A program can contain multiple threads.
Threads can’t exist outside any process and each thread belongs to exactly one process. Information like code segments, files, and data segments can be shared by different threads.
Type of threads
User Level Thread – These are created by the user and the kernel is not aware of its existence. User-level threads are easy to create and much faster than kernel threads. They are represented by a program counter, register, stack, and a small process control block.
Kernel Level Thread – Kernel Level Thread is handled by the operating system and the thread management is done by the kernel itself. Information for the process as well as the process threads are all managed by the kernel that’s why it is slower than the user-level thread.
Type of threaded system:-
As you can see in the case of a single-threaded process simply a single task is performed but in the case of multiple threaded systems multiple tasks performed at a time by making optimal use of resources like code, data, and files.
The benefit of using Multi threaded process:-
Responsiveness – Multi-threaded process is an interactive application that may allow a program to continue running even if part of it is blocked or is performing a lengthy operation thereby increasing responsiveness to the user.
Resource Sharing – Threads in a multi-threaded program share the memory and the resources of the process, to which they belong. The benefit of sharing data and code is that it allows an application to have several different threads of activity within the same address. For example – Threads sharing code, data, and files with each other.
Economical in Nature – Allocating memory and resources for process creation is costly. Thread sharing resources so they are economical in nature.
Utilization of multiprocessor architecture – If there is only one thread then it is not possible to divide the process into small tasks that different processors can perform. The single-threaded process running on one processor regardless of how many processors are available but in the case of Multithreaded small tasks are assigned to multiple processors and it increases CPU utilization.
Ways of creating a relationship between User thread and kernel thread
There are three basic ways of creating a relationship between user and kernel thread
Many to One model –
The Many to One model connects different user threads to a single Kernel thread. This one is efficient as the userspace manages the thread management.
Some of the drawbacks of this model:
1. The entire process blocks if a thread makes a blocked system call.
2. Only one thread accesses the kernel thread at a time.
3. Many threads are enabled to run in parallel on multiprocess.
One to One model –
In the One to One model, we map each user thread to a separate kernel thread. It allows another thread to run when a thread makes a blocking system call. This model allows multiple threads to run in parallel on multiple processors.
Some of the drawbacks are:
1. Creating a User thread requires creating the corresponding Kernel thread.
2. Creating a lot of Kernel threads can burden the performance of an application and there is a restriction on the number of threads in the system.
Many to Many model –
In many-to-many model, many user-level threads are mapped to a small or equal number of Kernel threads. We can create as many user threads as necessary and the corresponding kernel thread can run in parallel on a multiprocessor.
In this Many to Many model when a thread performs a blocking system call, the kernel can schedule another thread for execution.
This is a very basic view of threads. If you like the article please share and subscribe.