Skip to main content

Operating Systems

Deadlock

Deadlock questions are formula-and-trace questions: know the four conditions, how each prevention method breaks one of them, and be able to run the Banker’s safety algorithm on a small table without mistakes.

~5 hrs ~1–1.5 marks · High 8 samples · 8 practice · 8 cards

GATE CSE deadlock — Coffman conditions, resource-allocation graphs, prevention, avoidance with the Banker’s algorithm and safe states, detection with wait-for graphs, recovery and the minimum-resources formula, with worked examples, samples and drills.

Learning outcomes

  • State the four Coffman conditions and show how prevention methods negate them
  • Read a resource-allocation graph and decide when a cycle means deadlock
  • Compute Need = Max - Allocation and find a safe sequence with the Banker’s algorithm, including a resource request check
  • Use the n(m-1)+1 style bound for the minimum resources that make a system deadlock-free

Deadlock conditions and handling strategies

  • A set of processes is in DEADLOCK when every process waits for an event (usually the release of a resource) that only another process in the set can cause. Resources may be reusable (CPU, memory, files, locks) or consumable (messages, signals).
  • The four COFFMAN conditions must hold simultaneously: (1) MUTUAL EXCLUSION — a resource is held in a non-sharable mode; (2) HOLD AND WAIT — a process holds at least one resource while waiting for others; (3) NO PREEMPTION — resources cannot be forcibly taken away; (4) CIRCULAR WAIT — a cycle P0 → P1 → ... → Pn → P0 of processes each waiting for a resource held by the next.
  • Handling methods: PREVENTION (ensure at least one condition can never hold), AVOIDANCE (grant a request only if the system stays in a safe state; needs advance information about maximum needs), DETECTION AND RECOVERY (allow deadlocks, detect and fix them), and IGNORE (the “ostrich” approach used by most general-purpose operating systems).
  • PREVENTION details: mutual exclusion cannot be removed for intrinsically non-sharable resources. Negate HOLD AND WAIT by requesting all resources at once or releasing everything before a new request (low utilization, starvation). Negate NO PREEMPTION by taking resources away from a waiting process. Negate CIRCULAR WAIT by imposing a total ORDER on resource types and requiring requests in increasing order (the most practical method).
  • Deadlock is different from STARVATION: starvation is indefinite postponement of a process in an otherwise progressing system; deadlock is a permanent circular wait among a set of processes.
  • A deadlock requires at least two processes. A single process cannot be in a deadlock unless it waits on itself (for example acquiring a non-recursive lock twice).
  • Livelock: processes keep changing state in response to each other but make no progress; unlike deadlock they are not blocked.
  • Deadlock and the OS: the cost of prevention and avoidance is why UNIX/Windows mostly leave deadlock handling to the application programmer.
  • The conditions are NECESSARY, not sufficient: all four can be true in a state that later resolves (multiple instances). But if even one is false, deadlock is impossible. Typical GATE statement: “which of the following conditions, if removed, prevents deadlock?” — any of the four.
  • Relationship of states: SAFE states ⊂ all states; UNSAFE states contain all DEADLOCKED states but also some states that never deadlock. Avoidance never enters an unsafe state; detection lets the system enter any state and repairs afterwards.
  • Cost of the methods: prevention lowers resource utilization and throughput, avoidance needs maximum claims in advance and a fixed number of processes/resources, detection needs a periodic O(m × n^2) algorithm plus a recovery policy.
  • Preventing hold-and-wait can starve a process that needs a popular resource; preventing circular wait by ordering requires that every process request resources in increasing order of their numbers, so a process wanting R3 after R5 must release R5 first.
ConditionPrevention idea
Mutual exclusionmake resources sharable (read-only files); not always possible
Hold and waitrequest all resources at the start or release before requesting
No preemptionpreempt resources of a waiting process, restart it later
Circular waitimpose a global order on resource types

Worked example

Problem. Two processes lock two mutexes A and B. P1 locks A then B; P2 locks B then A. Which Coffman condition can be removed most easily to prevent deadlock?

Solution. Circular wait: if both processes lock in the same global order (A before B), a cycle is impossible. Mutual exclusion cannot be removed for a mutex, and the other methods (requesting both at once, preemption) are costlier.

Four boxes for mutual exclusion, hold and wait, no preemption and circular wait with a prevention idea for each, and a two-lock deadlock example.
All four Coffman conditions must hold together. Removing any one prevents deadlock; global lock ordering removes circular wait.

