Chapter 7: Deadlocks
Introduction
Learning Outcomes
Define deadlock; distinguish preemptable from nonpreemptable resources and describe the request–use–release cycle.
Illustrate the Coffman conditions (mutual exclusion, hold and wait, no preemption, circular wait) with an example.
Draw resource-allocation and wait-for graphs; state when a cycle means deadlock.
Run the matrix-based detection algorithm by hand; compare recovery by preemption, rollback, and termination.
Distinguish safe from unsafe states; use the banker's algorithm to decide whether to grant a request.
Recognize lock ordering, two-phase locking, livelock, and starvation; find a deadlock in Java with jstack.
Introduction
What Is a Deadlock?
- Definition (Tanenbaum): a set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause.
- The event is usually the release of a resource; since none of them runs, none can release anything.
- Example: P1 holds R1 and waits for R2; P2 holds R2 and waits for R1.
- Everyday examples: four cars at an intersection block each other (gridlock); two trains facing each other on a single track.
- Deadlocked processes use no CPU (they are blocked), but the resources they hold are unusable forever.
It isn't only an OS issue: database transactions, multithreaded applications, and distributed systems deadlock too.
Introduction
Resources: Preemptable and Nonpreemptable
Can be taken from its owner without harm. Examples: the CPU (context is saved), main memory (a page is written to disk). Deadlock can be resolved by reallocating it.
Taking it away breaks the computation. Examples: a Blu-ray being burned or a print job, a mutex, a database record lock. Deadlocks involve this kind of resource.
| Resource | Instances | Type | Note |
|---|---|---|---|
| CPU core | many | preemptable | handed over by the timer interrupt |
| Memory page | many | preemptable | reclaimed by swapping/paging |
| Mutex, semaphore, file lock | 1 | nonpreemptable | only the owner releases it |
| Database row lock | 1 | nonpreemptable* | *released if the transaction aborts |
Each resource type Ri may have Wi identical instances; instances are identical if a request can be satisfied by any instance of that type.
Introduction
The Resource-Use Cycle
- Request: granted if the resource is free; otherwise the process blocks (or gets an error code and retries later).
- Use: the process operates on the resource.
- Release: the resource becomes free and one of the waiters is woken.
- The OS keeps a system table recording which resource is allocated to whom and who is waiting.
| Request | Release |
|---|---|
open() | close() |
malloc() / mmap() | free() / munmap() |
pthread_mutex_lock() | pthread_mutex_unlock() |
sem_wait() (down) | sem_post() (up) |
Introduction
Same Order and Reverse Order
Each resource is guarded by a binary semaphore: down before access, up when done. With one process or one resource there is no problem.
Deadlock-free: both take 1, then 2
void process_A(void) { down(&resource_1); down(&resource_2); use_both_resources(); up(&resource_2); up(&resource_1); } void process_B(void) { down(&resource_1); down(&resource_2); use_both_resources(); up(&resource_2); up(&resource_1); }
Can deadlock: B takes 2, then 1
void process_A(void) { down(&resource_1); down(&resource_2); use_both_resources(); up(&resource_2); up(&resource_1); } void process_B(void) { down(&resource_2); /* reverse order */ down(&resource_1); use_both_resources(); up(&resource_1); up(&resource_2); }
Introduction · Step through the code
Acquiring in Reverse Order, Step by Step
semaphore r1 = 1, r2 = 1; void A(void) { down(&r1); down(&r2); use_both(); up(&r2); up(&r1); } void B(void) { down(&r2); down(&r1); use_both(); up(&r1); up(&r2); }
Two binary semaphores start at 1: both resources are free.
A calls down(&r1) and gets r1 (edge r1 → A).
The scheduler switches to B; B gets r2 with down(&r2).
B requests r1; r1 is held by A, so B blocks (dashed request edge).
A requests r2; r2 is held by B, so A blocks too.
The graph now has a cycle. Neither can reach its up lines: deadlock.
Deadlock doesn't occur on every run: if A finishes both downs before B starts, nothing goes wrong. That's why it rarely shows up in testing and surfaces in production.
Deadlock Conditions
The Four Conditions for Deadlock (Coffman, 1971)
Each resource is either assigned to exactly one process or free; it cannot be shared at the same time.
A process holding at least one resource requests new resources held by others, and waits.
A granted resource cannot be taken away by force; only the holding process can release it voluntarily.
T0 waits for a resource held by T1, T1 for one held by T2, …, and Tn for one held by T0.
Deadlock Conditions
The Four Conditions Together
- The intersection's four squares = four nonpreemptable resources; cars = processes.
- Each step highlights one condition.
- Breaking any condition is a solution:
- a traffic officer backs a car out (preemption),
- a "don't enter until the junction is clear" rule (no hold and wait),
- a traffic light (ordering, no cycle).
Mutual exclusion: only one car can be in an intersection square at a time.
Hold and wait: each car holds its square while waiting for the one ahead (orange arrows).
No preemption: there are queues behind; no car can back up to free its square.
Circular wait: A → B → C → D → A. All four conditions together: gridlock.
Modeling
Resource-Allocation Graph
- Holt (1972): two kinds of nodes; processes are circles, resources are squares. A multi-instance resource has one dot inside its square per instance.
- Assignment edge R → P: R is allocated to P. Request edge P → R: P is waiting for R (dashed orange in these slides).
- When a request is granted, the request edge becomes an assignment edge; on release it is deleted.
Tanenbaum MOS Figure 6-3. Silberschatz draws the same model as a "resource-allocation graph" with T (thread) nodes.
Modeling
How Deadlock Happens
A B C request R request S request T request S request T request R release R release S release T release S release T release R
If the OS grants requests in arrival order: R, S, and T are granted, then each process requests the next one's resource.
Initially all three resources are free. Round-robin scheduling is assumed.
1. A requests R and gets it.
2. B requests S and gets it.
3. C requests T and gets it.
4. A requests S: S is held by B → A blocks.
5. B requests T: T is held by C → B blocks.
6. C requests R: R is held by A → C blocks. The cycle closes: deadlock.
Tanenbaum MOS Figure 6-4 (a)–(j). The scheduler could have picked another order; deadlock depends on the interleaving.
Modeling
How Could Deadlock Be Avoided?
A B C request R request S request T request S request T request R release R release S release T release S release T release R
If the OS knew in advance that granting a request would lead to deadlock, it could hold B back and avoid the dangerous order.
The OS suspends B's requests for a while (doesn't schedule B). Only A and C run.
1. A requests R and gets it.
2. C requests T and gets it.
3. A requests S and gets it (S is free).
4. C requests R: R is held by A → C blocks. No cycle; A can proceed.
5. A releases R → R goes to C; C proceeds.
6. A releases S and finishes. Now B can run: it gets S, and T once C finishes. No deadlock.
Tanenbaum MOS Figure 6-4 (k)–(q). "Which request should be deferred?" is the heart of avoidance (the banker's algorithm), coming up later.
Modeling
Multiple-Instance Resources
- Resource types: R1 ×1, R2 ×2, R3 ×1, R4 ×3.
- T1: holds one instance of R2, waits for R1.
- T2: holds R1 and one instance of R2, waits for R3.
- T3: holds R3.
- An assignment edge leaves from a dot (an instance) in the square; a request edge goes to the square itself.
- No cycle in the graph → no deadlock: when T3 finishes it releases R3, then T2 and T1 proceed.
Silberschatz 10e Figure 8.4. All three instances of R4 are free.
Modeling
Cycles and Deadlock
- A single request edge (T3 → R2) creates two cycles.
- T3 waits for T1 or T2 to release an R2; T2 waits for T3's R3; T1 waits for T2's R1.
- Every process in the cycle waits for another one in the cycle, and there is no outside "rescuer".
Previous state: no cycle.
T3 requests an instance of R2. Both instances are taken (by T1 and T2) → T3 blocks.
Cycle 1: T1 → R1 → T2 → R3 → T3 → R2 → T1.
Cycle 2: T2 → R3 → T3 → R2 → T2. The holders of both R2 instances are in the cycle → deadlock.
Silberschatz 10e Figure 8.5.
Modeling
A Cycle Without Deadlock
- When resource types have several instances, a cycle is necessary but not sufficient.
- When a process outside the cycle (T2 or T4) finishes and releases its instance, the cycle dissolves.
- So with multiple instances, searching for cycles is not enough; a matrix-based algorithm is needed.
T1 requests an R1; both R1 instances are held by T2 and T3. T3 requests an R2; R2's instances are held by T1 and T4.
There is a cycle: T1 → R1 → T3 → R2 → T1. But T2 and T4 are outside it and wait for nothing.
T4 finishes and releases its R2 instance → it goes to T3. The cycle is broken: there was no deadlock.
Silberschatz 10e Figure 8.6.
Modeling
The Cycle Rule
| Graph | Single instance of each resource type | Multiple instances of some types |
|---|---|---|
| No cycle | No deadlock | No deadlock |
| Cycle | Deadlock (cycle ⇔ deadlock) | Deadlock possible; a cycle is necessary but not sufficient |
Cycle detection suffices: DFS in O(V + E). The processes on the cycle are deadlocked.
A cycle is only a warning. For a definite answer, ask "can everyone finish?" using the allocation/request matrices.
The circular-wait condition corresponds to a graph with a cycle; with single-instance resources, the four conditions together guarantee deadlock.
Modeling
Wait-For Graph
An allocation graph with single-instance resources: P1 holds R1 and requests R2, P2 holds R2 and requests R3, P3 holds R3 and requests R1, P4 holds R4 and requests R2.
Each path Pi → R → Pj collapses into one edge Pi → Pj (red): "Pi is waiting for Pj".
The resource nodes are removed: the wait-for graph.
A cycle in the wait-for graph ⇔ deadlock. The algorithm scans the graph periodically; the cost is O(n²) (n: number of processes).
Usable only when there is a single instance of each type. Databases (e.g., PostgreSQL, MySQL InnoDB) detect deadlock by searching for cycles in the lock wait-for graph.
Strategies
Strategies for Handling Deadlocks
"The ostrich algorithm." If deadlock is rare and prevention is expensive, pretend the problem doesn't exist and reboot when needed.
Reactive. Deadlocks are allowed, detected periodically, then resolved by preemption, rollback, or termination.
Proactive, at run time. A request that would put the system in an unsafe state is deferred (banker's). Requires knowing maximum needs in advance.
Proactive, at design time. At least one of the four conditions is made structurally impossible (e.g., lock ordering).
"Recovery" is not a separate technique; it is the second half of detection.
Strategies
The Ostrich Algorithm
- Strategy: do nothing. If a deadlock occurs, the user or administrator kills the process or reboots the system.
- An engineering trade-off: if deadlocks happen once a year while crashes and hardware faults are far more frequent, expensive and restrictive measures are not worth it.
- General-purpose OSes (Linux, Windows, macOS) take this approach for user-process locks, file locks, and pipe waits; the responsibility lies with the application developer.
- But the OS protects itself internally: kernel lock ordering and
lockdepchecking. - Unacceptable in real-time and safety-critical systems.
Detection and Recovery
Detection: One Resource of Each Type
- A: holds R, requests S. B: requests T.
- C: requests S. D: holds U, requests S and T.
- E: holds T, requests V. F: holds W, requests S.
- G: holds V, requests U.
- Question: is the system deadlocked, and if so, which processes?
- Cycle in the graph: D → T → E → V → G → U → D. D, E, and G are deadlocked.
- A, C, and F are not deadlocked: S is free; one of them takes it, finishes, and releases it. B cannot proceed, since it waits for T on the cycle (stuck, though not on the cycle).
Tanenbaum MOS Figure 6-5. Easy by eye; a graph with hundreds of nodes needs a systematic algorithm.
Detection and Recovery
Cycle-Detection Algorithm (DFS)
- For each node N in the graph, perform steps 2–6 with N as the starting node.
- Set L to the empty list; mark all edges as unmarked.
- Add the current node to the end of L. If the node appears in L twice, the graph has a cycle: report the cycle in L and stop.
- Are there unmarked outgoing edges from the current node? If so, go to 5; if not, go to 6.
- Pick an unmarked edge at random, mark it, follow it to the new current node; go to 3.
- If this is the starting node, no cycle is reachable from N. Otherwise we've hit a dead end: remove the node from L, back up to the previous node, and continue at step 4 (that node is already in L).
A depth-first search from each node; the list L serves as the stack.
The simple version restarts from every node, so it is O(V·(V+E)); a colored DFS (white/gray/black) needs a single O(V+E) pass.
The part of L after the repeated node is the cycle; the processes in it are deadlocked.
Detection and Recovery
Cycle Detection, Step by Step
- Starting nodes are taken in the order shown (left to right, top to bottom): R, A, B, C, …
- Orange nodes are in the current list L; the red border marks the current node.
- Edge choice may be "random"; here S is tried first, then T.
Start at R. L = [R]. The only exit is R → A.
R → A → S. S has no outgoing edge: a dead end.
S, then A and R, are removed from L; we're back at R: no cycle from R. Next we start from A (none again)…
Start from B: B → T → E.
E → V → G → U. Still no repeat.
U → D. From D, S is tried first: a dead end, so S leaves L.
D → T: T appears in L a second time → cycle: T, E, V, G, U, D. The algorithm stops.
Detection and Recovery · Step through the code
Cycle Detection: Colored DFS Code
enum { WHITE, GRAY, BLACK }; int color[N]; /* all WHITE */ bool cycle(int u) { color[u] = GRAY; /* on path */ for (int v = 0; v < N; v++) { if (!waits[u][v]) continue; if (color[v] == GRAY) return true; /* back edge */ if (color[v] == WHITE && cycle(v)) return true; } color[u] = BLACK; /* done */ return false; }
Wait-for graph: P1 → P2, P2 → P3 and P4, P4 → P5, P5 → P2. All white; the search starts at P1.
P1 turns gray: it is on the current path.
P1 waits for P2 and P2 is white → cycle(P2) is called; P2 turns gray.
P2 waits for P3 and P3 is white → cycle(P3) is called; P3 turns gray.
P3 waits for no one: the for loop ends empty, P3 turns black and returns false; back to P2.
P2's next neighbor, P4, is white → cycle(P4); P4 turns gray.
P4 waits for P5 and P5 is white → cycle(P5) is called; P5 turns gray.
On edge P5 → P2, P2 is gray: still open on the stack. A back edge = a cycle: P2 → P4 → P5 → P2.
true propagates up the stack. P2, P4, and P5 on the cycle are deadlocked; P1 is not on the cycle but is stuck because it waits for P2.
Detection and Recovery
Detection: Multiple Resources of Each Type
- n processes, m resource types. Row i of C is what Pi currently holds; row i of R is what Pi currently requests.
- Vector comparison: X ≤ Y if and only if Xj ≤ Yj for every j. E.g., (1 0 1 0) ≤ (2 1 0 0) is false.
Tanenbaum MOS Figure 6-6. Silberschatz names the same structures Available, Allocation, and Request.
Detection and Recovery
Detection Algorithm
A ← available vector
all Pi unmarked
repeat:
find an unmarked Pi with Ri ≤ A
if none: exit
A ← A + Ci /* Pi ends, releases */
mark Pi
unmarked Pi's are deadlocked- Idea: a process whose request can be met with the currently available resources can finish; when it does, it returns what it holds.
- This is an optimistic assumption: the process is assumed to finish without requesting more. If that's wrong, the next run will catch it.
- Whatever remains unmarked at the end is deadlocked.
- Complexity: O(m · n²).
Detection and Recovery · Step through the algorithm
Detection Example
A ← (2 1 0 0) all Pi unmarked repeat: find unmarked Pi with Ri ≤ A if none: exit A ← A + Ci mark Pi unmarked ones deadlocked
Tape drives (Tp), plotters (Pl), scanners (Sc), CD. E = (4 2 3 1), available A = (2 1 0 0).
P1: R₁ = (2 0 0 1). No free CD → it can't finish yet.
P2: R₂ = (1 0 1 0). No free scanner ✗.
P3: R₃ = (2 1 0 0) ≤ A ✓. P3 can run and finish.
P3 finishes and returns its C₃ = (0 1 2 0): A = (2 2 2 0).
New round. P1 still waits (CD = 0).
P2: (1 0 1 0) ≤ (2 2 2 0) ✓ → finishes, returning C₂ = (2 0 0 1): A = (4 2 2 1).
P1: (2 0 0 1) ≤ (4 2 2 1) ✓ → finishes: A = (4 2 3 1) = E.
Every process is marked: no deadlock.
Tanenbaum MOS Figure 6-7. The order found (P3, P2, P1) depends on the order in which rows are scanned; the outcome does not.
Detection and Recovery
One Request Too Many: Deadlock
- Changing a single cell (the CD in R₃) changes the outcome completely.
- If no row can be satisfied in the first round, the algorithm stops at once.
- With multi-instance resources this scan replaces the graph; cycle information comes for free.
Same state, one difference: in addition to two tape drives and a plotter, P3 also wants a CD: R₃ = (2 1 0 1).
P1 ✗ (needs a CD, none free).
P2 ✗ (needs a scanner, none free).
P3 ✗ (the only CD is held by P2). No row can be satisfied: all three processes are deadlocked.
Detection and Recovery
When Should Detection Run?
After every request that must wait. The deadlock is found the moment it forms, along with the culprit request; but the CPU cost is high.
Every k minutes. Cheap; a deadlock may go unnoticed for a while, and more processes may get caught in it.
When CPU utilization drops below some threshold, or a lock wait grows long.
- How often? It depends on how likely deadlock is and how many processes it would affect.
- Example: PostgreSQL scans the wait-for graph for a transaction that has waited for a lock for
deadlock_timeout(default 1 s); if it finds a cycle, it aborts a transaction (ERROR: deadlock detected). - The overhead of detection is not negligible and grows with how often it runs; the cost of recovery (lost work) must be counted too.
Detection and Recovery
Recovery from Deadlock
Temporarily take a resource from its owner and give it to another process (e.g., set a print job aside and separate the pages). Depends on the resource; often manual or impossible.
Processes periodically save checkpoints. The victim is rolled back to a checkpoint before it acquired the resource; later work is lost. This is a transaction abort in databases.
The crudest method. Kill either all processes in the cycle, or one at a time until the cycle breaks (rerunning detection after each kill). A process outside the cycle may also be chosen.
Victim selection (minimum cost): priority, how long it has run and how much remains, how many resources it holds, how many more it needs to finish, interactive or batch, whether it can be restarted (a compile, yes; a database update, carefully). If the same process is always chosen, it starves: count its rollbacks in the cost.
Detection and Recovery
Recovery, Step by Step
The detection algorithm found the cycle P1 → R2 → P2 → R3 → P3 → R1 → P1. P4 is stuck too, since it waits for R2.
The victim is the process whose loss costs least: P3. It is terminated (or rolled back to a checkpoint); the R3 it held becomes free.
R3 goes to the waiting P2. P2 finishes its work and releases R2 and R3.
R2 goes to the next waiter (P1); when P1 finishes, to P4. P3 is rerun from the start (or from its checkpoint).
When killing one at a time, detection reruns after each victim; stop once the cycle is broken.
Avoidance
Resource Trajectories
- A uses the printer from I1 to I3 and the plotter from I2 to I4.
- B uses the plotter from I5 to I7 and the printer from I6 to I8.
- The shaded regions cannot be entered because of mutual exclusion.
- Avoidance = deferring any request that would steer the trajectory into the unsafe region.
The horizontal axis is A's executed instructions, the vertical axis B's. With one CPU the trajectory moves only right (A runs) or up (B runs).
p → q: A runs; q → r: B runs; r → s: A acquires the printer (I1). s → t: B advances and will request the plotter at I5.
Once inside the red box, A holds the printer and B the plotter, and both must enter a shaded region: deadlock is inevitable. This region is unsafe.
At point t the OS defers B's plotter request; A runs to I4 and releases both. Then B continues: no deadlock.
Tanenbaum MOS Figure 6-8 (Dijkstra's two-process diagram).
Avoidance
Safe and Unsafe States
- State: determined by E, A, C, and the maximum needs (Max).
- Safe state: there is a safe sequence <P1, …, Pn> that runs all processes to completion in some order, even if every process immediately requests its maximum.
- Pi's remaining need ≤ available + held by all Pj (j < i).
- Unsafe ≠ deadlocked. An unsafe state is not yet a deadlock; if processes don't make their maximum requests, all may go well. But the system can no longer guarantee completion.
- An avoidance algorithm keeps the system in a safe state at all times.
Avoidance
A Safe State Example
- One resource type, 10 instances.
- Has: currently held; Max: the most it will need before finishing.
- Remaining need = Max − Has.
- At every step the OS is in a position to choose the order of requests.
(a) 10 instances in total; 7 allocated, 3 free. Is it safe? Let's look for a safe sequence.
(b) B's remaining need is 2 ≤ 3: B gets 2 and reaches its maximum.
(c) B finishes and releases 4 instances: 5 free.
(d) C's remaining need is 5 ≤ 5: C gets 5.
(e) C finishes: 7 free. A's remaining 6 can be met. The safe sequence B → C → A exists: the state is safe.
Tanenbaum MOS Figure 6-9.
Avoidance
An Unsafe State Example
- The difference is a single decision: giving A one extra instance in (a).
- (b) is not yet a deadlock. If A releases a resource before requesting more, there may be no problem.
- But the OS assumes the worst case: if everyone requests its maximum, completion is not guaranteed.
The same (safe) start. Now A requests one more instance and the OS grants it.
(b) A: 4/9, 2 free. Is there a safe sequence?
(c) Only B can finish (need 2 ≤ 2): B gets 2.
(d) B finishes: 4 free. A needs 5 and C needs 5: neither can be guaranteed. State (b) was unsafe; A's request should have been deferred.
Tanenbaum MOS Figure 6-10.
Avoidance
Banker's Algorithm: A Single Resource
- On every request: "If I grant this, will the state remain safe?"
- Yes → grant. No → defer (the process waits and the request is reconsidered later).
- Safety: find a customer whose remaining need can be met from what's free, assume it finishes, add its credit back to the vault; repeat.
(a) The banker (Dijkstra, 1965): 10 units in the vault, 4 customers whose credit limits total 22. Not everyone asks for their maximum at once.
(b) 2 free. C's need 2 ≤ 2 → C finishes (4 free), then D (3 ≤ 4 → 8 free), A, B. Safe.
(c) If B asks for one more unit in (b): 1 free, smallest need 2. Unsafe → the request must be refused and B made to wait.
Avoidance
Banker's Algorithm: Multiple Resources
Available[m]: free instances of each type.Max[n][m]: each process's maximum demand, declared in advance.Allocation[n][m]: currently held.Need = Max − Allocation: remaining need.
/* safety algorithm */ Work ← Available Finish[i] ← false (all i) while some i has Finish[i] = false and Need[i] ≤ Work: Work ← Work + Allocation[i] Finish[i] ← true SAFE if Finish[i] for all i
/* Ti requests Request[i] */ if Request[i] > Need[i]: error /* exceeds Max */ if Request[i] > Available: Ti waits /* not available */ /* pretend to allocate: */ Available -= Request[i] Allocation[i] += Request[i] Need[i] -= Request[i] if safe: grant the request else: restore, Ti waits
Complexity: the safety test is O(m · n²) and runs on every request.
Avoidance · Step through the algorithm
The Safety Algorithm: An Example
Work ← Available Finish[i] ← false for i = T0 … T4 (repeatedly): if Finish[i] = false and Need[i] ≤ Work: Work ← Work + Allocation[i] Finish[i] ← true if all Finish[i]: SAFE
A=10, B=5, C=7 instances. Available = (3 3 2). Need = Max − Allocation has been computed.
T0: Need (7 4 3) ≤ (3 3 2)? No, skip.
T1: (1 2 2) ≤ (3 3 2) ✓ → finishes, Work = (3 3 2) + (2 0 0) = (5 3 2).
T2: (6 0 0) ≤ (5 3 2)? Not enough A ✗.
T3: (0 1 1) ✓ → Work = (7 4 3).
T4: (4 3 1) ✓ → Work = (7 4 5).
Second round. T0: (7 4 3) ≤ (7 4 5) ✓ → Work = (7 5 5).
T2: (6 0 0) ✓ → Work = (10 5 7), i.e., all resources.
All Finish true: the state is safe. Other safe sequences exist too, such as <T1, T3, T4, T2, T0>.
The example from Silberschatz 10e §8.6.3.3. Max: T0 (7 5 3), T1 (3 2 2), T2 (9 0 2), T3 (2 2 2), T4 (4 3 3).
Avoidance
Granting a Request: T1 (1 0 2)
- Three steps: the Need check, the Available check, and the safety test.
- The safety test runs on the "pretend" state; had the state been unsafe, the changes would be rolled back.
- Calculations verified with a Python script.
T1 requests (1 0 2). Check 1: it doesn't exceed its declared maximum need ✓.
Check 2: the resources are free right now ✓. But that alone is not enough.
The new state is computed as if the request were granted.
The safety algorithm finds the sequence <T1, T3, T4, T0, T2> in the new state: safe → the request is granted.
Avoidance
Refusing Requests: T4 (3 3 0) and T0 (0 2 0)
- Reason to wait 1: resources unavailable (T4).
- Reason to wait 2: resources available, but granting would make the state unsafe (T0).
- This is the price of avoidance: a request is deferred even while resources sit idle, lowering utilization.
After T1's request is granted, T4 requests (3 3 0). It doesn't exceed Need, but only 2 instances of type A are free: T4 waits.
T0 requests (0 2 0). Both checks pass; the resources are free.
Pretend: Available = (2 1 0). Which Need ≤ (2 1 0)? T1 (0 2 0): not enough B; T3 (0 1 1): not enough C…
No process can finish: the state is unsafe. The changes are rolled back and T0 waits, even though the resources are free.
Avoidance · Step through the algorithm
Banker's Request Algorithm: Code Trace
/* Ti requests Request */ if Request > Need[i]: error /* exceeds Max */ if Request > Available: Ti waits Available -= Request /* pretend */ Allocation[i] += Request Need[i] -= Request if safe(): GRANT the request else: restore, Ti waits safe(): Work ← Available; Finish ← false for i = T0 … T4 (repeatedly): if !Finish[i] and Need[i] ≤ Work: Work ← Work + Allocation[i] Finish[i] ← true return all Finish[i]
A safe initial state: Available = (3 3 2). T3 requests Request = (0 1 1). Need[3] = (0 1 1): the request doesn't exceed the declared maximum.
(0 1 1) ≤ Available (3 3 2): the resources are there now. Still, it isn't granted immediately.
Pretend to allocate: Available = (3 2 1), Allocation[3] = (2 2 2), Need[3] = (0 0 0).
Safety test: Work = (3 2 1). T0 (7 4 3), T1 (1 2 2), T2 (6 0 0) can't finish with this Work.
T3: Need (0 0 0) ≤ Work (3 2 1) ✓ → finishes, Work = (3 2 1) + (2 2 2) = (5 4 3).
T4: Need (4 3 1) ≤ Work (5 4 3) ✓ → finishes, Work = (5 4 3) + (0 0 2) = (5 4 5).
Second round. Not enough A for T0 (7 4 3); T1: Need (1 2 2) ≤ Work (5 4 5) ✓ → finishes, Work = (5 4 5) + (2 0 0) = (7 4 5).
T2: Need (6 0 0) ≤ Work (7 4 5) ✓ → finishes, Work = (7 4 5) + (3 0 2) = (10 4 7).
Third round: T0: Need (7 4 3) ≤ Work (10 4 7) ✓ → finishes, Work = (10 4 7) + (0 1 0) = (10 5 7).
All Finish true → safe sequence <T3, T4, T1, T2, T0>. The request is granted and the state becomes permanent.
The same Silberschatz state; only the request differs. Values verified by a Python simulation.
Avoidance
Multiple Resources: A Second Example
Five processes, four resource types (Tp, Pl, Pr = printer, CD). A = E − P = (1 0 2 0).
D (0 0 1 0) ≤ A → finishes, A = (2 1 2 1); E, A, B, C then finish in turn. The state is safe.
B requests a printer. If granted, A = (1 0 1 0); D can still finish and the sequence survives: granted.
If E then requests the last printer, A becomes (1 0 0 0) and no row can be satisfied: unsafe → deferred.
Tanenbaum MOS Figure 6-12. The "find a row ≤ A" steps here are the safety test; the same logic is used with the request matrix in detection and with the remaining-need matrix in avoidance.
Avoidance
The Banker's Algorithm in Practice
Each process declares its maximum need in advance; the number of processes is fixed; the number of resources doesn't change (no device fails); processes release resources in finite time.
These assumptions don't hold in general-purpose OSes, and the algorithm is almost never used. Related "admission control" ideas: real-time scheduling, cloud quotas, pooled connection limits.
- Because of the O(m·n²) test on every request and unnecessary deferrals, avoidance is usually more expensive than detection and recovery; its advantage is that no recovery is needed.
- The algorithm doesn't run continuously in the background: the test runs only when a request arrives (and, for waiters, when a resource is released).
Prevention
Prevention: Attacking the Conditions
| Condition | Attack | Cost |
|---|---|---|
| Mutual exclusion | Make the resource shareable: spooling, read-only sharing, lock-free structures | Most resources are inherently unshareable (a mutex!) |
| Hold and wait | Request all resources at once up front, or release what you hold before a new request | Low utilization, starvation; needs must be known in advance |
| No preemption | Take back a waiting process's resources; tryLock + back off; virtualize | Only for resources whose state can be saved; risk of livelock |
| Circular wait | Impose a total order on resources and request only in increasing order (lock ordering) | Following the order takes programmer discipline |
Prevention
Attacking Mutual Exclusion
- Spooling: processes write their output to disk; a single daemon uses the printer in turn. Contention for the printer disappears.
- Read-only files and code pages are shared simultaneously; reader locks don't exclude each other.
- Lock-free structures progress with CAS; with no locks there is no deadlock (starvation is still possible).
- The limit: the spool disk is itself a resource. If it fills with half-written output, deadlock can still occur (Tanenbaum).
- General principle: don't assign a resource unless it is really needed, and minimize the number of processes that may claim it.
Prevention
Attacking Hold and Wait
- Protocol 1: a process acquires all the resources it will need in a single request before it starts (all or nothing).
- Protocol 2: a process may request resources only when it holds none: it releases what it holds before requesting more.
- Example: copy from DVD to disk, sort the file, print it. Under Protocol 1 the printer is held idle from start to finish.
- Problems: low resource utilization; starvation for a process that needs many resources; needs are often unknown in advance.
- In practice: "all or nothing" acquisition with
pthread_mutex_trylock; conservative 2PL in databases, which takes all locks up front.
Silberschatz 10e §8.5.2.
Prevention
Attacking No Preemption
- Preemption protocol: if a process holding resources makes a request that cannot be granted at once, all its resources are implicitly released; it restarts only when it can get them all together.
- Suitable only for resources whose state can be saved and restored: CPU registers, memory, database transactions (abort).
- Virtualization: the printer is spooled, memory is paged.
- For mutexes: timed attempts. A thread that can't acquire a lock releases what it holds itself and retries after a random delay.
- Run on JDK 21: 2 threads × 1000 transfers in opposite directions, result
x=1000 y=1000 total=2000.
boolean tryXfer(Account a, Account b, int m) throws InterruptedException { if (!a.lock.tryLock(50, MILLISECONDS)) return false; try { if (!b.lock.tryLock(50, MILLISECONDS)) return false; /* a released too */ try { a.balance -= m; b.balance += m; return true; } finally { b.lock.unlock(); } } finally { a.lock.unlock(); } } /* caller: while (!tryXfer(x, y, 1)) Thread.sleep(random); */
The random delay matters: if two threads try and release in lockstep, a livelock results.
Prevention · Step through the code
Backing Off with tryLock: Code Trace
boolean tryXfer(Account a, Account b, int m) throws InterruptedException { if (!a.lock.tryLock(50, MILLISECONDS)) return false; try { if (!b.lock.tryLock(50, MILLISECONDS)) return false; /* a released too */ try { a.balance -= m; b.balance += m; return true; } finally { b.lock.unlock(); } } finally { a.lock.unlock(); } } /* caller: while (!tryXfer(x, y, 1)) Thread.sleep(random); */
T1 moves 1 from x to y, T2 from y to x: they request the locks in opposite order. Both locks are free.
T1 takes x at once.
T2 takes y. With a plain lock(), the next step would make deadlock certain.
Each waits at most 50 ms for its second lock: a circular wait has formed, but a timed one.
T1's 50 ms are up: tryLock returns false → return false.
finally makes T1 release the x it holds: the no-preemption condition is broken voluntarily.
T2 is still within its own 50 ms: it gets x once it is free.
T2 transfers: y.balance = 999, x.balance = 1001.
T2 releases x, then y, and returns true.
After a random sleep T1 retries, gets both locks, and transfers: the total is still 2000.
Prevention
Attacking Circular Wait
| # | Resource |
|---|---|
| 1 | Imagesetter |
| 2 | Printer |
| 3 | Plotter |
| 4 | Tape drive |
| 5 | Blu-ray drive |
If a printer and a tape drive are needed, request 2 first, then 4. Finding a suitable ordering is the hardest part.
Every resource type gets a number. Rule: a process may request only resources numbered higher than any it holds.
If A holds i and requests j, the rule implies i < j.
If B held j and requested i, then j < i: a contradiction. This edge can never form. In general, the process holding the highest-numbered resource in a cycle could not request a lower-numbered one.
Tanenbaum MOS Figure 6-13. A variant: before requesting Rj, a process must release every Ri it holds with i ≥ j.
Prevention · Step through the code
Lock Ordering in Practice
void transfer(Account a, Account b, int m) { Account first = a.id < b.id ? a : b; Account second = a.id < b.id ? b : a; first.lock.lock(); try { second.lock.lock(); try { a.balance -= m; b.balance += m; } finally { second.lock.unlock(); } } finally { first.lock.unlock(); } } /* T1: transfer(x, y, 1) x.id = 1 T2: transfer(y, x, 1) y.id = 2 */
T1 moves x→y and T2 moves y→x: the "natural" order is reversed between the two threads. The id order of the locks, however, is shared.
T1: first = x (1 < 2). It acquires lock x.
T2: a = y, but first is still x (id 1). It waits for x holding no lock at all.
T1 also gets y (no one holds it) and performs the transfer.
T1 releases its locks; T2 takes x, then y. No cycle can form.
Run on JDK 21: 100,000 opposite transfers each, result x=1000 y=1000, no deadlock.
The Linux kernel takes two locks of the same type in address order (e.g., double_rq_lock); lock order is documented and checked by lockdep.
Other Issues
Two-Phase Locking (2PL)
- A protocol for database transactions: in the growing phase locks are only acquired, in the shrinking phase only released; after the first release, no new lock may be taken.
- Its goal is serializability (as if transactions ran one after another), not deadlock prevention. Basic 2PL can deadlock: T1 locks a, T2 locks b, then each requests the other's.
- Tanenbaum's variant: in phase 1, try to lock all records; if any is locked, release them all and start over. This removes hold and wait.
- Strict 2PL: locks are released at transaction end (commit/abort). The DBMS detects deadlocks with a wait-for graph and rolls back a transaction.
Other Issues
Communication Deadlocks
- Communication deadlock: there is no resource; processes wait for messages from each other. A sends B a request and waits for the reply; if the request is lost, A waits for the reply and B for the request, forever.
- Resource ordering and avoidance don't help; the cure is a timeout and retransmission. The protocol needs sequence numbers to handle duplicate messages.
- In network hardware, though, buffers are real resources; the ring below is a resource deadlock.
Four routers in a ring, each with limited buffer memory.
Under heavy traffic every buffer fills with packets bound for the next router clockwise.
A waits for a free buffer at B, B at C, C at D, D at A: circular wait. Remedies: dropping packets on timeout, virtual channels, credit-based flow control.
Tanenbaum MOS §6.7.2, Figure 6-15.
Other Issues
Livelock
- Two people meeting in a corridor both step to the same side at the same time: neither gets past.
- In deadlock, processes sleep; in livelock they run but do no useful work.
void process_A(void) { enter_region(&resource_1); enter_region(&resource_2); use_both_resources(); leave_region(&resource_2); leave_region(&resource_1); } /* B: resource_2 first, then 1 */
Tanenbaum Figure 6-16: if busy-waiting enter_region is called in reverse order, the processes spin forever without blocking.
A gets r1, B gets r2. A tries r2 (tryLock fails), B tries r1 (fails).
They are polite: both release what they hold and retry after the same delay. The same thing repeats.
The processes are not blocked; they burn CPU and their state keeps changing, yet neither makes progress: livelock.
Remedies: random (exponential) backoff, priorities/sequence numbers, or lock ordering. Ethernet's CSMA/CD backoff is the same idea.
Other Issues
Starvation
- A process waits indefinitely because it never gets its turn, even though the resource becomes available. Others make progress: the system is not deadlocked.
- It results from the allocation policy. Example: always giving the printer to the smallest file reduces average waiting time but can starve a large file.
- Other examples: strict priority scheduling, a reader-preferring reader–writer lock, always choosing the same victim in recovery.
- Remedies: first-come, first-served (FIFO), or raising priority the longer a process waits (aging).
| Deadlock | Livelock | Starvation | |
|---|---|---|---|
| Process state | blocked | running | ready/blocked |
| Who is affected? | everyone in the cycle | the interacting group | one or a few processes |
Other Issues
Non-Deadlock Bugs: Atomicity and Order
Deadlock isn't the most common concurrency bug: of 105 bugs studied in MySQL, Apache, Mozilla, and OpenOffice, 74 were non-deadlock, and nearly all fit two patterns (Lu et al., 2008).
/* T1 */ if (sess->name != NULL) puts(sess->name); /* NULL now? */ /* T2 */ sess->name = NULL;Fix: put the test-and-use and T2's write under the same mutex.
/* T1: init() */ worker = thread_create(body); /* T2: body(), the new thread */ s = worker->state; /* worker set yet? */Fix: a
ready flag and a condition variable. T2 does while (!ready) wait; T1 does ready = 1; signal after the assignment (or use a semaphore starting at 0).Deadlock in Practice · Step through the code
Deadlock in Java
Object a = new Object(), b = new Object(); Thread t1 = new Thread(() -> { synchronized (a) { nap(10); /* widen the window */ synchronized (b) { work(); } } }, "T1"); Thread t2 = new Thread(() -> { synchronized (b) { nap(10); synchronized (a) { work(); } } }, "T2"); t1.start(); t2.start();
Two threads will acquire two monitors (synchronized locks) in opposite order.
T1 enters a's monitor and sleeps.
Meanwhile T2 enters b's monitor.
T1 wakes and requests b: b is held by T2 → T1 is BLOCKED.
T2 requests a → BLOCKED. JVM monitors cannot be preempted: the program never ends.
nap(10) makes the timing very likely but doesn't guarantee it. A version that used a CountDownLatch to wait until both first locks were held deadlocked on every run under JDK 21.
Deadlock in Practice · Step by step
Encapsulation Hides Lock Order
var a = Collections.synchronizedList( new ArrayList<Integer>()); var b = Collections.synchronizedList( new ArrayList<Integer>()); /* inside a.addAll(b): */ /* synchronized (a) { b.toArray() } */ /* → synchronized (b) { ... } */ new Thread(() -> a.addAll(b)).start(); new Thread(() -> b.addAll(a)).start(); /* Fix: take the copy outside the lock */ Integer[] k = b.toArray(new Integer[0]); a.addAll(Arrays.asList(k));
| Cross call | JDK 8 | JDK 21 |
|---|---|---|
Vector: a.addAll(b) ‖ b.addAll(a) | 10/10 | 0/10 |
synchronizedList: same | 10/10 | 10/10 |
| Fixed version | 0/10 | 0/10 |
T1: a.addAll(b) first takes a's lock in the wrapper.
At the same time T2: b.addAll(a) first takes b's lock.
Inside, b.toArray() and a.toArray() are called: each wants the other's lock.
Table: deadlocks out of 10 trials, 2 threads × 200,000 rounds; results may vary between runs (e.g., 9/10). In JDK 21, Vector.addAll calls the argument's toArray() without the lock. Rule: never call alien methods while holding a lock.
Deadlock in Practice
Kilitlenmeyi Bulmak: jstack
$ jstack <pid>
Found one Java-level deadlock:
=============================
"T1":
waiting to lock monitor 0x… (object 0x…, a java.lang.Object),
which is held by "T2"
"T2":
waiting to lock monitor 0x… (object 0x…, a java.lang.Object),
which is held by "T1"
Java stack information for the threads listed above:
"T1":
at Hang.run(Hang.java:7)
- waiting to lock <0x…d0> (a java.lang.Object)
- locked <0x…c0> (a java.lang.Object)
…
Found 1 deadlock.Real JDK 21 output (addresses shortened). The same report: jcmd <pid> Thread.print, JConsole/VisualVM "Detect Deadlock".
ThreadMXBean mx =
ManagementFactory.getThreadMXBean();
long[] ids = mx.findDeadlockedThreads();
if (ids != null)
for (ThreadInfo ti : mx.getThreadInfo(ids))
System.out.println(ti.getThreadName()
+ " waits, lock owner "
+ ti.getLockOwnerName());T1 waits, lock owner T2 T2 waits, lock owner T1
- The JVM builds a wait-for graph from monitor and
ReentrantLockownership and searches it for cycles. - It detects but does not recover: the process must be restarted.
Deadlock in Practice
Tools: lockdep and Others
| Environment | Tool / behavior |
|---|---|
| Linux kernel | lockdep (CONFIG_PROVE_LOCKING): lock-order validator; hung-task detector |
| C/C++ | ThreadSanitizer: lock-order-inversion; Valgrind Helgrind |
| Java | jstack, jcmd, ThreadMXBean |
| Go | fatal error: all goroutines are asleep - deadlock! |
| Database | PostgreSQL, InnoDB: wait-for graph + rolling back a victim transaction |
lockdep ties every lock to a class (all instances of the same kind).
When a task takes B while holding A, the edge "A → B" is recorded.
If another path takes A while holding B, the graph gets a cycle: the deadlock is reported without ever happening.
lockdep and TSan find potential deadlocks (order violations); jstack and the Go runtime report deadlocks that have happened.
Summary
Summary
| Approach | When? | Information needed | Cost | Used in |
|---|---|---|---|---|
| Ignore | — | none | an occasional reboot | general-purpose OSes |
| Detection + recovery | after the fact | current allocations and requests | periodic O(m·n²), lost work | databases, JVM tools |
| Avoidance | on every request | maximum needs (Max) | deferrals, O(m·n²) per request | rarely, special systems |
| Prevention | at design time | resource order | lost flexibility and utilization | kernel, application code |
- The four conditions are necessary together; with single-instance resources, cycle ⇔ deadlock.
- An unsafe state is not a deadlock, but completion is not guaranteed.
- In practice the most effective tools: lock ordering + timed attempts + checking with tools (lockdep, jstack, TSan).
Exercises
Exercise: Classify the Bug
Classify each case as deadlock, livelock, atomicity violation, or order violation, and give the fix.
- Two consumers on a thread-safe queue:
if (!q.isEmpty()) x = q.remove(); - The main thread first starts the worker, then does
buf = malloc(N); the worker writesbuf[0]on its first line. transfer(x, y)andtransfer(y, x)run at once; each locks the source account first, then the destination.- Two threads do
lock(A); if (!trylock(B)) { unlock(A); usleep(10); retry; }; the other takes B first. The delay is always 10 µs. - 2 CPUs and 4 threads: T1 and T2 use both L1 and L2, T3 uses only L2, T4 uses no locks. How can a lock-aware scheduler prevent deadlock?
Answers
- Atomicity violation: each call is safe on its own, but the other consumer can take the item in between. Fix: one atomic operation (
poll()), or both under the same lock. - Order violation: the worker may run before
bufis assigned. Fix: allocate first, then start the thread; or a flag + condition variable. - Deadlock (circular wait). Fix: acquire the locks in account-number order.
- Livelock: both acquire and release in lockstep. Fix: random, increasing backoff, or lock ordering.
- Never run T1 and T2 at the same time (run them back to back on one CPU). T3 wants a single lock, so it can't create hold and wait; T4 is free. The cost: less parallelism, and lock usage must be known in advance.
Summary
Review Questions
- State the four conditions for deadlock. Which one is a state rather than a policy?
- How can a graph have a cycle without deadlock? Give an example.
- With E = (4 2 3 1), A = (2 1 0 0), what if P3's request were (2 1 0 1)?
- What is the difference between an unsafe state and a deadlocked state?
- In the Silberschatz example, what if T0 requests (0 2 0) after T1's (1 0 2) request? Why?
- Why isn't the banker's algorithm used in general-purpose OSes?
- Why does numbering resources make circular wait impossible?
- How does livelock differ from deadlock? How is livelock broken?
Answers
- Mutual exclusion, hold and wait, no preemption, circular wait; circular wait is the current state.
- With multi-instance resources, a process outside the cycle can release an instance (Silberschatz Fig. 8.6: T4 releases R2).
- No row is ≤ A; P1, P2, and P3 are deadlocked.
- In an unsafe state nobody is blocked yet, but completion for all isn't guaranteed; in deadlock a cycle has formed.
- The resources are free, but in the pretend state Available = (2 1 0) and no Need can be met: unsafe, so T0 waits.
- Maximum needs aren't known in advance, process and resource counts change, and testing every request is costly.
- The holder of the highest-numbered resource in the cycle can't request a lower one, so the cycle can't close.
- Deadlocked processes are blocked; livelocked ones run without progress. Break it with random backoff or ordering.
Summary
References
- A. Silberschatz, P. B. Galvin, G. Gagne, Operating System Concepts, 10th ed., Wiley, 2018 (Chapter 8: Deadlocks).
- A. S. Tanenbaum, H. Bos, Modern Operating Systems, 4th ed. (2014) / 5th ed. (2023), Pearson (Chapter 6: Deadlocks).
- R. H. Arpaci-Dusseau, A. C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces (OSTEP), ostep.org: "Common Concurrency Problems".
- E. G. Coffman, M. J. Elphick, A. Shoshani, "System Deadlocks", ACM Computing Surveys 3(2), 1971.
- E. W. Dijkstra, "Cooperating Sequential Processes", EWD-123, 1965 (the banker's algorithm); R. C. Holt, "Some Deadlock Properties of Computer Systems", ACM Computing Surveys, 1972.
- Linux kernel documentation:
Documentation/locking/lockdep-design.rst. - Java SE 21:
java.lang.management.ThreadMXBean,java.util.concurrent.locks.ReentrantLock; thejstackmanual. - PostgreSQL documentation: "Explicit Locking: Deadlocks",
deadlock_timeout.