Operating System - Threads
What are Threads?
A thread is the smallest unit of execution within a process. It is often referred to as a lightweight process. Each thread represents a separate flow of control inside a program and has its own program counter (to track the next instruction), a set of registers (to store temporary data), and its own stack (to manage function calls and execution history).
Even though threads run independently, they share certain resources of the process they belong to, such as the code section, data section, and open files. This shared environment allows threads to communicate easily. For instance, if one thread modifies shared data, other threads can immediately see those changes.
Threads are mainly used to enable parallel execution. By breaking a process into multiple threads, tasks can run simultaneously while sharing the same memory and resources. Common examples include multiple tabs in a web browser or different operations in a text editor.
Each thread exists within a process, and no thread can operate independently outside of one. They are widely used in applications like web servers and network systems, where handling multiple tasks at once is essential.
Why Are Threads Important?
Threads provide several benefits in operating systems:
- Responsiveness: If one thread is busy, others can still respond to user actions.
- Resource Sharing: Threads share memory, making communication faster and simpler.
- Efficiency: Creating and managing threads requires fewer resources than processes.
- Parallel Execution: Threads allow programs to run tasks simultaneously on multi-core systems.
- Improved CPU Usage: While one thread waits (e.g., for I/O), others can continue working.
- Simpler Design: Large tasks can be divided into smaller, manageable parts.
Components of a Thread
- Thread ID: A unique identifier used by the operating system to manage threads.
- Program Counter: Keeps track of the next instruction to be executed.
- Register Set: Stores temporary data and working variables.
- Stack: Holds local variables, function parameters, and return addresses.
Types of Threads
- 1. User-Level Threads (ULTs): These are handled in user space by libraries. The operating system kernel is not aware of them and treats the process as a single thread. Thread operations like scheduling and management are handled at the user level.
- 2. Kernel-Level Threads (KLTs): These are managed directly by the operating system kernel. The kernel keeps track of all threads and performs operations such as scheduling and creation.
Thread Operations
Common operations performed on threads include:
- Creation: A new thread is created within a process with its own execution context.
- Termination: A thread ends either after completing its task or being stopped manually.
- Join: One thread waits for another to finish before continuing execution.
Process vs Thread
| Process | Thread |
|---|---|
| Requires more resources (heavyweight) | Uses fewer resources (lightweight) |
| Switching is slower and more complex | Switching is faster and simpler |
| Has its own separate memory space | Shares memory with other threads |
| Entire process may block | Only the affected thread blocks |
| Processes are independent | Threads are interdependent |

Comparing memory space and control flows between single and multi-threaded processes
Advantages and Disadvantages of Threads
| Advantages | Disadvantages |
|---|---|
| Faster context switching compared to processes | Additional memory needed for each thread stack |
| Enables concurrency within a process | Possibility of data inconsistency due to shared memory |
| Efficient communication due to shared memory | Increased complexity in programming |
| Lower overhead in creation and management | Higher risk of security issues since threads share data |
| Better utilization of multi-core processors |
Sort the Concepts
Assign each characteristic to either a 'Process' or a 'Thread'.