Resource-allocation graph, safe states and the Banker’s algorithm

  • RESOURCE-ALLOCATION GRAPH (RAG): vertices are processes and resource types; a request edge P → R means P waits for R, an assignment edge R → P means an instance of R is allocated to P. A claim edge (dashed) shows a possible future request.
  • With SINGLE-INSTANCE resources a cycle in the RAG means DEADLOCK (necessary and sufficient). With MULTIPLE instances a cycle is necessary but NOT sufficient: if a process outside the cycle can release an instance, the cycle may break.
  • SAFE STATE: there exists a SAFE SEQUENCE <P1, ..., Pn> in which each Pi can finish using the currently available resources plus those held by all earlier Pj (j < i). Safe → no deadlock. Unsafe → deadlock POSSIBLE (not certain). Avoidance keeps the system in safe states.
  • BANKER’S ALGORITHM data (n processes, m resource types): Available[m], Max[n][m], Allocation[n][m], Need[n][m] = Max - Allocation. Safety algorithm: Work = Available, Finish[i] = false; repeatedly pick an i with Finish[i] = false and Need[i] ≤ Work; set Work = Work + Allocation[i] and Finish[i] = true. If all finish, the state is safe. Complexity O(m × n^2).
  • RESOURCE-REQUEST algorithm: if Request[i] > Need[i] → error; if Request[i] > Available → process must wait; otherwise PRETEND to allocate: Available -= Request, Allocation[i] += Request, Need[i] -= Request, and run the safety algorithm. If safe, grant; otherwise restore the old state and make the process wait.
  • DETECTION: for single-instance resources build a WAIT-FOR GRAPH (remove resource nodes from the RAG) and look for cycles, O(n^2). For multiple instances use the Banker-like detection algorithm with Request in place of Need and with no Max.
  • RECOVERY: terminate all deadlocked processes, or terminate one at a time until the cycle disappears (choose by priority, run time, resources held), or PREEMPT resources with rollback; avoid starvation by limiting how often the same victim is chosen.
  • Minimum resources for deadlock freedom: if n processes each need at most m instances of a single resource type, deadlock cannot occur when R ≥ n(m - 1) + 1. (For different needs m_i use R ≥ sum(m_i - 1) + 1.)
  • Reading a RAG quickly: (1) any process with no outgoing request edge is not waiting and will finish; (2) remove it and its assignment edges, freeing instances; (3) repeat. If every remaining process is waiting and no instance can be freed, those processes are deadlocked (graph reduction).
  • Safe-state test for a single resource type is a sort: list Need in increasing order and check Need[i] ≤ Available + (sum of Allocation of the earlier processes). A request is granted only if the state after the pretend allocation is still safe.

Formulas to remember

  • Need[i][j] = Max[i][j] - Allocation[i][j]
  • Deadlock-free if R ≥ n(m - 1) + 1 (each of n processes needs at most m units)
  • Safety algorithm cost = O(m × n^2)

Worked example

Problem. Banker’s: 5 processes, resource types A, B, C with totals 10, 5, 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). Is the state safe?

Solution. Available = total - sum(Allocation) = (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). Safety: P1 (1,2,2) ≤ (3,3,2) → Work (5,3,2); P3 (0,1,1) → Work (7,4,3); P4 (4,3,1) → Work (7,4,5); P2 (6,0,0) → Work (10,4,7); P0 (7,4,3) → Work (10,5,7). Safe sequence <P1, P3, P4, P2, P0>. Request (1,0,2) by P1: it is ≤ Need (1,2,2) and ≤ Available (3,3,2); after pretending, Available = (2,3,0), P1 Need (0,2,0); P1 can finish, then P3, P4, P0, P2 can too → safe, so the request is granted.

Two resource-allocation graphs: a cycle with single-instance resources that is a deadlock, and a cycle with a two-instance resource where a third process can finish so there is no deadlock.
Cycle + single instances = deadlock. Cycle with multiple instances is only a possibility: check whether a process outside the cycle can finish.

