Threads: Lightweight Processes
A thread is the smallest unit of CPU execution within a process.
Process vs Thread
| Aspect | Process | Thread |
|---|---|---|
| Memory | Separate address space | Shared address space |
| Communication | IPC (pipes, sockets) | Shared memory (direct) |
| Creation time | Slow (heavy) | Fast (lightweight) |
| Context switch | Expensive | Cheaper |
| Isolation | Strong | Weak (crash affects all) |
Thread Components
Each thread has its own:
- Thread ID
- Program counter
- Register set
- Stack
Threads share:
- Code section
- Data section (global variables)
- Heap
- Open files and signals
Process Memory Layout with Threads: