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 proceedReal example: two threads lock two mutexes in opposite order.
// Thread A // Thread B
lock(m1); lock(m2);
lock(m2); // waits lock(m1); // waits -> deadlockCoffman Conditions
A deadlock can occur only if all four conditions hold at the same time:
- Mutual exclusion: at least one resource is non-shareable.
- Hold and wait: a process holds at least one resource while waiting for another.
- No preemption: resources cannot be forcibly taken; they are released voluntarily.
- Circular wait: a cycle
P1 -> P2 -> ... -> Pn -> P1where 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
- Prevention: design so one Coffman condition can never hold.
- Avoidance: decide at runtime whether granting a request could lead to deadlock.
- Detection and recovery: let deadlocks happen, find them, fix them.
- 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
| Condition | How to break it | Downside |
|---|---|---|
| Mutual exclusion | Make resources shareable (read-only files, spooling printers) | Not possible for most resources |
| Hold and wait | Request all resources at once, or release everything before requesting more | Low utilization, starvation |
| No preemption | If a request fails, release held resources; or preempt from others | Only works for resources whose state can be saved (CPU, memory) |
| Circular wait | Impose a global ordering on resources; always acquire in increasing order | Must 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 typeMax[n][m]: maximum demand of each processAllocation[n][m]: currently allocatedNeed[n][m] = Max - Allocation
Safety algorithm:
Work = Available,Finish[i] = falsefor all.- Find
iwithFinish[i] == falseandNeed[i] <= Work. - If found:
Work += Allocation[i],Finish[i] = true, go to step 2. - If all
Finish[i]are true, the state is safe.
Worked example
Resources A, B, C with total (10, 5, 7).
| Process | Allocation | Max | Need |
|---|---|---|---|
| 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 |
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
Requestinstead ofNeed.
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).
| Deadlock | Livelock | Starvation | |
|---|---|---|---|
| Processes state | Blocked | Running, busy | Ready, never scheduled |
| Progress | None | None | Others 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).