Banker’s algorithm: worked matrices and request checks

  • Procedure to follow every time: (1) Available = total - column sums of Allocation; (2) Need = Max - Allocation; (3) run the safety algorithm; (4) for a request: Request ≤ Need? Request ≤ Available? pretend-allocate and re-run safety.
  • The classic 5-process example (A, B, C totals 10, 5, 7) has Available (3,3,2) and Need P0 (7,4,3), P1 (1,2,2), P2 (6,0,0), P3 (0,1,1), P4 (4,3,1). The safe sequence <P1, P3, P4, P2, P0> gives Work (5,3,2), (7,4,3), (7,4,5), (10,4,7), (10,5,7). Final Work equals the total, a good arithmetic check.
  • Safe sequences are not unique; <P3, P1, P4, P2, P0> is also safe. GATE options usually list several sequences: test each by checking that Need fits Work at every step.
  • A request that is ≤ Need and ≤ Available can still be DENIED because it leads to an unsafe state; a request that exceeds Available simply makes the process wait; a request that exceeds Need is an error (exceeds the declared maximum).
  • Second example, two resource types A, B with totals (6, 4): P0 Alloc (1,1) Max (3,2); P1 (2,1) (4,2); P2 (1,1) (2,2); P3 (0,0) (3,2). Available = (6,4) - (4,3) = (2,1). Need: P0 (2,1), P1 (2,1), P2 (1,1), P3 (3,2). Safety from Work (2,1): P0 → (3,2), P1 → (5,3), P2 → (6,4), P3 fits. Safe.
  • Request (2,1) by P3: it is ≤ Need (3,2) and ≤ Available (2,1). Pretend: Available becomes (0,0); the remaining Needs are P0 (2,1), P1 (2,1), P2 (1,1), P3 (1,1), none fits in (0,0), so the state is UNSAFE and the request must wait. Request (1,1) by P2 instead: Available (1,0), P2 Need (0,0) can finish → Work (3,2) → all others finish: granted.
ProcessAllocation (A,B)Max (A,B)Need (A,B)
P0(1,1)(3,2)(2,1)
P1(2,1)(4,2)(2,1)
P2(1,1)(2,2)(1,1)
P3(0,0)(3,2)(3,2)
Available(2,1)Total (6,4)

Worked example

Problem. In the two-resource state above, may P3’s request for (2,1) be granted? What about P0’s request for (1,0)?

Solution. P3 (2,1): pretend allocation leaves Available (0,0) and no process has Need ≤ (0,0), so the state is unsafe → DENY. P0 (1,0): ≤ Need (2,1) and ≤ Available (2,1); afterwards Available = (1,1), P0 Need = (1,1). P0 can finish (needs ≤ (1,1)) → Work (1,1) + Alloc P0 (2,1) = (3,2); then P1 (2,1) → (5,3); P2 (1,1) → (6,4); P3 (3,2) fits. Safe → GRANT.

Allocation, Max and Need matrices for five processes and three resource types with Available (3,3,2) and the safe sequence P1, P3, P4, P2, P0 with the Work vector after each step.
Banker’s safety run: Work = Available, then repeatedly pick a process whose Need fits in Work and add back its Allocation.

Counting resources and exam patterns

  • For a single resource type, to test safety quickly: sort processes by Need and check whether each can complete with Available plus the resources released so far.
  • Minimum R for deadlock freedom example: 3 processes each need at most 4 units: R ≥ 3 × (4 - 1) + 1 = 10. Maximum R that can still deadlock = 9 (each holds 3 and waits for the 4th).
  • Worst-case reasoning: deadlock needs every process to hold (max - 1) units and all to wait for one more; if even one extra unit exists, some process can complete.
  • Unsafe state example: 12 units; P0 max 10 holds 5, P1 max 4 holds 2, P2 max 9 holds 2 (3 free). Safe sequence <P1, P0, P2>. If the OS gives P2 one more unit, Available = 2 and no process can be guaranteed to finish: UNSAFE.
  • In a RAG with multiple instances of each resource, always check whether a process outside the cycle can finish and release resources before declaring deadlock.
  • Deadlock and mutex/semaphore: two processes doing wait(A) wait(B) and wait(B) wait(A) can deadlock; consistent ordering prevents it.
  • Detection run frequency: after each request (high overhead) or when CPU utilization drops below a threshold.
  • GATE favourites: number of processes that can be deadlocked, minimum number of resources, whether a given request can be granted, and which sequence is safe.
  • Different maximum needs: processes needing at most 2, 3 and 4 units of one resource cannot deadlock if R ≥ (2-1) + (3-1) + (4-1) + 1 = 7; with R = 6 each can hold (1, 2, 3) and all wait for one more, which is deadlock.
  • Largest number of processes that can share R units without any chance of deadlock when each needs at most m units: n ≤ (R - 1) / (m - 1). For R = 7 and m = 3 this gives n ≤ 3; with n = 4 the allocation 2, 2, 2, 1 uses all 7 units and every process still wants more, which is a deadlock.
  • Detection with wait-for graph: Pi → Pj when Pi waits for a resource held by Pj. Look for a cycle; with several instances use the matrix algorithm (Available, Allocation, Request) and mark processes whose Request ≤ Work as able to finish; the unmarked processes at the end are deadlocked.
  • Recovery victim selection: lowest priority, least work done, fewest resources held, fewest rollbacks needed; include the number of times a process was already chosen to avoid starvation.

Worked example

Problem. A system has 12 units of one resource. Allocation/Max: P0 5/10, P1 2/4, P2 2/9. Find a safe sequence and decide whether granting P2 one more unit keeps the state safe.

Solution. Available = 12 - 9 = 3. Need: P0 5, P1 2, P2 7. P1 (2 ≤ 3) finishes → Available 5; P0 (5 ≤ 5) finishes → Available 10; P2 (7 ≤ 10) finishes. Safe: <P1, P0, P2>. If P2 receives 1 more, Available = 2 and Need becomes P0 5, P1 2, P2 6. P1 finishes → Available 4; now P0 needs 5 and P2 needs 6, both > 4 → no sequence: UNSAFE, so the request must be delayed.

Exam tips

  • List the four conditions as MHNC: Mutual exclusion, Hold and wait, No preemption, Circular wait — ALL must hold for deadlock.
  • Cycle in a single-instance RAG = deadlock; cycle with multiple instances = only a possibility.
  • Banker’s: compute Need = Max - Allocation first, then repeatedly find a process with Need ≤ Work.
  • Minimum resources for deadlock freedom: n(m-1)+1 (equal maximum need m for n processes).
  • Unsafe does not mean deadlocked; safe means no deadlock for sure.
Exam pattern · 8 questions

GATE sample questions & answers

8 GATE-style MCQs for Deadlock with full explanations. Attempt each question before revealing the answer.

Q1

Which one of the following is NOT a necessary condition for deadlock?

  • A) mutual exclusion
  • B) hold and wait
  • C) preemption
  • D) circular wait
Show answer & explanation

Answer: C

The necessary condition is NO preemption. If resources can be preempted, deadlock cannot persist.

Q2 GATE n(m-1)+1 pattern

A system has three processes, each needing at most 4 units of a resource of one type. The minimum number of units that guarantees deadlock freedom is:

  • A) 7
  • B) 9
  • C) 10
  • D) 12
Show answer & explanation

Answer: C

R ≥ n(m - 1) + 1 = 3 × 3 + 1 = 10. With 9 units each process could hold 3 and wait for a 4th.

Q3

Available = (3,3,2) and Need rows P0 (7,4,3), P1 (1,2,2), P2 (6,0,0), P3 (0,1,1), P4 (4,3,1) with Allocation P0 (0,1,0), P1 (2,0,0), P2 (3,0,2), P3 (2,1,1), P4 (0,0,2). Which is a safe sequence?

  • A) P0, P1, P2, P3, P4
  • B) P1, P3, P4, P2, P0
  • C) P4, P3, P2, P1, P0
  • D) P2, P0, P1, P3, P4
Show answer & explanation

Answer: B

Only P1 or P3 can start with Available (3,3,2). P1 → Work (5,3,2), P3 → (7,4,3), P4 → (7,4,5), P2 → (10,4,7), P0 → (10,5,7). The other sequences begin with a process whose Need exceeds Available.

Q4

In a resource-allocation graph where every resource type has exactly one instance, a cycle indicates:

  • A) a safe state
  • B) a possible deadlock
  • C) a definite deadlock
  • D) starvation only
Show answer & explanation

Answer: C

With single-instance resources, every process in the cycle waits for a resource held by another process in the cycle: deadlock.

Q5

Imposing a total order on resource types and requiring processes to request them in increasing order prevents which condition?

  • A) mutual exclusion
  • B) hold and wait
  • C) no preemption
  • D) circular wait
Show answer & explanation

Answer: D

If all requests follow one global order, no cycle of waiting processes can form.

Q6

A wait-for graph is used for deadlock detection when:

  • A) every resource has multiple instances
  • B) every resource type has a single instance
  • C) preemption is allowed
  • D) the Banker’s algorithm is in use
Show answer & explanation

Answer: B

The wait-for graph is derived from the RAG by deleting resource nodes; it is valid for single-instance resources, and a cycle means deadlock.

Q7

Which of the following statements is correct?

  • A) an unsafe state is always a deadlock
  • B) a safe state can lead to deadlock
  • C) an unsafe state may or may not lead to deadlock
  • D) a deadlock state is always safe
Show answer & explanation

Answer: C

Safe means a completion order exists, so deadlock cannot occur. Unsafe only means deadlock is possible; a deadlock state is a subset of unsafe states.

Q8

