Deadlock conditions and resource-allocation graphs
Deadlock conditions and resource-allocation graphs
Recorded by Dr. Krithika L.B. Slot m2-deadlock-rag
When processes wait for each other forever: the four necessary conditions, reading resource-allocation graphs, and preventing, avoiding (Banker's algorithm), detecting and recovering from deadlock.
By the end of this session you should be able to:
A system has a finite number of resource types R1 … Rm (CPU cycles, memory, files, I/O devices, locks), each with Wi identical instances. A process uses a resource in three steps: request (wait if not available) → use → release.
A set of processes is deadlocked when every process in the set is waiting for an event (usually the release of a resource) that can be caused only by another process in the set.
/* Thread 1 */ /* Thread 2 */
pthread_mutex_lock(&A); pthread_mutex_lock(&B);
pthread_mutex_lock(&B); /* waits for T2 */ pthread_mutex_lock(&A); /* waits for T1 */
... ...Deadlock can arise only if all four hold simultaneously (Coffman conditions):
At least one resource is non-sharable: only one process can use it at a time.
A process holds at least one resource while waiting for others.
A resource can be released only voluntarily by the process holding it.
A chain P0 → P1 → … → Pn → P0, each waiting for a resource held by the next.
Vertices are processes (circles) and resource types (rectangles with one dot per instance). A request edge Pi → Rj means Pi is waiting for Rj; an assignment edge Rj → Pi means an instance is held by Pi.
Cycle P1 → R2 → P2 → R1 → P1, and every resource has a single instance → deadlock.
Three broad approaches: prevent or avoid deadlock so it never happens; detect and recover after it happens; or ignore the problem (the “ostrich algorithm”, used by most general-purpose OSes including Linux and Windows, leaving it to application developers).
| Condition | How to deny it | Cost |
|---|---|---|
| Mutual exclusion | Make resources sharable (read-only files) | Impossible for printers, mutex locks |
| Hold and wait | Request all resources at once, or release everything before requesting more | Low utilisation, possible starvation |
| No preemption | If a request fails, release (preempt) what you hold and retry later | Works only for resources whose state can be saved (CPU, memory) |
| Circular wait | Number all resource types; request them only in increasing order | The most practical — lock ordering |
Avoidance needs a priori information: each process declares the maximum number of each resource it may need. Before granting any request, the OS checks that the resulting state is safe.
A state is safe if there is a safe sequence <P1, …, Pn> such that each Pi's remaining need can be satisfied by the currently available resources plus those held by all Pj with j < i. Safe ⇒ no deadlock. Unsafe ⇒ deadlock is possible (not certain). Avoidance = never enter an unsafe state.
Data structures (n processes, m resource types): Available[m], Max[n][m],
Allocation[n][m], and Need[n][m] = Max − Allocation.
Safety algorithm. Let Work = Available and Finish[i] = false for all i.
Find an i with Finish[i] == false and Need[i] ≤ Work. If none exists, go to step 4.
Pretend Pi runs to completion and releases its resources: Work = Work + Allocation[i], Finish[i] = true. Go to step 2.
If Finish[i] == true for all i, the state is safe; the order in which processes were chosen is a safe sequence.
Resource-request algorithm for Pi asking for Request[i]: if Request > Need[i] → error (exceeded its maximum claim).
If Request > Available → Pi must wait.
Pretend to allocate: Available −= Request, Allocation[i] += Request, Need[i] −= Request. Run the safety algorithm. Safe → grant; unsafe → restore the old state and make Pi wait.
Allocation: P0 (0 1 0), P1 (2 0 0), P2 (3 0 2), P3 (2 1 1), P4 (0 0 2). Max: P0 (7 5 3), P1 (3 2 2), P2 (9 0 2), P3 (2 2 2), P4 (4 3 3). Available = (3 3 2).
Need = Max − Allocation: P0 (7 4 3), P1 (1 2 2), P2 (6 0 0), P3 (0 1 1), P4 (4 3 1).
Scanning in order: P1 fits → Work (5 3 2); P3 → (7 4 3); P4 → (7 4 5); P0 → (7 5 5); P2 → (10 5 7). Safe sequence <P1, P3, P4, P0, P2> (the textbook's <P1, P3, P4, P2, P0> is also safe — several sequences can exist). Press Step below to watch it.
If neither prevention nor avoidance is used, the system can let deadlocks happen, detect them periodically and recover.
Request
matrix instead of Need; processes left with Finish = false are deadlocked.Abort all deadlocked processes (expensive — work is lost), or abort them one at a time until the cycle disappears (re-run detection after each). Choose victims by priority, work done, resources held, and how many must be killed.
Take resources away from some processes: select a victim (minimum cost), roll back it to a safe state (or restart it), and prevent starvation by counting rollbacks in the cost.
Deadlock conditions and resource-allocation graphs
Recorded by Dr. Krithika L.B. Slot m2-deadlock-rag
Banker's algorithm step by step
Recorded by Dr. Krithika L.B. Slot m2-bankers
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0Circular wait — no cycle can form if everyone requests resources in the same increasing order.
Not necessarily. If the second instance is held by a process outside the cycle that can finish and release it, the cycle is broken.
Request ≤ Need (1 2 2) and ≤ Available (3 3 2). New Available = (2 3 0); the safety check finds <P1, P3, P4, P0, P2> → safe → grant.
No. Unsafe means the OS can no longer guarantee that deadlock will be avoided; processes may still release resources early.
Any two of: priority, how long it has run / how much remains, resources held, resources still needed, number of processes to terminate, interactive vs batch.
Deadlock is what happens when processes share resources carelessly. Module 3 is about sharing carefully: how processes communicate and synchronise without races or deadlocks.
Go to Module 3 — Session 3.1: Inter-process communication →