1
Deadlock ★★★★★
Operating System
A deadlock is a state where a set of processes wait forever, each holding a resource the other needs.
Resource allocation graph with a cycle
Four necessary conditions (Coffman) — all must hold:
- Mutual exclusion — a resource is used by one process at a time.
- Hold and wait — a process holds one resource and waits for another.
- No preemption — a resource cannot be taken away forcibly.
- Circular wait — P1 waits for P2, P2 waits for P1 (a cycle).
Handling: Prevention (break one condition), Avoidance (Banker's algorithm, only grant requests that keep the system in a safe state), Detection and recovery (kill a process / preempt resources), or Ignore it (Ostrich approach, used by most desktop OSes).
Q & A
- Deadlock vs starvation?
Deadlock: processes wait for each other forever and none progresses. Starvation: one process waits a very long time because others keep getting priority; the system still progresses.