A thread is the smallest unit of CPU utilization. A process provides the environment (address space, file descriptors, etc.), and threads execute within that environment sharing the same resources.
Process vs Thread
| Aspect | Process | Thread |
|---|---|---|
| Address space | Separate | Shared within process |
| Creation time | Slow | Fast (10-100x faster) |
| Context switch | Expensive (TLB flush, address space switch) | Cheap (registers + stack only) |
| IPC | Requires kernel mediation (pipes, shared memory) | Reads/writes same memory directly |
| Protection | Fully isolated from other processes | No isolation between threads (can corrupt each other) |
User-Level vs Kernel-Level Threads
| User-Level | Kernel-Level | |
|---|---|---|
| Managed by | Thread library at user level | OS kernel |
| Context switch | No system call — very fast | System call — slower |
| Blocking | One blocking thread blocks all threads in process | One thread can block independently |
| Parallelism | No — only one thread runs at a time | Yes — multiple threads across CPU cores |
| Example | Green threads (old Java) | POSIX threads (pthreads on Linux) |
Modern Model: N:M (Hybrid)
Many user-level threads mapped to fewer kernel-level threads. Best of both worlds — fast creation + true parallelism.
Inter-Process Communication (IPC)
| Mechanism | Speed | Direction | Use case |
|---|---|---|---|
| Shared memory | Fastest (no copy) | Bidirectional | Producer-consumer, large data |
| Message passing | Moderate (kernel copy) | Any | Distributed systems, small messages |
| Pipes | Moderate | Unidirectional | Shell pipelines (ls | grep) |
| Sockets | Slow (network stack) | Any | Network communication |
| Signals | Fast | Unidirectional | Notifications, interrupts |
Shared Memory
Two or more processes map the same physical memory region into their address spaces. No kernel involvement after setup — just reads and writes. Must use synchronization (mutex, semaphore) to prevent race conditions.
Message Passing (Queues)
Processes send and receive messages through a queue managed by the kernel. The sender writes a message, the receiver reads it. No shared memory needed — works across network nodes.
Pipes
A unidirectional byte stream connecting stdout of one process to stdin of another. pipe() creates two file descriptors: one for reading, one for writing. Anonymous pipes work between related processes; named pipes (FIFOs) work between any processes.
Q: Why are threads called “lightweight processes”?
A: Threads share the same address space (code, data, heap) as their parent. Creating a thread doesn’t require allocating a new virtual address space or setting up new page tables. Context switching between threads is much faster than between processes.
Q: What is the difference between user-level and kernel-level threads?
A: User-level threads are managed by a library without kernel awareness — fast but if one blocks, all block. Kernel-level threads are managed by the OS — slower but independent blocking. Modern systems (Linux, Windows) use kernel-level threads.
Q: What is the fastest IPC mechanism?
A: Shared memory — data is not copied between processes. Once mapped, both processes read/write the same physical RAM directly. No kernel involvement during data transfer. The trade-off: you need synchronization (mutex, semaphore) to prevent race conditions.
Q: What is a pipe in Linux?
A: A unidirectional communication channel. pipe() returns two file descriptors — write to one end, read from the other. Used in shell pipelines: ls | grep .txt sends ls output directly to grep input.
Q: What are the multithreading models?
A: 1:1 (each user thread = one kernel thread — Linux, Windows), N:1 (many user threads to one kernel thread — obsolete), N:M (many user threads to many kernel threads — most flexible).
Premium Content
Unlock Threads, Context Switching & IPC and all premium lessons with a subscription.
From ₹199.99/year — See plans