Four cars reach a four-way stop at the same moment. Each driver waits for the car on their right to go first, so nobody moves, forever. Inside an operating system the same thing happens to threads and processes that wait on each other's locks, files or memory. This article covers how to recognise a deadlock and the three ways an OS deals with it, ending with the banker's algorithm.
If you haven't met mutexes and race conditions yet, read Synchronisation first: deadlock is what goes wrong when locking goes wrong.
What a Deadlock Looks Like#
Two threads each need two locks. Thread 1 takes lock A, thread 2 takes lock B, and then each asks for the lock the other already holds:
Nothing crashes and nothing errors. Both threads just stop, holding their locks, and anything else that needs A or B stops behind them.
The Four Conditions#
A deadlock can only happen when all four of these hold at the same time. They are called the Coffman conditions, after the 1971 paper that set them out.
| Condition | Meaning | In the example |
|---|---|---|
| Mutual exclusion | A resource can be held by only one thread at a time | Only one thread can hold lock_a |
| Hold and wait | A thread keeps what it holds while it waits for more | Thread 1 holds A while asking for B |
| No preemption | A resource can't be taken away; only its holder releases it | Nobody can force thread 2 to drop B |
| Circular wait | There is a cycle: T1 waits for T2, which waits for T1 | 1 waits for 2, 2 waits for 1 |
Break any one of the four and deadlock becomes impossible. That idea is the whole of deadlock prevention.
Watch HOLDING as the strategies land. It falls 4 → 3 → 2 and then stops at 1,
because mutual exclusion is the condition none of them touch. Some resources
simply cannot be shared. That is the gap avoidance fills: the Banker's Algorithm
leaves the conditions alone and refuses any grant that would leave the system
unsafe.
Seeing It as a Graph#
A resource allocation graph draws processes as circles and resources as boxes. An arrow from a resource to a process means "held by"; an arrow from a process to a resource means "waiting for".
- If every resource has one instance, a cycle in the graph means a deadlock.
- If a resource has several instances (three printers, say), a cycle means a deadlock is possible, not certain: another holder may finish and free an instance.
For single instances the OS often simplifies this to a wait-for graph, with an arrow from each process to the process it is waiting on. A cycle there is a deadlock.
Three Ways to Handle It#
| Approach | Idea | Cost |
|---|---|---|
| Prevention | Design so one of the four conditions can never hold | Restrictive: less concurrency, wasted resources |
| Avoidance | Check every request and refuse any that could lead to deadlock | Needs each process's maximum demand in advance |
| Detection and recovery | Let deadlocks happen, find them, then break them | Work is lost when a process is killed or rolled back |
A fourth option is to ignore the problem, known as the ostrich algorithm. General-purpose systems such as Linux and Windows mostly do this for application locks: deadlocks are rare, and the checks would slow everyone down. Avoiding them is left to the programmer.
Prevention: Break One Condition#
| Condition to break | How | Drawback |
|---|---|---|
| Mutual exclusion | Make the resource shareable (read-only files, a spooler in front of a printer) | Most resources can't be shared |
| Hold and wait | Ask for everything at once, before starting | Resources sit idle; you may not know what you need |
| No preemption | If a request fails, release what you hold and retry | Only works for state you can save and restore |
| Circular wait | Number every resource and always acquire them in increasing order | Every piece of code must follow the order |
Lock ordering (breaking circular wait) is the one used most in real code. If both transfers take lock_a before lock_b, the cycle can't form:
Avoidance: the Banker's Algorithm#
Avoidance lets all four conditions exist, but refuses any request that could lead somewhere a deadlock is possible. Dijkstra's banker's algorithm does this. The name comes from a bank that never lends out so much cash that it couldn't meet every customer's maximum withdrawal.
Each process declares the most of each resource it will ever need. The OS keeps four tables:
- Available: free instances of each resource type
- Max: the most each process may ask for
- Allocation: what each process holds now
- Need: what each process may still ask for,
Need = Max - Allocation
A state is safe if there is some order in which every process can get its remaining need and finish. When a process finishes it hands back everything it holds, which is what lets the next one finish.
A worked example#
Five processes share three resource types: A has 10 instances, B has 5 and C has 7. Right now 3 A, 3 B and 2 C are free.
| Process | Allocation (A B C) | Max (A B C) | Need (A B C) |
|---|---|---|---|
| P0 | 0 1 0 | 7 5 3 | 7 4 3 |
| P1 | 2 0 0 | 3 2 2 | 1 2 2 |
| P2 | 3 0 2 | 9 0 2 | 6 0 0 |
| P3 | 2 1 1 | 2 2 2 | 0 1 1 |
| P4 | 0 0 2 | 4 3 3 | 4 3 1 |
Is this state safe? Start with Work = Available = (3, 3, 2) and look for a process whose Need fits in Work:
| Step | Runs | Need fits in Work? | Work after it finishes |
|---|---|---|---|
| 1 | P1 | (1,2,2) ≤ (3,3,2) yes | (3,3,2) + (2,0,0) = (5,3,2) |
| 2 | P3 | (0,1,1) ≤ (5,3,2) yes | (5,3,2) + (2,1,1) = (7,4,3) |
| 3 | P0 | (7,4,3) ≤ (7,4,3) yes | (7,4,3) + (0,1,0) = (7,5,3) |
| 4 | P2 | (6,0,0) ≤ (7,5,3) yes | (7,5,3) + (3,0,2) = (10,5,5) |
| 5 | P4 | (4,3,1) ≤ (10,5,5) yes | (10,5,5) + (0,0,2) = (10,5,7) |
Everyone finishes, so the state is safe, with safe sequence P1, P3, P0, P2, P4. Other safe orders exist; one is enough.
A request that is granted. P1 asks for (1, 0, 2). It is within P1's Need and within Available, so the OS pretends to grant it (Available becomes (2, 3, 0)) and runs the safety check again. P1 can still finish first, and the rest follow as before, so the request is granted.
A request that is refused. Now P0 asks for (0, 2, 0). Two B are free, so it could be handed over, but afterwards only (2, 1, 0) would be free. No process's Need fits in that, so the state would be unsafe and P0 is made to wait. Nothing is deadlocked yet: avoidance refuses before any harm is done.
The safety check is O(m × n²) for n processes and m resource types, run on every request. That cost, plus needing every maximum in advance, is why the banker's algorithm appears in textbooks and interviews far more than in real kernels.
An unsafe state is not a deadlock. It only means the OS can no longer guarantee that every process will finish. The banker's algorithm stays safe to be sure.
Detection and Recovery#
The third approach lets processes take what they ask for and checks for deadlock now and then. With single-instance resources it looks for a cycle in the wait-for graph. With several instances it runs a check much like the banker's safety test, using what each process is actually requesting.
Once a deadlock is found, it is broken by:
- Killing a process: all of the deadlocked ones, or one at a time until the cycle is gone
- Preempting a resource: take it from one process and give it to another
- Rolling back: return a process to an earlier checkpoint and restart it from there
Databases do exactly this. PostgreSQL checks for a cycle once a transaction has waited on a lock for deadlock_timeout (one second by default), then aborts one transaction with a "deadlock detected" error. MySQL's InnoDB detects the cycle and rolls back the smaller transaction. Either way the application should catch that error and retry the transaction.
Deadlock, Livelock and Starvation#
| What happens | Threads are | Example | |
|---|---|---|---|
| Deadlock | Each waits for another in a cycle | Blocked, doing nothing | The two transfers above |
| Livelock | Each keeps reacting to the other, so nobody gets anywhere | Running, but making no progress | Two people in a corridor who keep stepping aside the same way |
| Starvation | One thread never gets its turn while others carry on | That one waits, the rest run | A low-priority process that always loses the CPU |
What to Remember#
- A deadlock needs all four Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait
- Prevention breaks one condition; lock ordering, which breaks circular wait, is the common one
- Avoidance (the banker's algorithm) grants a request only if the state afterwards is still safe
- A safe state has an order in which every process can finish; unsafe does not mean deadlocked
- Detection finds a cycle in the wait-for graph and recovers by killing, preempting or rolling back, as databases do
- Linux and Windows mostly ignore application deadlocks and leave prevention to the programmer