What is Thread in OS? User and Kernel level thread

A thread is a part of a process. A process is classified into a number of light-weighted processes, each light weighted process is known as a thread. It is a track of execution in process. The thread has a program counter that holds the path of which instruction to execute next.

It has a register which keep the current working variable and a stack which store the execution history. For example, when we open a site, then after scrolling down some ads came automatically (which is a thread), some random videos played (which is another thread). Here, opening a site is a process but ads coming and playing videos are threads.

What is Thread?

In operating systems (OS), a thread is the smallest sequence of programmed instructions that can be managed independently by a scheduler. Threads are components of a process—where a process is the collection of resources and the environment within which threads run. Unlike processes, threads within the same process share the same memory space and resources but operate independently in terms of execution flow.

There are two types of threads

  1. User level thread
  2. Kernel level thread

1. User-Level Threads

User-level threads are managed without kernel support, entirely in user space. The kernel is unaware of these threads and manages them as if each process has a single thread of control.

Advantages:

  • Speed: Operations like thread creation, destruction, and switching are faster than kernel-level threads because they don’t involve system calls.
  • Simplicity: Scheduling can be application-specific; you can design custom schedulers for your application threads.
  • Portability: Because they are implemented by a library at the user level, they are easy to move between different operating systems.

Disadvantages:

  • Blocking Issues: If one user-level thread performs a blocking operation, it can cause the entire process to block, not just the single thread.
  • Lack of Coordination with Kernel: The OS scheduler only sees the process as a whole, not the individual threads, which can lead to inefficiencies in CPU utilization.

2. Kernel-Level Threads

Kernel-level threads are supported and managed directly by the operating system. Here, the kernel performs thread creation, scheduling, and management in kernel space.

Advantages:

  • Concurrency: Multiple threads of the same process can run truly in parallel on a multiprocessor system.
  • Blocking: One thread can block without affecting the entire process; other threads can continue to run.
  • Coordination: Because the kernel has knowledge of all threads, system calls and scheduling can be more efficiently managed.

Disadvantages:

  • Overhead: Operations such as thread creation, destruction, and switching involve system calls, which can be slower than those for user-level threads.
  • Complexity: Implementing and managing kernel threads can be more complex due to the need for handling in the kernel space.