Interview contents
Processes vs Threads
In operating systems, processes and threads are the basic units of execution.
Understanding their differences is essential for concurrency, parallelism, and system design interviews.
1. What is a Process?
- A process is an independent program in execution.
- It has its own memory space, registers, stack, heap, and code segment.
- Examples:
- Opening Chrome → one process.
- Opening VS Code → another process.
Key Characteristics
- Heavyweight: context switching is expensive.
- Isolated: one process cannot access another’s memory directly.
- Communicates via Inter-Process Communication (IPC) → pipes, sockets, shared memory.
2. What is a Thread?
- A thread is the smallest unit of execution within a process.
- A process can have multiple threads, sharing the same memory space.
- Example:
- Chrome process → one thread renders UI, another fetches network data.
Key Characteristics
- Lightweight: faster context switching.
- Shares memory: threads of the same process share code, data, and heap.
- Needs synchronization (locks, semaphores) to avoid race conditions.
3. Memory Model Comparison
| Aspect | Process | Thread |
|---|---|---|
| Memory Space | Separate per process | Shared within process |
| Communication | IPC needed | Shared memory (direct access) |
| Context Switch | Expensive | Cheaper |
| Failure | One process crash isolated | One thread crash may affect all |
4. Use Cases
-
Processes:
- Isolation and security (Chrome tabs, database servers).
- Long-running independent programs.
-
Threads:
- Parallel tasks (multi-threaded servers).
- Real-time responsiveness (UI + background tasks).
5. Processes vs Threads in Interviews
- Be ready to explain:
- Why web servers use multi-threading (to handle many requests).
- Why databases use multi-process architecture (for isolation).
- Common pitfalls:
- Threads → race conditions.
- Processes → heavy context switching.
6. Analogy
- Process = A house (with its own kitchen, bathroom, resources).
- Thread = Roommates in the same house (sharing resources, but needing rules to avoid conflicts).