Deadlock
Also known as: Deadly embrace
Situation in which two or more processes are each waiting for another to release a resource, so that none of them can ever proceed.
A deadlock is a standstill in a concurrent system. It happens when each member of a set of processes or threads holds a resource and waits for another resource held by a different member of the set, forming a cycle with no way out. A simple case involves two threads and two locks: the first thread locks A and then tries to lock B, while the second locks B and then tries to lock A. Each waits forever for the other. The program does not crash; it simply stops making progress.
In a 1971 paper, Edward Coffman and colleagues identified four conditions that must all hold for a deadlock to occur: mutual exclusion, where resources cannot be shared; hold and wait, where a process keeps one resource while requesting another; no preemption, where resources cannot be forcibly taken away; and circular wait. Breaking any one condition prevents deadlock. Edsger Dijkstra's banker's algorithm avoids it by granting requests only when the system remains in a safe state, and the dining philosophers problem, which Dijkstra devised and Tony Hoare later popularized, became the standard teaching example.
In everyday programming the most common prevention technique is to acquire locks in a consistent global order, which rules out circular waits, along with using timeouts and holding locks for as short a time as possible. Databases detect deadlocks between transactions automatically and abort one of them so the others can continue. Related failures include livelock, where processes keep reacting to each other without progressing, and starvation, where one process never gets its turn. Deadlocks are a central hazard of concurrency, alongside the race conditions that locks are meant to prevent.
Key points
- A deadlock forms when processes wait on each other in a cycle, so that none can continue.
- Coffman and colleagues showed in 1971 that four conditions must hold simultaneously for deadlock to occur.
- Acquiring locks in a consistent order is a simple and widely used way to prevent circular waiting.
- Database systems detect deadlocks between transactions and roll one back so the rest can proceed.
Related entries
Concurrency
Ability of a program or system to make progress on several tasks during overlapping time periods, whether or not they literally run simultaneously.
Race Condition
Flaw in which a system's behavior depends on the uncontrolled timing or ordering of events, such as two threads updating the same data at once.
Edsger Dijkstra
Dutch computer scientist known for his shortest path algorithm, structured programming and foundational work on concurrency and program correctness.
Database
Organized collection of structured data stored electronically and managed by software that lets users store, query and update it reliably.
ACID
Set of four properties, atomicity, consistency, isolation and durability, that guarantee database transactions are processed reliably.
Operating System
System software that manages a computer's hardware resources and provides common services and interfaces for the programs that run on it.
Parallel Computing
Form of computation in which many calculations run simultaneously on multiple processors or cores to solve problems faster or at larger scale.