Facebook Pixel

Operating System - Multithreading

What is a Thread?

A thread is the smallest unit of execution within a process. It represents a path through the program's code and includes its own program counter (which tracks the next instruction), a set of registers (to hold temporary data), and a stack (to manage function calls and execution flow).

Threads within the same process share common resources such as code, data, and open files. This shared environment allows easy communication between threads. For example, if one thread modifies shared data, other threads can immediately access those changes.

Threads are often called lightweight processes because they require fewer resources compared to full processes. They improve application performance by enabling parallel execution and reducing overhead. Every thread belongs to a single process, and multiple threads together allow a program to perform multiple tasks simultaneously. Threads are widely used in systems like web servers and network applications.

Process vs Thread

ProcessThread
Resource-heavy and independentLightweight and part of a process
Requires OS interaction for switchingFaster switching with minimal OS involvement
Has its own memory and resourcesShares memory and resources with other threads
If blocked, no other process can proceedOther threads can continue if one is blocked
Uses more system resourcesUses fewer resources
Operates independentlyThreads can interact and share data

Advantages of Threads

  • Faster context switching compared to processes
  • Enables concurrent execution within a process
  • Easier communication due to shared memory
  • Lower cost of creation and management
  • Better utilization of multi-core processors

Types of Threads

Threads can be implemented in two main ways:

  • User-Level Threads (managed by user programs)
  • Kernel-Level Threads (managed by the operating system)

User-Level Threads

In this approach, thread management is handled entirely in user space using thread libraries. The operating system kernel is not aware of these threads.

The library provides functionality for creating, scheduling, and managing threads. The application typically starts with a single thread and can create more as needed.

  • No need for kernel-level support during switching
  • Portable across different operating systems
  • Faster creation and management
  • Flexible scheduling controlled by applications
  • Blocking system calls can halt the entire process
  • Cannot fully utilize multiple processors

Kernel-Level Threads

In this model, the operating system directly manages threads. All thread-related activities, such as creation, scheduling, and management, are handled by the kernel.

The kernel maintains detailed information about each thread and schedules them individually.

  • Supports true parallel execution on multi-core systems
  • If one thread is blocked, others can continue running
  • Kernel itself can run multiple threads
  • Slower to create and manage compared to user-level threads
  • Requires switching between user mode and kernel mode

Multithreading Models

Some operating systems combine both user-level and kernel-level threading. This allows better performance and flexibility. There are three common models:

Many-to-Many Model

In this model, multiple user threads are mapped to multiple kernel threads. The number of kernel threads may be equal to or less than user threads.

This approach allows high concurrency and efficient use of processors. If one thread is blocked, others can continue execution.

Many-to-One Model

Here, multiple user threads are mapped to a single kernel thread. Thread management happens in user space.

While this model is simple, it has limitations: if one thread blocks, the entire process stops, and it cannot take advantage of multiple processors.

One-to-One Model

In this model, each user thread is mapped to a corresponding kernel thread.

This allows better concurrency and supports parallel execution. However, creating many threads can increase overhead since each requires a kernel thread.

Operating systems like Windows and older systems such as OS/2 follow this model.

User-Level vs Kernel-Level Threads

User-Level ThreadsKernel-Level Threads
Faster to create and manageSlower due to kernel involvement
Managed by thread librariesManaged by operating system
Can run on any OSOS-dependent implementation
Limited multiprocessing supportFull support for parallel execution
Conclusion
Multithreading allows a program to execute multiple tasks at the same time within a single process. It improves efficiency, responsiveness, and CPU utilization. Threads can be managed either at the user level or by the kernel, each having its own benefits and limitations. Different threading models provide flexibility in how threads are mapped and executed, helping systems achieve better performance depending on their design.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.