What is Thread
A thread means a lightweight process. Thread is the basic unit of CPU execution, which consists of thread ID, Program counter, set of registers to hold the information of current working variable, and Stack, which includes the history of execution.
A thread shares some information to its associated thread and the information consists of open files, the data segment, and code segments. If a thread changes a code segment memory item, then the other threads can see the changes in the thread.
In other words, the thread is defined as a flow of execution via the process code. Thread improves the performance of applications with the help of parallelism because threads are executed in a parallel manner.
Every thread is related to a single process, and without process, thread cannot exist. Every thread shows a distinct flow of control. Threads are used in implementing web servers and network servers. Parallel execution of the application is also possible in threads, which can be shared among the memory multiprocessors.
The below figure shows the working of a Single-threaded and a Multithreaded process.
Difference between Process and Thread
|A process can be defined as a program in execution.||A thread can be defined as the flow of execution via the process code.|
|In the process, switching requires interaction with the operating system.||In thread switching, there is no requirement to interact with the operating system.|
|It is heavyweight.||It is lightweight.|
|In a process, if a process is blocked due to some reasons, then the other processes cannot be executed until the process which is blocked will not be unblocked.||In a thread, if one thread is blocked, then the other thread is able to do the same task.|
|The Process consumes more resources.||Thread consumes fewer resources.|
|Context switching requires more time in process.||Context switching requires less time in thread.|
|The Process needs more time for termination.||The Thread takes less time for termination.|
|The Process takes more time for creation||The Thread takes less time for execution.|
|In terms of communication, the process is less efficient.||In terms of communication, the thread is more efficient.|
|In Process switching, the interface in the operating system is used.||In thread switching, no need to call an operating system.|
|It is Isolated.||It shares memory.|
Advantages of Thread
- Better CPU utilization: - If multiple threads are existing in one process, they can be scheduled on different processes, which makes the process execution quicker.
- Increased throughput of the system: - If a process is broken into different threads, and each thread task is considered as one job, and the number of jobs done is increased per unit of time. Thus, the throughput of the system also increases.
- Communication: - In the thread, communication is quite simple because each thread shares a common address space. But in the process, we use some specific communication methods for communication between the process.
- Resource sharing: - Resource can be shared easily between all threads within a process such as data, files, and code.
- Responsiveness: - When the process is broken into different threads and if any file finished its execution, the output of the thread is returned instantly.
- Quick Context Switch: - The time of context switching in threads is less than the context switching time of the process.
Types of Thread
There are two types of Threads
- User-level Threads
- Kernel-level Threads
- User-Level Threads: - User-level threads are those types of thread that are created in the user-level library. These threads are not implemented with the help of the system calls. If we want to switch thread, then there is no need to interrupt the kernel and call operating system. In the user-level thread, there is no need for the kernel to know about the user-level thread and handle threads, if a process containing a single thread.
Examples of a User-level threads are POSIX threads, java threads.
Advantages of User-Level Threads
- User-level thread is easy to create because in this, there is no involvement of kernel.
- It is fast because there is no need for operating system calls.
- It is easy to run a user-level thread in any operating system.
Disadvantages of User-Level Threads
- If in one thread page Fault is caused, then the whole process is blocked.
- In the User-level thread, there is no coordination between threads and the kernel.
- Kernel-Level Threads: - Kernel-level threads are those threads that are directly managed by the operating system, and the kernel does the management of the thread.
- In this, rather creating a thread table for each process, kernel has its own table that is a master table that is used to keep track of all the threads in the system. It is also keeping track of the classical process table to maintain track of the processes. The Operating system kernel offers a system call to handle and implement a thread.
Example of a kernel-level thread is window Solaris.
Advantages of Kernel-Level Thread
The advantages of Kernel-level thread are:
- Kernel-level thread is useful for applications that are blocked frequently.
- Because the kernel has complete knowledge of the system’s threads, the scheduler may decide to give more time to those processes which have a different number of threads.
Disadvantages of Kernel-Level Thread
The disadvantages of kernel-level thread are:
- Kernel-level threads are slow
- Kernel-level threads are not efficient.
- Kernel-level thread is overhead because it needed a thread control block.
Difference between User-Level Threads and Kernel-Level Thread:
|User-Level Thread||Kernel-Level Thread|
|The thread which is created with the help of a user is known as User-level thread.||The threads which are created with the help of an operating system are known as Kernel-Level thread.|
|In the User-level thread, the time of context switching is less.||In the kernel-level threads, the time of context switching is more.|
|The operating system cannot recognize the User-level threads||The operating system can recognize the kernel-level threads.|
|In User-level threads, for context switching, there is no need for hardware support.||In kernel-level threads for context switching, hardware support is required.|
|Examples of User-level threads are POSIX threads, java threads.||An example of a kernel-level thread is window Solaris.|
|The User-Level threads are implemented in an easy way.||The Kernel-Level threads are difficult to Implement.|
|In the User-Level-Thread, if any of the thread performs the blocking operations, then due to blocking operation, the whole process will be blocked.||In the kernel-level thread, when one kernel-level thread performs blocking operation, then due to of blocking operations, other thread will not affect the other thread so, the remaining thread continues its execution.|
Multithreading means using both types of threads User-Level as well as Kernel-Level Thread. An Example of multithreading is Solaris.
Multithreading Models can be classified into three types:
- One to One Model
- Many to One Model
- Many to Many Model
- One to One Model: - One to One model is a Multithreading Model in which One to One relationship exists between the kernel level and User Level Thread. One to One Models can run multiple threads on multiple processors. In one-to-one Model, to create a User-Level thread we also need kernel thread. This is a problem in one to one model.
- Many to One Model: - In Many to One Multithreading model, many user threads are mapped to a single kernel. If due to user thread, the system call is blocked, then the whole process is blocked. In this, the task of thread management is managed by the thread library in the convenient user space.
- Many to Many Model: - In Many to Many Multithreading models, the user can create multiple threads, and if the kernel system call is blocked, then the entire process does not block.
This model multiplexes multiple user-level threads onto smaller or an equal number of kernel-level threads.
In this, processes can be divided across multiple processors.
Advantages of Multithreading
The advantages of Multithreading are:
- Context switching is easy in Multithreading.
- Resource sharing is easy, so it offers better utilization of resources.
- Implementing and managing threads is quite simple.
The multithreading issues are:
- Fork() System Call: - Fork() is defined as a system call that is executed in the kernel. With the help of Fork(), a copy of the process is created. The issue in multithreading is that when one thread forks, the whole process should be copied or not.
- Thread Cancellation: - Thread cancellation is another issue in multithreading in which the thread terminates before the thread completes its working. We can use two approaches to perform thread cancellation.
Asynchronous Cancellation- It terminates the destination thread.
Deferred Cancellation: - It checks the destination thread regularly if it should be canceled or not.
3.Security: - Security is another issue that arises in Multithreading because, in multithreading, resources are shared extensively among multiple threads.
4.Signal Handling: - In UNIX systems, signals are used to Alert a process that there has been a particular event.
When the multithread process receives a signal for the delivery of thread, then there may be a chance that it can be distributed to everyone or just to a single thread.
Thread libraries means the API for the implementation and management of threads, offered to the programmers. Thread libraries can be created in any space that is either kernel space or user space. The Kernel space consists of system calls and needs a kernel along with the support of the thread library. The user space consists of API functions that are created only within the user space.