← 🚧 Deadlock
Subtopic 1 of 4

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

  1. Mutual exclusion: the resource is not shareable.
  2. Hold and wait: a process holds some resources while waiting for more.
  3. No preemption: resources cannot be taken away.
  4. 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 conditionPrevention ideaCost
Mutual exclusionmake resources shareable (read-only files)not possible for printers, tape drives
Hold and waitrequest everything at the start, or release before asking againlow utilisation, starvation
No preemptiontake resources away from a waiting processonly for state that can be saved (CPU, memory)
Circular waitnumber the resources and always request in increasing orderthe 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.

T1
P(A)
P(B)
in
out
V(B)
V(A)
T2
P(B)
P(A)
in
out
V(A)
V(B)
semaphore A1
semaphore B1
Threads in critical section0
Statusrunning
No steps yet.

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

Remember: Four conditions all needed. Ordering breaks circular wait; all-at-once breaks hold-and-wait.
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.