← back to Operating Systems

Deadlock

Wikipedia · wpDeadlock · CC BY-SA 4.0

Deadlock occurs when a set of processes each hold a resource and wait for another resource held by a different process in the set. Nobody can proceed. The system is stuck.

Four necessary conditions

Deadlock requires all four of these simultaneously. Break any one, and deadlock cannot occur.

  1. Mutual exclusion: at least one resource is non-shareable
  2. Hold and wait: a process holds one resource while waiting for another
  3. No preemption: resources cannot be forcibly taken away
  4. Circular wait: a cycle of processes, each waiting for the next
P1 P2 P3 R1 R2 R3 solid = holds, dashed = waits-for

Resource allocation graph

A directed graph where processes and resources are nodes. An edge from a process to a resource means "waiting for." An edge from a resource to a process means "held by." A cycle in this graph indicates deadlock (for single-instance resources).

Scheme

Prevention

Break one of the four conditions. Impose a total ordering on resources (break circular wait). Require processes to request all resources at once (break hold-and-wait). Allow preemption of resources.

Avoidance: Banker's algorithm

wpDijkstra's wpBanker's algorithm checks whether granting a request leaves the system in a safe state: one where there exists a sequence in which all processes can finish. If not, the request is denied. The cost is knowing maximum resource needs in advance.

Scheme
Neighbors
  • 🔢 Graphs — deadlock detection is cycle detection in a directed graph
  • ♟ Game Theory Ch.12 — prisoner's dilemma: deadlock is the game-theoretic outcome where rational self-interest (holding resources) leads to mutual destruction
  • ⚙ Algorithms Ch.6 — graph cycle detection: deadlock detection is cycle detection in the resource allocation digraph

Foundations (Wikipedia)