A thread is a basic unit of CPU utilization. It is also called a lightweight process. CPU threads share the code section, data section, and any operating system resources with its peer threads in a particular task which are vacant for the job.
In this article, you will be able to learn about a thread, different types of threads, their advantages and disadvantages, major differences between process and threads, etc. Let’s read it further.
A thread is a basic unit of CPU utilization. It is also called a lightweight process. It is a sequence of instructions within a process. A CPU thread behaves like a process within a process but it does not have its own PCB. Usually, multiple threads are created within a process.
The multiple CPU threads in a process allow multiple executions. It can be used to improve the application through parallelism. Threads consist of the following:
- Thread ID
- Program Counter
- Register Set
A thread shares the following with its peer threads in a particular task: Thread is a sequence of instructions within a process. It behaves like a process within a process but it does not have its own PCB. Types:Kernel-Level & User-Level
- Code Section
- Data Section
- Any operating system resources, which are vacant for the job.
Have You Read About:
Benefits of CPU Threads
Some important benefits of threads are as follows:
- Threads share common data and do not require to make use of inter-process communication.
- Threads can get an advantage of multiprocessors
- There is a higher throughput when multiple threads collaborate in a single job. The entire process does not stop if one thread must wait.
- Threads are cheap to create as they only need a stack and storage to register
- Threads use very few resources of an operating system. They do not need new address space, global data, program code, or operating system resources
- Context switching becomes fast when working with threads.
Difference B/w Process And Thread
The difference between processes and threads are as follows:
|A thread is a lightweight entity||A process is a heavyweight entity|
|A thread cannot exist without a process||There must be at least one thread in a process|
|If a thread ends, the process may still run.||If a process ends, all threads in it also end|
|A thread has no data segment or heap||A process has code, data, and heap segments|
|The communication among threads occurs via memory||The communication among processes occurs via the operating system and data copying|
|The creation of threads and context switching is inexpensive||The creation of processes and context switching is expensive.|
Types of CPU Threads
Threads may be handled at different levels. There are two types of CPU threads to manage in a modern system. One is called the user thread and 2nd is the kernel thread.
- Kernel-Level Thread
- User-Level Thread
Kernel-level threads are supported within the kernel of the operating system. All modern operating systems support kernel-level threads. They allow the kernel to perform multiple tasks and to service multiple kernel system calls simultaneously.
Different advantages of kernel-level threads are as follows:
- The kernel has full knowledge of all threads. Therefore the scheduler may decide to give more time to process with a large number of threads than the process having a small number of threads.
- Kernel-level threads are especially good for applications that are blocked frequently.
Different disadvantages of kernel-level CPU threads are as follows:
- The kernel-level threads are slow and incompetent
- The kernel must manage and schedule threads as well as the processes. It requires a full thread control block (TCB) for each thread to maintain information about threads. There is significant overhead and increases the kernel complexity.
User Level Threads
User-level threads are implemented in user-level libraries instead of systems calls. The thread switching does not need to call the operating system. It does not cause interruptions to the kernel. The kernel knows nothing about user-level CPU threads. It manages these threads as single-threaded processes. The user-level threads are very fast.
Different advantages of user-level threads are as follows:
- The user-level threads are fast and efficient
- They can be implemented on an operating system that does not support threads
- They are simple to manage. The switching between threads can be done without a kernel
Different disadvantages of user-level threads are as follows:
- There is a lack of coordination between threads and the operating system kernel. The process gets a single time slice even if it has a large number of threads in it.
- A multi-threaded application with pure user-level threads cannot take advantage of multiprocessing. The kernel assigns only one process to a processor at a time.