CSC 394 · Operating Systems
Our notes
2 min readWhat a deadlock is
A set of processes is deadlocked when each is waiting for a resource held by another in the set, so none can continue. Example: P1 holds the printer and wants the scanner; P2 holds the scanner and wants the printer.
Four necessary conditions (Coffman)
All four must hold at once:
- Mutual exclusion: a resource can be used by only one process at a time.
- Hold and wait: a process holds resources while waiting for more.
- No preemption: resources cannot be taken away; they are released voluntarily.
- Circular wait: P1 waits for P2, P2 for P3, …, Pn for P1.
Resource-allocation graph
Circles are processes, boxes are resource types (dots = instances). Request edge P → R; assignment edge R → P. No cycle → no deadlock. A cycle means deadlock if every resource has one instance; with several instances a cycle means deadlock is possible.
Handling deadlocks
- Prevention: break one condition: allow sharing where possible; request all resources at the start (no hold and wait); allow preemption; number resource types and request them only in increasing order (no circular wait).
- Avoidance: check each request and grant it only if the system stays in a safe state (Banker's algorithm).
- Detection and recovery: let deadlocks happen, detect them, then recover by ending processes or taking resources back (rollback).
- Ignore (the "ostrich algorithm"): used by most desktop OSs because deadlocks are rare.
Banker's algorithm (worked)
5 processes, 3 resource types A, B, C with totals A = 10, B = 5, C = 7.
| Allocation (A B C) | Max (A B C) | Need = Max − Allocation | |
|---|---|---|---|
| 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 |
Allocated: A 7, B 2, C 5 → Available = (3, 3, 2).
Safety check (find a process whose Need ≤ Work, let it finish, add its Allocation to Work):
- Work (3,3,2): P1 needs (1,2,2) ✓ → Work = (3,3,2) + (2,0,0) = (5,3,2).
- P3 needs (0,1,1) ✓ → Work = (7,4,3).
- P4 needs (4,3,1) ✓ → Work = (7,4,5).
- P0 needs (7,4,3) ✓ → Work = (7,5,5).
- P2 needs (6,0,0) ✓ → Work = (10,5,7).
All finish: safe sequence ⟨P1, P3, P4, P0, P2⟩.
A request: P1 asks for (1,0,2). It is ≤ its Need (1,2,2) and ≤ Available (3,3,2). Pretend to grant: Available = (2,3,0), P1 Allocation (3,0,2), Need (0,2,0). Check: P1 (0,2,0) ✓ → (5,3,2); P3 ✓ → (7,4,3); P4 ✓ → (7,4,5); P0 ✓ → (7,5,5); P2 ✓ → (10,5,7). Still safe → grant it.
Detection and recovery
With single-instance resources, look for cycles in a wait-for graph. Recovery: abort all deadlocked processes, or one at a time (cheapest first) until the cycle breaks; or preempt resources and roll a process back.
Exams usually ask: define deadlock; the four conditions; draw and read a resource-allocation graph; prevention methods; run the Banker's algorithm (Need matrix, safe sequence, grant a request); deadlock vs starvation.
Common mistake: in the Banker's algorithm, forgetting to add the finished process's Allocation (not its Need or Max) back to Work.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick OSTEP: Common Concurrency Problems (deadlock)
Free chapter PDF from the authors' site (127 KB).
(opens pages.cs.wisc.edu in a new tab)
-
Deadlock (computer science)
(opens en.wikipedia.org in a new tab)
Wikipedia, Deadlock (computer science), CC BY-SA 4.0 · licence
-
Banker's algorithm
(opens en.wikipedia.org in a new tab)
Wikipedia, Banker's algorithm, CC BY-SA 4.0 · licence
Watch
-
Lec19 Deadlock | Necessary conditions for Deadlock | Operating Systems
13 min · ≈ 59 MB · about ₦33
(opens youtube.com in a new tab)
-
Lec20 Resource Allocation Graph in Deadlock | with example | Operating System
25 min · ≈ 113 MB · about ₦63
(opens youtube.com in a new tab)
-
Lec23 Deadlock Avoidance: Banker's Algorithm with example | Operating System
21 min · ≈ 95 MB · about ₦53
(opens youtube.com in a new tab)
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.