4.1Deadlock conditions and handling
The idea
Deadlock: a set of processes each waiting for a resource held by another in the set, so none can proceed. It needs four conditions at once.
Think of it like this: Four cars at a crossroads, each blocking the next, nobody can reverse.
Why it exists
To fight it you must know the conditions, because each strategy works by breaking one of them.
How it works
- Mutual exclusion: the resource is not shareable.
- Hold and wait: a process holds some resources while waiting for more.
- No preemption: resources cannot be taken away.
- Circular wait: a cycle of processes each waiting for the next.
Worked example: Two locks A and B
T1 locks A then B, T2 locks B then A. If each gets its first lock, both wait forever. Fix: make both take A before B, which removes circular wait.
Now play with it
- →Run the "opposite lock order" scenario: T1 P(A), T2 P(B).
- →Read the table: which condition does an ordering rule break?
| Coffman condition | Prevention idea | Cost |
|---|---|---|
| Mutual exclusion | make resources shareable (read-only files) | not possible for printers, tape drives |
| Hold and wait | request everything at the start, or release before asking again | low utilisation, starvation |
| No preemption | take resources away from a waiting process | only for state that can be saved (CPU, memory) |
| Circular wait | number the resources and always request in increasing order | the usual practical method |
All four must hold together for a deadlock. Prevention breaks one of them, avoidance steers away from unsafe states, detection lets deadlock happen and repairs it.
Run T1 P(A), then T2 P(B): each now waits for the other. Fix: both take A before B. P(s) waits until s > 0 then decrements; V(s) increments.
Check yourself
Requiring processes to request all resources at once breaks
Exam notes (the dense version)
Deadlock needs all four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait. Breaking any one prevents it. Strategies: prevention (negate a condition: request all resources at once, allow preemption, impose a global ordering on resources), avoidance (needs advance information: Banker's algorithm), detection and recovery, or ignore it (the ostrich approach used by most general-purpose systems).
Previous year questions: Deadlock (9)
PYQs are grouped by topic, so this is the full set for Deadlock; try the ones that match this subtopic. Open all 9 on the topic page.