What Is the Difference Between a Process and a Thread?
Question: What is the difference between a process and a thread?
Core Answer
A process is an independent program in execution with its own memory space. A thread is a lightweight unit of execution that shares its parent process's memory space.
The Key Difference: Memory
| Aspect | Process | Thread |
|---|---|---|
| Memory space | Separate address space per process | Shares the process's address space |
| Isolation | Hardware-enforced by the MMU | None — threads share all memory |
| Protection | One process crashing cannot affect another | A bug in one thread can crash all threads |
Because threads share memory, they communicate by reading and writing shared variables. This is fast but requires synchronization (locks, mutexes, semaphores) to prevent race conditions. Processes communicate via IPC (inter-process communication) — pipes, message queues, shared memory — which is slower but safer.
Creation Cost
Process creation is expensive. The kernel must:
- Allocate a Process Control Block (PCB)
- Create a new page table (or set up copy-on-write)
- Duplicate file descriptors
- Initialize the CPU register state
Thread creation is cheaper because the kernel reuses the existing memory space. It only needs to:
- Allocate a thread stack (~8MB virtual, less physical)
- Allocate a Thread Control Block (TCB)
- Set up initial register state
This is why web servers often use a hybrid model: a process per CPU core (for isolation) with threads within each process (for concurrency).
Failure Isolation
If a process crashes (e.g., segmentation fault), only that process dies. Other processes are unaffected. If a thread crashes, the entire process — and all its threads — die.