Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 2.6: Deadlocks: characterization, resource-allocation graphs and handling methods

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.

Module 2 1.5 hours Theory CO2
← Before
2.5 Scheduling II
RR, SRTF, MLFQ
You are here
2.6 Deadlocks: characterization, resource-allocation graphs and handling methods
Module 2 · Process, Scheduling, Threads and Deadlocks
Next →
3.1 IPC
Module 3 begins

Learning objectives

By the end of this session you should be able to:

  • Define deadlock using the request–use–release system model.
  • State the four necessary conditions and show how each can be denied.
  • Draw and interpret resource-allocation graphs with single and multiple instances.
  • Apply the Banker's safety and resource-request algorithms to decide whether a request is granted.
  • Describe deadlock detection with a wait-for graph and the options for recovery.

Lecture notes

1. System model

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.

Definition

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.

Two threads, two locks — the classic deadlock
/* 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 */
...                                ...

2. Deadlock characterization — four necessary conditions

Deadlock can arise only if all four hold simultaneously (Coffman conditions):

1 · Mutual exclusion

At least one resource is non-sharable: only one process can use it at a time.

2 · Hold and wait

A process holds at least one resource while waiting for others.

3 · No preemption

A resource can be released only voluntarily by the process holding it.

4 · Circular wait

A chain P0 → P1 → … → Pn → P0, each waiting for a resource held by the next.

3. Resource-Allocation Graph (RAG)

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.

R1 R2 P1 P2 held by held by P1 requests R2 P2 requests R1

Cycle P1 → R2 → P2 → R1 → P1, and every resource has a single instance → deadlock.

Reading a RAG
  • No cycle → no deadlock.
  • Cycle, and each resource type has one instance → deadlock (a cycle is necessary and sufficient).
  • Cycle, but some resource has several instances → possibly deadlocked (necessary, not sufficient) — another process outside the cycle may release an instance and break it.

4. Methods for handling deadlocks

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).

4a. Deadlock prevention — break one of the four conditions

ConditionHow to deny itCost
Mutual exclusionMake resources sharable (read-only files)Impossible for printers, mutex locks
Hold and waitRequest all resources at once, or release everything before requesting moreLow utilisation, possible starvation
No preemptionIf a request fails, release (preempt) what you hold and retry laterWorks only for resources whose state can be saved (CPU, memory)
Circular waitNumber all resource types; request them only in increasing orderThe most practical — lock ordering

4b. Deadlock avoidance — safe states and the Banker's algorithm

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.

Safe state

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.

Worked example (Silberschatz) — 5 processes, resources A = 10, B = 5, C = 7

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.

Simulator — Banker's safety algorithmTry request “1: 1 0 2” (safe → granted) and “4: 3 3 0” (state becomes unsafe → P4 must wait).

4c. Deadlock detection

If neither prevention nor avoidance is used, the system can let deadlocks happen, detect them periodically and recover.

  • Single instance of each resource: collapse the RAG into a wait-for graph (Pi → Pj if Pi waits for a resource held by Pj). A cycle = deadlock. Cycle detection costs O(n²).
  • Several instances: run an algorithm like the safety algorithm, but using the current Request matrix instead of Need; processes left with Finish = false are deadlocked.
  • How often? Every request (expensive), at fixed intervals, or when CPU utilisation drops below a threshold.

4d. Recovery from deadlock

Process termination

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.

Resource preemption

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.

VideoBytes

Deadlock conditions and resource-allocation graphs

Video coming soon

Deadlock conditions and resource-allocation graphs

Recorded by Dr. Krithika L.B. Slot m2-deadlock-rag

Banker's algorithm step by step

Video coming soon

Banker's algorithm step by step

Recorded by Dr. Krithika L.B. Slot m2-bankers

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
Which condition does ordering lock acquisition (always lock A before B) break?

Circular wait — no cycle can form if everyone requests resources in the same increasing order.

A RAG has a cycle but one resource in it has two instances. Is the system deadlocked?

Not necessarily. If the second instance is held by a process outside the cycle that can finish and release it, the cycle is broken.

In the worked example, can P1's request (1, 0, 2) be granted?

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.

Is every unsafe state a deadlock?

No. Unsafe means the OS can no longer guarantee that deadlock will be avoided; processes may still release resources early.

Give two criteria for choosing a victim process during recovery.

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.

Key takeaways

  • Deadlock needs mutual exclusion, hold-and-wait, no preemption and circular wait — all four.
  • RAG: no cycle ⇒ no deadlock; cycle with single instances ⇒ deadlock.
  • Prevention breaks a condition; avoidance (Banker's) stays in safe states; detection + recovery fixes deadlocks after the fact.

Where this leads

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 →