With the Banker’s algorithm state above (Available (3,3,2)), P1 requests (1,0,2). The request:

  • A) must be denied because it exceeds Available
  • B) must be denied because it exceeds Need
  • C) can be granted, since the resulting state is safe
  • D) can be granted only after P0 finishes
Show answer & explanation

Answer: C

Request (1,0,2) ≤ Need (1,2,2) and ≤ Available (3,3,2). After pretending, Available (2,3,0) still allows the safe sequence P1, P3, P4, P0, P2.

Topic drill · 8 questions

8 practice questions — Deadlock

Extra page-specific practice beyond the GATE samples. Attempt all 8, then reveal answers to check your reasoning.

P1

Four processes each need at most 3 units of a single resource. The minimum number of units that ensures freedom from deadlock is:

  • A) 8
  • B) 9
  • C) 12
  • D) 13
Show answer & explanation

Answer: B) 9

R = 4 × (3 - 1) + 1 = 9.

P2

A process has Max = 7 units and currently holds Allocation = 3 units. Its Need is:

  • A) 3
  • B) 4
  • C) 7
  • D) 10
Show answer & explanation

Answer: B) 4

Need = Max - Allocation = 7 - 3 = 4.

P3

12 units; P0 holds 5 (max 10), P1 holds 2 (max 4), P2 holds 2 (max 9). Which is a safe sequence?

  • A) P0, P1, P2
  • B) P2, P0, P1
  • C) P1, P0, P2
  • D) P0, P2, P1
Show answer & explanation

Answer: C) P1, P0, P2

Available = 3. P1 (need 2) → 5; P0 (need 5) → 10; P2 (need 7) → done.

P4

In the same system, if P2 is granted one additional unit, the state becomes:

  • A) still safe
  • B) unsafe
  • C) deadlocked immediately
  • D) invalid
Show answer & explanation

Answer: B) unsafe

Available becomes 2. P1 can finish and release (Available 4), but then P0 needs 5 and P2 needs 6, neither of which can be satisfied.

P5

Requesting all resources before starting execution prevents which Coffman condition?

  • A) mutual exclusion
  • B) hold and wait
  • C) no preemption
  • D) circular wait
Show answer & explanation

Answer: B) hold and wait

A process never holds some resources while waiting for others, because it obtains everything at once.

P6

A deadlock detection algorithm for single-instance resources checks for:

  • A) a cycle in the wait-for graph
  • B) an unsafe state
  • C) a negative Need value
  • D) a process with zero allocation
Show answer & explanation

Answer: A) a cycle in the wait-for graph

A cycle in the wait-for graph is necessary and sufficient for deadlock with single-instance resources.

P7

A system with 3 processes each needing at most 2 units of a single resource type can deadlock only if the number of units is at most:

  • A) 1
  • B) 2
  • C) 3
  • D) 4
Show answer & explanation

Answer: C) 3

Deadlock freedom needs R ≥ 3 × (2 - 1) + 1 = 4, so the largest R that can still deadlock is 3: each process holds 1 unit and waits for a second one.

P8

Which one of the following recovery methods can avoid terminating any process?

  • A) abort all deadlocked processes
  • B) abort processes one at a time
  • C) resource preemption with rollback
  • D) ignoring the deadlock
Show answer & explanation

Answer: C) resource preemption with rollback

Preemption takes resources away from a victim and rolls it back to a safe point; no process needs to be permanently terminated.

Topic FAQs

What are the necessary conditions for deadlock?

Mutual exclusion, hold and wait, no preemption and circular wait. All four must hold at the same time; breaking any one prevents deadlock.

What is the difference between a safe state and a deadlock?

A safe state has at least one sequence in which all processes can finish; no deadlock can occur from it. An unsafe state may lead to deadlock but does not have to. A deadlock state is always unsafe.

How do I compute the minimum number of resources for deadlock freedom?

If n processes each need at most m units, the worst case is every process holding m - 1 units. So R = n(m - 1) + 1 units guarantee that some process can always complete.

Does a cycle in a resource-allocation graph always mean deadlock?

Only when each resource type has a single instance. With multiple instances the cycle is necessary but not sufficient, so you must check whether other processes can release resources.

Quick revision · 8 cards

8 flashcards — Deadlock

Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.

Q1 Four Coffman conditions?
Q2 Which prevention method is most practical?
Q3 Safe state definition?
Q4 Need matrix formula?
Q5 Minimum resources for no deadlock (n processes, max need m each)?
Q6 Cycle in RAG: single vs multiple instances?
Q7 Time complexity of the Banker’s safety algorithm?
Q8 Deadlock vs starvation?