Processes vs Threads
A process is a program in execution with its own address space. A thread is the smallest unit of CPU scheduling and lives inside a process, sharing its memory with sibling threads.
Cheat sheet
- ▸Process = code + data + heap + stack + PCB (Process Control Block).
- ▸Threads share code, data, heap and open files; each has its own stack, registers and program counter.
- ▸Process states: New → Ready → Running → Waiting → Terminated.
- ▸Context switch between threads is cheaper than between processes (no address-space switch).
- ▸fork() creates a child process; it returns 0 in the child and the child's PID in the parent.
- ▸Zombie = finished child not yet reaped by parent; Orphan = child whose parent died (adopted by init).
Process vs Thread
| Aspect | Process | Thread |
|---|---|---|
| Memory | Separate address space | Shared with other threads |
| Creation cost | Heavy | Light |
| Communication | IPC (pipes, sockets, shared mem) | Shared variables |
| Crash impact | Isolated | Can crash whole process |
| Context switch | Slower | Faster |
Visual
New ──admit──▶ Ready ──dispatch──▶ Running ──exit──▶ Terminated
▲ ◀──interrupt───┘ │
└──── I/O done ── Waiting ◀─ I/O waitTop 10 campus interview questions
Quick quiz
1. Which is NOT shared between threads of the same process?
2. fork() returns what value in the child?
3. A process whose parent has not called wait() after it exits is a:
4. Which transition happens when a running process requests I/O?
5. Which is cheapest?