×

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

Evolution of Operating System

Evolution of Operating System The evolution of various types of operating systems can be briefly described as follows: Serial Processing Since 1950, the operating system started to be in use. Before 1950, there...

5 minutes read.

Deadlock Detection and Recovery

Deadlock Detection and Recovery In deadlock detection and recovery, to avoid or prevent a deadlock, the operating system uses various methods. For checking deadlock, an operating system continuously monitors the system. Then also,...

2 minutes read.

Directory Structure in Operating System

Directory Structure in Operating System What is a Directory A Directory is the collection of the correlated files on the disk. In simple words, a directory is like a container which contains...

8 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.

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.

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.

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.

Inter Process Communication in Operating System

Inter-Process Communication or IPC refers to the methods and techniques that allow processes (programs or tasks) to communicate with each other within a computer system. This is typically done through...

3 minutes read.

Batch Operating System

Computers were once quite huge devices that operated from a console. Tape drives, punch cards, line printers were often utilized for the output, and tape drivers or card readers for...

4 minutes read.

Operating System Tutorial

What is an Operating System? An Operating System is a System Software that handles the resources of a computer such as computer hardware and software and offers frequent services to the...

7 minutes read.

DOS Operating System

What is DOS Operating System? An operating system that runs from a hard disk drive is known as the DOS (Disk Operating System).In other words, DOS (Disk Operating System) is defined...

7 minutes read.

SJF Scheduling in OS

Shortest Job First (SJF) Scheduling in OS Shortest Job First is a Preemptive or Non-Preemptive algorithm. In the shortest job first algorithm, the job having shortest or less burst time will...

4 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.

Producer-Consumer Problem in OS

What is Producer-Consumer Problem? The Producer-Consumer problem is a classical problem. The Producer-Consumer problem is used for multi-process synchronization, which means synchronization between more than one processes. In this problem, we have one producer...

4 minutes read.

Test Set Lock Mechanism | Operating System

Modification in the Assembly Code The problem in the lock variable is that, in some cases, the process reads the previous or old value of the lock variable and enters into the critical...

4 minutes read.

Dining Philosophers Problem in Operating System

Edsger Wybe Dijkstra was a well-known Dutch scientist. He was the first person ever to present the Dining Philosophers Problem in the stream of Computer Science. The dining philosophers problem...

5 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.

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.

Disk Scheduling Algorithms

Disk Scheduling Algorithms Disk scheduling algorithms are the algorithms that are used for scheduling a disk. Generally, the scheduling refers to a time-table for completing any task or a job. With the help...

2 minutes read.

Real-time Operating System

In this essay, we will thoroughly understand the real-time operating system. Real-Time Operating System: What do you mean? For any task to be completed, a real-time operating system (RTOS), a special-purpose operating...

5 minutes read.