×

What is Thread and Types of Thread

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.

What is Thread and Types of Thread

Difference between Process and Thread

         Process              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

  1. Better CPU utilization: - If multiple threads are existing in one process, they can be scheduled on different processes, which makes the process execution quicker.
  2. 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.
  3. 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.
  4. Resource sharing: - Resource can be shared easily between all threads within a process such as data, files, and code.
  5. Responsiveness: - When the process is broken into different threads and if any file finished its execution, the output of the thread is returned instantly.
  6. 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

  1. User-level Threads
  2. Kernel-level Threads
  3. 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.

What is Thread and Types of Thread

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.

What is Thread and Types of Thread

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 Models

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:

  1. One to One Model
  2. Many to One Model
  3. Many to Many Model
  1. 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.
What is Thread and Types of Thread
  • 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.
What is Thread and Types of Thread
  • 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.

What is Thread and Types of Thread

Advantages of Multithreading

The advantages of Multithreading are:

  1. Context switching is easy in Multithreading.
  2. Responsiveness
  3. Resource sharing is easy, so it offers better utilization of resources.
  4. Implementing and managing threads is quite simple.

Multithreading Issues

The multithreading issues are:

  1. 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.
  2. 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

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.


Related Topics

Monitors in Operating System

Monitors in Operating System Monitors are used for process synchronization. With the help of programming languages, we can use a monitor to achieve mutual exclusion among the processes. Example of monitors:...

3 minutes read.

Process State in OS

The Process passes from different states, from its formation to completion. The following are the states of the Process. NewReadyRunningBlock or WaitTerminated or CompletedSuspend readySuspend wait or suspend blocked 1. New: - The state in which a process...

2 minutes read.

Thrashing in Operating System

A virtual memory system's poor performance when the same pages are loaded repeatedly due to a lack of main memory to store them in secondary memory is referred to as...

3 minutes read.

How to implement Monitors using Semaphores

Monitors Monitor is a type of synchronization device designed to solve difficulties caused by semaphores, such as timing errors. Monitors are the type of data types that are abstract by nature...

2 minutes read.

File Access Methods in Operating System

File Access Methods in OS There are various types of file access methods in the operating system: Sequential Access MethodDirect Access MethodIndex Sequential Access Method Sequential Access Method: - Sequential Access Method is...

3 minutes read.

Process Synchronization | Operating System

Process Synchronization Process Synchronization means managing the process in such a manner so that no two processes have access to share similar data and resources. We can use Process Synchronization in a Multi-Process System...

4 minutes read.

Why is it critical for the Scheduler to distinguish between I/O-bound and CPU-bound programs

A CPU-bound process is one that spends most of its time executing instructions on the processor. A process that is I/O-bound spends most of its time waiting for input and...

2 minutes read.

FCFS Scheduling in OS

FCFS: First Come First Serve Scheduling in OS FCFS is a non-preemptive and preemptive scheduling algorithm that is easy to understand and use. In this, the process which reaches first is...

3 minutes read.

File in Operating System

What is File in Operating System?  A file is defined as a collection of the interrelated information that is stored in the secondary memory or the non-volatile memory such as optical...

4 minutes read.

Process Scheduling in Operating System

Process Scheduling is an essential component of a Multiprogramming Operating System. So now let’s understand how it works by taking a real-life example. Suppose that you want to call someone...

6 minutes read.

Advantages of UNIX Operating System

What is UNIX OS? UNIX is a family of multitasking, multiuser operating systems that are used on a wide variety of computer platforms. It was developed in the late 1960s and...

4 minutes read.

Components of Operating System

Components of Operating System: An operating system is a complex and vast system. We can only create the operating system by dividing it into small parts. Each part must be...

5 minutes read.

Partitioning Algorithms

Partitioning Algorithms There are various types of Partitioning Algorithm: First-Fit AlgorithmNext-Fit AlgorithmBest-Fit AlgorithmWorst-Fit AlgorithmQuick-Fit Algorithm 1. First-Fit Algorithm: - In the First-Fit Partitioning algorithm, first, the linked list is scan, and when it...

3 minutes read.

Turn Variable or Strict Alternative Approach

Turn Variable or Strict Alternative Approach Turn Variable Approach is used for process synchronization mechanism, which offers synchronization between two processes. Turn variable is implemented on user mode, and it is a software mechanism. We...

3 minutes read.

Lock Variable Mechanism | Operating System

Lock Variable Mechanism The Lock variable mechanism is a synchronization mechanism that is implemented in a user mode. It is a software procedure. Lock variable is a solution for busy waiting that can be...

3 minutes read.

Memory Management in Operating System

The term "Main Memory" refers to the computer's internal physical memory. It is distinguished from external mass storage devices like disk drives by using the term "main." RAM is another...

4 minutes read.

Resource Allocation Graph in Operating System

Resource Allocation Graph in OS We use the resource allocation graph for the pictographic representation of the state of a system. The resource allocation graph contains all the information related to the processes that...

5 minutes read.

Can a Computer Run Without the Operating System

Computers may function right out of the box, mostly due to operating systems. Nowadays, there are probably already some pre-installed apps in a new computer when you buy one. Have...

9 minutes read.

Starvation in Operating System

Starvation is one of the major problems occurring when resource management is not done properly. This problem arises in the operating system when the resources are not being allocated for...

4 minutes read.

Fixed Partitioning in Operating System

Fixed Partitioning in OS Fixed Partitioning is also known as Contiguous memory allocation. Fixed Partitioning is the easiest method, which is used to load more than one process into the main memory. In Fixed...

5 minutes read.