Step 06

Deadlocks

A deadlock is a permanent traffic jam inside the operating system: every process holds something the next one needs. Build one yourself, then learn how the OS avoids it.

What is a deadlock?

A set of processes is deadlocked when every process in the set is waiting for an event that only another process in the same set can cause. Since nobody can proceed, they wait forever.

Resource — anything a process needs: printer, file, memory, semaphore
Safe state — a state in which some order of execution finishes all processes
Starvation — indefinite waiting — a process still could run, but never gets a turn
Preemption — taking a resource away from a process by force

Real-world analogy: The narrow bridge

Two cars meet in the middle of a one-lane bridge from opposite ends. Each needs the space the other occupies, and neither will reverse. Nothing moves until someone is forced to back out — that is deadlock recovery by preemption.
A resource-allocation cycle: Process 1 holds a printer and waits for a disk, while Process 2 holds the disk and waits for the printer, causing deadlock.
Deadlock occurs when each process holds a resource the other process needs.
The four Coffman conditions (all four must hold)
condition

Mutual exclusion

At least one resource cannot be shared. Only one process can use it at a time, so other processes must wait.

condition

Hold and wait

A process keeps one resource while asking for another resource that is currently being used by someone else.

condition

No preemption

The OS cannot forcibly take a resource away from a process. A resource is released only when the process voluntarily gives it up.

condition

Circular wait

A chain of processes forms: P1 waits for P2, P2 waits for P3, and eventually Pn waits for P1. Each process is waiting on the next one in the loop.

Break any one condition and a deadlock becomes impossible — that is exactly how deadlock prevention works.

Experiment: Build a Resource Allocation Graph and detect the cycle

P1

R1
R2

P2

R1
R2

Resource Allocation Graph edges

No edges yet — assign some resources above.

No cycle detected — the system can still make progress. Try: P1 holds R1 and requests R2, while P2 holds R2 and requests R1.
Experiment: Banker's Algorithm — safety sequence calculator

Edit Allocation, Max and Available. The algorithm recomputes Need, finds a safe sequence and shows every step. Try changing Available to [1, 0, 0] to see an unsafe state.

Available (A, B, C):
ProcessAllocationMaxNeed = Max − Allocation
P0[7, 4, 3]
P1[1, 2, 2]
P2[6, 0, 0]
P3[0, 1, 1]
P4[4, 3, 1]
Safe state. Safe sequence: P1 → P3 → P4 → P0 → P2
  1. Work = Available = [3, 3, 2]
  2. P1: Need = [1, 2, 2] ≤ Work = [3, 3, 2] → run it. Work becomes [5, 3, 2] after it releases its allocation.
  3. P3: Need = [0, 1, 1] ≤ Work = [5, 3, 2] → run it. Work becomes [7, 4, 3] after it releases its allocation.
  4. P4: Need = [4, 3, 1] ≤ Work = [7, 4, 3] → run it. Work becomes [7, 4, 5] after it releases its allocation.
  5. P0: Need = [7, 4, 3] ≤ Work = [7, 4, 5] → run it. Work becomes [7, 5, 5] after it releases its allocation.
  6. P2: Need = [6, 0, 0] ≤ Work = [7, 5, 5] → run it. Work becomes [10, 5, 7] after it releases its allocation.
Handling deadlocks: four strategies
  • Prevention — design the system so one Coffman condition can never hold (e.g. request all resources at once, or order resources numerically).
  • Avoidance — grant a request only if the resulting state is safe (Banker's Algorithm).
  • Detection & recovery — allow deadlocks, detect cycles periodically, then kill a process or preempt a resource.
  • Ignore it — the “ostrich algorithm”, used by most general-purpose operating systems because deadlocks are rare.
Quick quiz

1. How many Coffman conditions must hold simultaneously for a deadlock?

2. The Banker's Algorithm is a technique for deadlock:

3. In a resource allocation graph, an edge from a resource to a process means:

Practice problem

A system has 12 tape drives. P0 holds 5 and may need 10 in total, P1 holds 2 and may need 4, P2 holds 2 and may need 9. Is the state safe?