Skip to content
Chethan Yadav_
Operating Systems

OS/Lesson 6 of 9/6 min read

Deadlocks

Deadlock definition, the four Coffman conditions, resource allocation graphs, prevention, avoidance with the Banker's algorithm, detection, recovery, and livelock.


On this page

What is a Deadlock

A deadlock is a situation where a set of processes are blocked forever because each one holds a resource and waits for a resource held by another process in the set.

P1 holds R1, wants R2
P2 holds R2, wants R1      -> neither can proceed

Real example: two threads lock two mutexes in opposite order.

// Thread A            // Thread B
lock(m1);              lock(m2);
lock(m2);  // waits    lock(m1);  // waits  -> deadlock

Coffman Conditions

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

  1. Mutual exclusion: at least one resource is non-shareable.
  2. Hold and wait: a process holds at least one resource while waiting for another.
  3. No preemption: resources cannot be forcibly taken; they are released voluntarily.
  4. Circular wait: a cycle P1 -> P2 -> ... -> Pn -> P1 where each waits for the next.

Breaking any one of them prevents deadlock.

Resource Allocation Graph

A Resource Allocation Graph (RAG) has process nodes (circles), resource nodes (squares, with dots for instances), request edges P -> R, and assignment edges R -> P.

  • No cycle: no deadlock.
  • Cycle, every resource has one instance: deadlock for sure.
  • Cycle, some resources have multiple instances: deadlock is possible, not certain.

For single-instance resources a simpler wait-for graph (only processes, edge Pi -> Pj if Pi waits for Pj) is used; a cycle means deadlock.

Handling deadlocks: four strategies

  1. Prevention: design so one Coffman condition can never hold.
  2. Avoidance: decide at runtime whether granting a request could lead to deadlock.
  3. Detection and recovery: let deadlocks happen, find them, fix them.
  4. Ignore it (Ostrich algorithm): assume deadlocks are rare and reboot if needed. Used by most general purpose OSes, including Linux and Windows, for user resources.

Deadlock Prevention

ConditionHow to break itDownside
Mutual exclusionMake resources shareable (read-only files, spooling printers)Not possible for most resources
Hold and waitRequest all resources at once, or release everything before requesting moreLow utilization, starvation
No preemptionIf a request fails, release held resources; or preempt from othersOnly works for resources whose state can be saved (CPU, memory)
Circular waitImpose a global ordering on resources; always acquire in increasing orderMust know and follow the order

Lock ordering (breaking circular wait) is the most practical technique in real code.

Deadlock Avoidance

The OS needs advance knowledge of each process's maximum resource needs and only grants a request if the system stays in a safe state.

Safe State

A state is safe if there exists some order (a safe sequence) in which every process can get its maximum need and finish. Safe means no deadlock. Unsafe does not guarantee deadlock but makes it possible.

Banker's Algorithm

Dijkstra's Banker's Algorithm works for multiple instances of each resource type.

Data structures for n processes and m resource types:

  • Available[m]: free instances of each type
  • Max[n][m]: maximum demand of each process
  • Allocation[n][m]: currently allocated
  • Need[n][m] = Max - Allocation

Safety algorithm:

  1. Work = Available, Finish[i] = false for all.
  2. Find i with Finish[i] == false and Need[i] <= Work.
  3. If found: Work += Allocation[i], Finish[i] = true, go to step 2.
  4. If all Finish[i] are true, the state is safe.

Worked example

Resources A, B, C with total (10, 5, 7).

ProcessAllocationMaxNeed
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

Available = (10,5,7) - (7,2,5) = (3, 3, 2)

  • P1 Need (1,2,2) <= (3,3,2): run, Work = (5,3,2)
  • P3 Need (0,1,1) <= (5,3,2): run, Work = (7,4,3)
  • P4 Need (4,3,1) <= (7,4,3): run, Work = (7,4,5)
  • P0 Need (7,4,3) <= (7,4,5): run, Work = (7,5,5)
  • P2 Need (6,0,0) <= (7,5,5): run, Work = (10,5,7)

Safe sequence: P1, P3, P4, P0, P2. The state is safe.

Resource request algorithm: when Pi requests Request[i], check Request <= Need and Request <= Available, pretend to allocate, run the safety algorithm, and grant only if still safe.

Time complexity of the safety check: O(m * n^2).

Deadlock Detection

Run periodically (or when CPU utilization drops):

  • Single instance resources: look for a cycle in the wait-for graph, O(n^2).
  • Multiple instances: a Banker-like algorithm using current Request instead of Need.

Deadlock recovery

  • Process termination: abort all deadlocked processes, or abort one at a time until the cycle breaks (pick the victim by priority, work done, resources held).
  • Resource preemption: take resources from a victim and roll it back to a safe checkpoint. Avoid always picking the same victim (starvation).

Databases detect deadlocks with a wait-for graph and abort one transaction (see concurrency control).

Livelock

In a livelock processes are not blocked; they keep changing state in response to each other but make no progress. Example: two people in a corridor both stepping aside in the same direction repeatedly. Fix: add randomness (random backoff, as Ethernet does after collisions).

DeadlockLivelockStarvation
Processes stateBlockedRunning, busyReady, never scheduled
ProgressNoneNoneOthers progress

Quick revision

  • Four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait.
  • RAG cycle with single instances means deadlock.
  • Banker's algorithm keeps the system in a safe state; Need = Max - Allocation.
  • Lock ordering is the practical fix in code.
  • Most OSes just ignore deadlocks (Ostrich).