Skills101

Operating Systems · Lesson 5 of 9 · Updated ·

Deadlocks — The 4 Conditions, Prevention, Avoidance and Banker's Algorithm

A deadlock is when a set of processes are all waiting for each other, so none can ever continue.

P1 holds R1 and wants R2; P2 holds R2 and wants R1 — a circular wait that never ends

Real-life version: two cars meet on a one-lane bridge from opposite ends; neither will reverse.

1. The four necessary conditions (Coffman)

A deadlock can happen only if all four hold at the same time:

  1. Mutual exclusion — a resource can be used by only one process at a time.
  2. Hold and wait — a process holds one resource while waiting for another.
  3. No preemption — resources can't be taken away forcibly.
  4. Circular wait — P1 waits for P2, P2 waits for … Pn, Pn waits for P1.

Break any one and deadlock is impossible.

2. Handling deadlocks: four strategies

StrategyIdea
Preventiondesign the system so one condition can never hold
Avoidancecheck each request; grant only if the system stays safe (Banker's)
Detection & recoverylet deadlocks happen, detect cycles, then recover
Ignore it"ostrich algorithm" — reboot if it happens (most desktop OSes)

Prevention examples

  • Break hold and wait: request all resources at once.
  • Break no preemption: if a request fails, release what you hold.
  • Break circular wait: number resources and always request them in increasing order (the most practical fix — also used with database locks).

3. Avoidance: the Banker's algorithm

The system is safe if there's some order (a safe sequence) in which every process can get its maximum need and finish.

Need = Max − Allocation. Available resources (A, B, C) = (3, 3, 2).

ProcessAllocation (A B C)Max (A B C)Need (A B C)
P00 1 07 5 37 4 3
P12 0 03 2 21 2 2
P23 0 29 0 26 0 0
P32 1 12 2 20 1 1
P40 0 24 3 34 3 1

Find a safe sequence: repeatedly pick a process whose Need ≤ Available; it finishes and returns its allocation.

StepPickNeed ≤ Available?Available after it finishes
1P1(1,2,2) ≤ (3,3,2) ✓(3,3,2) + (2,0,0) = (5,3,2)
2P3(0,1,1) ≤ (5,3,2) ✓+ (2,1,1) = (7,4,3)
3P4(4,3,1) ≤ (7,4,3) ✓+ (0,0,2) = (7,4,5)
4P0(7,4,3) ≤ (7,4,5) ✓+ (0,1,0) = (7,5,5)
5P2(6,0,0) ≤ (7,5,5) ✓+ (3,0,2) = (10,5,7)

Safe sequence: ⟨P1, P3, P4, P0, P2⟩ → the system is in a safe state. (Other safe sequences may exist.)

When a new request arrives, the OS pretends to grant it, runs this check, and grants it only if the state stays safe.

4. Detection and recovery

  • Draw a wait-for graph; a cycle means deadlock (for single-instance resources).
  • Recover by terminating a process (or all deadlocked ones) or preempting resources and rolling back.

5. Deadlock vs starvation

DeadlockStarvation
Whatprocesses wait for each other forevera process waits indefinitely while others progress
System progressstuck for those processesothers keep running
Fixprevent/avoid/detectaging, fair scheduling

Common mistakes

  • Saying one condition is enough for deadlock — all four are needed.
  • Thinking an unsafe state is always a deadlock. Unsafe means a deadlock may happen.
  • Forgetting to add back the finished process's allocation in the Banker's table.

Quick recap

  • Four conditions: mutual exclusion, hold and wait, no preemption, circular wait.
  • Prevention breaks a condition; avoidance (Banker's) keeps the state safe.
  • Need = Max − Allocation; find a safe sequence by finishing processes one by one.
  • Detection: cycles in the wait-for graph; recover by killing or preempting.

Practice what you learned

📝 Quiz: Deadlocks5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →