Chapter 7: Deadlocks
Multiple Choice · Introduction
Question 1: Preemptible Resource
Which of the following is a preemptible resource, one that can be taken from its holder and returned later without harm?
C: A page’s contents can be written to disk and restored later; the CPU too changes hands by saving its context. An interrupted write job is ruined, and a mutex or record lock is released only by its owner: these are non-preemptible resources, and deadlocks arise with this kind of resource.
Output Question · Introduction
Question 2: Acquisition in Opposite Order
Binary semaphores r1 and r2 start at 1. Two scheduling orders are tried on one CPU: (a) A1, A2, B1, … (b) A1, B1, A2, B2 (A1: A’s first line). What happens in each order?
/* A */ /* B */ down(&r1); down(&r2); down(&r2); down(&r1); use_both(); use_both(); up(&r2); up(&r1); up(&r1); up(&r2);
Show answer
(a) A gets both resources. At B1, B waits for r2, but continues once A finishes and calls up: no deadlock.
(b) A takes r1 and B takes r2; at A2, A waits for r2, and at B2, B waits for r1. Neither can reach its up lines: deadlock.
Deadlock occurs only if each thread makes its first down before the other’s second down; that is why it is rarely seen in tests. If both took r1 first, then r2, no order would deadlock.
Multiple Choice · Deadlock Conditions
Question 3: Which Condition Was Broken?
A system has this rule: before requesting a new resource, a process must release all the resources it holds, then request them all together again. Which deadlock condition does this rule eliminate?
A: A process never waits for one resource while holding another. The cost: resources are released and reacquired needlessly, and a process needing many resources may starve. Breaking no preemption means being able to forcibly take a resource back from the process.
Output Question · Deadlock Conditions
Question 4: How Many Resources Suffice?
Processes request identical resources of the same type (e.g. tape drives) one at a time and release them all when done. (a) 4 processes, each needing at most 3 drives at once: how many drives are needed at minimum so that deadlock can never occur? (b) 6 processes, each needing at most 4 drives, 18 drives in the system: can deadlock occur? (c) With 18 drives and at most 4 per process, how many processes can run safely at most?
Show answer
In the worst case every process holds k − 1 resources and waits for one more. If one more resource exists, one process completes, finishes and releases its resources: the required minimum is n · (k − 1) + 1.
- (a) 4 · 2 + 1 = 9 drives.
- (b) 6 · 3 + 1 = 19 > 18: it can happen. If all 18 drives are spread three per process across six processes, everyone waits for a fourth.
- (c) 3n + 1 ≤ 18 → n ≤ 5.67 → at most 5 processes.
This bound is the number of resources at which hold and wait cannot turn into circular wait; it is the single-resource worst case of the banker’s algorithm.
Step-by-Step Trace · Modeling
Question 5: A Cycle, but Deadlock?
R1 has 2 instances, R2 and R3 have one each. P1 holds R2 and requests R1; P2 holds one R1 and requests R3; P3 holds R3 and requests R2; P4 holds the other R1 instance and makes no request. Is there a cycle in the graph? Is the system deadlocked?
Dashed arrow is a request (process → resource), solid arrow an assignment (instance → process). No resource has a free instance.
Cycle: P1 → R1 → P2 → R3 → P3 → R2 → P1. With single instances this would be certain deadlock; but R1’s second instance is held by P4, outside the cycle.
P4 waits for nothing: it finishes and releases its R1 instance.
P1’s request for R1 is granted; P1 finishes and releases R1 and R2.
P3 gets R2 and finishes; R2 and R3 become free.
P2 gets R3 and finishes. Everyone finished: no deadlock. With multiple-instance resources a cycle is necessary but not sufficient.
while (some unfinished P has a request the free resources can meet) { give P what it wants; P finishes, releases all its resources; } if (an unfinished process remains) deadlock exists; else no deadlock;
Output Question · Modeling
Question 6: Who Is Deadlocked?
All resources have a single instance. A holds R and requests S; B holds S and requests T; C holds T and requests R. D holds U and requests S. E holds V and makes no request. Draw the wait-for graph. Which processes are deadlocked, and which can make progress?
Show answer
Resource nodes are removed, and every “Pi → R → Pj” path becomes a “Pi waits for Pj” edge: A → B, B → C, C → A, D → B.
Cycle A → B → C → A: with single-instance resources a cycle proves deadlock, A, B, C are deadlocked. D is not in the cycle, but it waits for B, which is, so it cannot proceed either. E waits for nothing and finishes.
Multiple Choice · Strategies
Question 7: Which Strategy Is the Banker’s?
The banker’s algorithm asks before every grant “will the state stay safe if this request is granted?” and postpones the request if not. Which strategy is it an example of?
B: Avoidance allows all four conditions but keeps the system in a safe state at run time; it requires maximum needs to be known in advance. Prevention makes one condition impossible by design; detection lets deadlock happen and then finds it.
Step-by-Step Trace · Detection and Recovery
Question 8: Detection Algorithm
Three resource types, total E = (5 3 4). The allocation (C) and current request (R) matrices are in the table. Is the system deadlocked? Rows are scanned in order starting from P1.
E = (5 3 4), sum of allocations (4 3 3) → free A = (1 0 1). Look for an unmarked process whose request A can satisfy.
P1: R (0 1 0) ≤ A (1 0 1) fails (no free resource of type B) → skip for now.
P2: R (1 0 1) ≤ A ✓ → can finish, releases its allocation: A = (3 1 1).
P3: R (2 0 1) ≤ A ✓ → can finish, releases its allocation: A = (3 2 3).
P4: R (0 0 2) ≤ A ✓ → can finish, releases its allocation: A = (4 3 3).
P1: R (0 1 0) ≤ A ✓ → can finish, releases its allocation: A = (5 3 4). All processes are marked: no deadlock.
A = E - (sum of the rows of C); all unmarked; while (some unmarked Pi has R[i] <= A) { A = A + C[i]; /* Pi finishes */ mark Pi; } if (no unmarked process remains) no deadlock;
Multiple Choice · Detection and Recovery
Question 9: Deadlocked Processes
Three resource types, total E = (5 3 4); free A = (1 0 1). The allocation (C) and current request (R) matrices are in the table. According to the detection algorithm, which processes are deadlocked?
| Process | Allocation C | Request R |
|---|---|---|
| P1 | (1 0 1) | (0 1 0) |
| P2 | (2 1 0) | (1 1 1) |
| P3 | (0 1 2) | (2 0 1) |
| P4 | (1 1 0) | (0 0 2) |
D: With A = (1 0 1), P1 and P2 want type B (0 free), P3 wants 2 of type A (1 free), P4 wants 2 of type C (1 free). No row can be satisfied in the first pass; the algorithm stops at once and all four stay unmarked. Only one cell matters: if P2’s request were (1 0 1), P2 would be satisfied and all four would finish.
Output Question · Avoidance
Question 10: Single-Resource Banker
There are 14 units of a single resource type. A holds 4 (max 9), B holds 3 (max 5), C holds 2 (max 8), D holds 1 (max 6). Is the state safe? In this state, should a request by C for 3 more units be granted?
Show answer
| A | B | C | D | |
|---|---|---|---|---|
| Held | 4 | 3 | 2 | 1 |
| Need | 5 | 2 | 6 | 5 |
Free 14 − 10 = 4. B (2 ≤ 4) finishes → 7; C (6 ≤ 7) → 9; D (5 ≤ 9) → 10; A → 14. Safe; safe sequence B, C, D, A (B, A, C, D works too).
If C is given 3, only 1 is left free; even the smallest need (B: 2) cannot be met → unsafe. The request is denied, C is made to wait. The resource is actually free yet the request is postponed: that is the price of avoidance.
Step-by-Step Trace · Avoidance
Question 11: Safety Algorithm
There are 10, 6 and 8 instances of types A, B, C in total. Allocation and Max are in the table. Is the state safe? Rows are scanned in order starting from P0.
Total (10 6 8) − allocations (7 4 6) = Available (3 2 2); Need = Max − Allocation. Start with Work = Available.
P0: Need (4 2 1) ≤ Work (3 2 2) fails (not enough of type A) → skip for now.
P1: Need (1 2 1) ≤ Work ✓ → finishes, returns its allocation: Work = (5 2 3).
P2: Need (4 0 2) ≤ Work ✓ → finishes, returns its allocation: Work = (8 3 3).
P3: Need (2 2 1) ≤ Work ✓ → finishes, returns its allocation: Work = (8 5 4).
P4: Need (4 2 1) ≤ Work ✓ → finishes, returns its allocation: Work = (9 5 6).
P0: Need (4 2 1) ≤ Work ✓ → finishes, returns its allocation: Work = (10 6 8). Safe sequence: P1, P2, P3, P4, P0.
Work = Available; Finish[i] = false; while (Finish[i] == false and Need[i] <= Work for some i) { Work = Work + Allocation[i]; Finish[i] = true; } /* safe if all are true */ safe = (all Finish true);
Output Question · Avoidance
Question 12: Two Requests
There are 10, 6 and 8 instances of types A, B, C in total; Available = (3 2 2). Allocation, Max and Need are in the table.
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | (1 1 2) | (5 3 3) | (4 2 1) |
| P1 | (2 0 1) | (3 2 2) | (1 2 1) |
| P2 | (3 1 0) | (7 1 2) | (4 0 2) |
| P3 | (0 2 1) | (2 4 2) | (2 2 1) |
| P4 | (1 0 2) | (5 2 3) | (4 2 1) |
(a) If P3 requests (1 1 0), is the request granted immediately? (b) Instead, if P4 requests only (0 1 0), is it granted?
Show answer
(a) Granted. Request ≤ Need (2 2 1) and ≤ Available. Pretend it is granted: Available (2 1 2), P3’s need (1 1 1). P0, P1, P2 cannot be satisfied, but P3 can → Work (3 4 3); then P1 (5 4 4), P2 (8 5 4), P4 (9 5 6), P0 (10 6 8). Safe sequence P3, P1, P2, P4, P0.
(b) Not granted, P4 is made to wait. Available becomes (3 1 2), P4’s need (4 1 1): P0, P1, P3 want 2 of type B (1 free), P2 and P4 want 4 of type A (3 free). No row can be satisfied → unsafe.
Even a small request can be denied: what matters is not the amount but whether a sequence remains in which everyone can finish after the request.
Multiple Choice · Avoidance
Question 13: Unsafe State
Which statement is true of a system in an unsafe state according to the banker’s algorithm?
B: In an unsafe state there is no sequence that guarantees completion if processes make their maximum requests; but they may not, and deadlock may never occur. Deadlocked states are a subset of unsafe states. Avoidance never enters an unsafe state; terminating a process is recovery.
Multiple Choice · Prevention
Question 14: Printer Spooling
Instead of accessing the printer directly, processes write their output to a spool directory on disk; only the spooling daemon uses the printer. Which condition does this design eliminate for the printer?
A: To processes the printer is now shareable: everyone can submit jobs at once, and only a single daemon uses the real device. Limit: the spool disk is also a resource; if it fills with half-finished output, deadlock can still occur.
Multiple Choice · Prevention
Question 15: Resource Numbering
Resources are numbered: 1 scanner, 2 printer, 3 plotter, 4 tape drive, 5 Blu-ray. A process may request only a resource numbered higher than any it holds (releasing none). Which request sequence obeys this rule?
D: 2 → 4 → 5 is strictly increasing. In A the printer (2) is requested while holding the tape drive (4), in B the scanner (1) while holding the plotter (3), in C the plotter (3) while holding Blu-ray (5). The rule breaks cycles: a process holding the highest-numbered resource in a cycle cannot request a lower-numbered one.
Classic Question · Other Problems
Question 16: Classify the Fault
Say whether each situation is deadlock, livelock, starvation or an atomicity violation, give the reason, and suggest a remedy.
(1) Two people meeting in a narrow corridor keep stepping to the same side at the same time.
(2) A low-priority backup job has been unable to run for days because high-priority jobs keep arriving.
(3) Two bank transactions each wait for the account record the other has locked.
(4) Two consumers of a thread-safe queue each run if (!q.isEmpty()) x = q.remove(); ; an exception occurs now and then.
Show answer
- (1) Livelock: both are active and changing state but there is no progress. Remedy: wait a random time and retry.
- (2) Starvation: the system progresses, only this job is postponed indefinitely. Remedy: aging or FIFO.
- (3) Deadlock: a circular wait, both blocked. Remedy: always lock records in the same order, or roll back one of the transactions.
- (4) Atomicity violation: each call is safe by itself, but between them the other consumer may take the last item. Remedy: a single atomic operation (
poll()) or put both under the same lock.
Multiple Choice · Deadlock in Practice
Question 17: synchronized in Opposite Order
What will the program most likely do? (pause: Thread.sleep wrapper helper)
Object a = new Object(); Object b = new Object(); Thread t1 = new Thread(() -> { synchronized (a) { pause(50); synchronized (b) { System.out.println("T1"); } } }); Thread t2 = new Thread(() -> { synchronized (b) { pause(50); synchronized (a) { System.out.println("T2"); } } }); t1.start(); t2.start();
C: While T1 holds a and T2 holds b they sleep 50 ms; on waking each requests the other’s lock and both stay BLOCKED (the program did not finish in five runs). JVM monitors are non-preemptible; jstack reports the deadlock but does not resolve it. Fix: acquire locks in the same order everywhere.