Deadlock is a state where a set of processes are each waiting for a resource held by another process in the set, so none of them can ever proceed. Example: P1 holds the printer and waits for the scanner; P2 holds the scanner and waits for the printer.
Necessary (Coffman) conditions — all four must hold at once:
- Mutual exclusion: a resource can be held by only one process at a time.
- Hold and wait: a process holds at least one resource while waiting for others.
- No preemption: a resource cannot be taken away; it is released only voluntarily.
- Circular wait: a cycle P1 → P2 → … → Pn → P1, each waiting for the next one’s resource.
Methods of handling
- Prevention: break one condition by design — e.g. request all resources at once (no hold-and-wait), allow preemption, or number resources and always request them in increasing order (no circular wait).
- Avoidance: grant a request only if the system stays in a safe state, using advance knowledge of maximum needs — the Banker’s algorithm.
- Detection and recovery: let deadlocks happen, detect cycles in a wait-for / resource-allocation graph periodically, then recover by terminating processes or preempting resources (rolling a victim back).
- Ignoring (Ostrich approach): assume deadlocks are rare and reboot if one occurs; used by most general-purpose OSs such as Windows and Linux.
— rhs