OS Notes
Understanding threads as lightweight processes — thread components, user-level vs kernel-level threads, thread models (one-to-one, many-to-one, many-to-many), and threading benefits.
Introduction
Suppose you open Microsoft Word. You start typing a document. While you type, Word simultaneously checks your spelling (red underlines), auto-saves your work every few minutes, and responds to your mouse clicks on the toolbar. How does a single process do multiple things at the same time? The answer is threads.
A thread is the smallest unit of CPU execution within a process. While a process is the unit of resource ownership (memory, files, devices), a thread is the unit of scheduling and execution. A process can have multiple threads, all sharing the same address space but each executing independently with their own program counter, stack, and registers.
Thread vs Process
| Aspect | Process | Thread |
|---|---|---|
| Address space | Own private address space | Shares process address space |
| Creation time | Slow (new page tables, memory) | Fast (shares existing memory) |
| Context switch | Expensive (TLB flush, cache cold) | Cheap (same address space) |
| Communication | IPC needed (pipes, shared memory) | Direct memory access (shared heap) |
| Independence | Crash does not affect others | Crash can bring down all threads |
| Resources | Has own file descriptors, signals | Shares files, signals with siblings |
What Each Thread Has (Private)
- Thread ID: Unique identifier within the process
- Program Counter: Where this thread is in the code
- Register Set: Current CPU register values for this thread
- Stack: Local variables, function call frames (separate per thread)
- State: Running, Ready, Blocked (per thread)
What Threads Share (With Other Threads in Same Process)
- Code section: All threads execute the same program code
- Data section: Global variables accessible by all threads
- Heap: Dynamically allocated memory shared by all threads
- Open files: File descriptors shared
- Signals: Signal handlers shared
Benefits of Multithreading
1. Responsiveness
A GUI application can have one thread handling user input while another thread performs lengthy computation. The UI remains responsive even during heavy processing.
2. Resource Sharing
Threads naturally share memory, making communication between them trivial compared to inter-process communication. No pipes, sockets, or shared memory segments needed — just read/write shared variables (with synchronization).
3. Economy
Thread creation is 10-100x faster than process creation. Context switching between threads of the same process is 2-5x faster than between processes. Memory overhead is minimal since code/data/heap are shared.
4. Scalability
On multi-core processors, threads can execute truly in parallel — one thread per core. A single-threaded process can only use one core regardless of how many are available.
Single-threaded on 4 cores
Core 1: [BUSY - process running]
Core 2: [IDLE]
Core 3: [IDLE] ← 75% waste!
Core 4: [IDLE]
Multi-threaded (4 threads) on 4 cores
Core 1: [BUSY - Thread 1]
Core 2: [BUSY - Thread 2]
Core 3: [BUSY - Thread 3] ← Full utilization!
Core 4: [BUSY - Thread 4]
User-Level Threads vs Kernel-Level Threads
User-Level Threads (ULT)
Managed entirely by a user-space thread library (like POSIX green threads). The kernel is unaware of their existence — it sees only one process with one thread.
Advantages: Ultra-fast creation and switching (no system call needed), portable across any OS Disadvantages: If one thread blocks (I/O), ALL threads in the process block (kernel blocks the whole process). Cannot exploit multiple CPU cores.
Kernel-Level Threads (KLT)
Managed directly by the OS kernel. Each thread is a schedulable entity known to the kernel.
Advantages: True parallelism on multi-core, one thread blocking does not affect others Disadvantages: Slower creation and switching (requires system calls), less portable
Threading Models
Many-to-One Model
Many user threads mapped to one kernel thread. Thread management in user space. If one thread blocks, all block. Cannot run on multiple cores. Example: Old Solaris Green Threads
One-to-One Model
Each user thread maps to one kernel thread. True parallelism, but creating too many threads is expensive (each needs kernel resources). Example: Linux (NPTL), Windows
Many-to-Many Model
M user threads mapped to N kernel threads (M ≥ N). Combines benefits of both — flexibility without excessive kernel threads. Example: Solaris prior to version 9, Windows ThreadFiber
Thread Libraries
POSIX Threads (pthreads) — C/C++
Real-World Analogy
Threads are like workers in a shared office. Each worker (thread) has their own desk (stack) with personal notes (local variables) and their own task list (program counter). But they share the same office space (address space), whiteboard (shared memory), printer (files), and coffee machine (resources). Communication is easy — just talk or look at the whiteboard. But they must coordinate when using shared resources (synchronization), or they will interfere with each other.
Key Takeaways
- A thread is the basic unit of CPU execution; a process can contain multiple threads
- Threads share code, data, heap, and files but have private stacks and registers
- Benefits include responsiveness, resource sharing, economy, and multi-core scalability
- User-level threads are fast but cannot exploit parallelism; kernel threads are slower but fully parallel
- The one-to-one model (Linux, Windows) is most common in modern systems
- Thread creation is 10-100x cheaper than process creation
- Shared memory makes communication easy but introduces synchronization challenges
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Threads.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, process, management, threads
Related Operating Systems Topics