IAE Academy

GATE CS

Deadlock

Conditions, prevention, avoidance and detection

Deadlock is a state in which every process in a set waits for a resource held by another process in the same set, so none can ever proceed. GATE questions test the four necessary conditions, resource-allocation and wait-for graphs, the Banker's algorithm (safety check and request check), the detection algorithm, and the counting result n(k - 1) + 1 for the minimum number of instances that rules out deadlock.

6 previous-year questions · 6 solved · 5 practice questions

Deadlock and its four necessary conditions

A set of processes is deadlocked when every process in the set is waiting for an event (here, the release of a resource) that can be caused only by another process in the same set. Nobody can release anything, so nobody ever moves. Resources here are non-shareable units that a process requests, uses, and releases; a request for a unit that is not free makes the process wait.

Conventions used in these notes: processes are numbered ; when several processes could be picked next in an algorithm, the one with the smallest index is picked (any valid order gives the same yes/no answer for safety and detection). Resource types are written and a type may have several identical instances.

A system can handle deadlock in four ways: prevention (make one necessary condition impossible), avoidance (grant a request only if the system stays safe), detection and recovery (let deadlock happen, find it, break it), or ignoring the problem and relying on a restart.

Deadlock can occur only if all four of the following hold at the same time. They are necessary, not sufficient: all four can hold in a system that is not currently deadlocked.

The four necessary conditions

Condition

Meaning

Broken by (prevention)

Mutual exclusion

At least one resource is held in a non-shareable mode; only one process can use it at a time.

Make resources shareable (read-only files); not possible for printers, locks, etc.

Hold and wait

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

Request all resources at once, or release everything before requesting more.

No preemption

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

Allow the system to take resources away from a waiting process.

Circular wait

There is a cycle where each process waits for a resource held by the next.

Impose a total order on resource types; request only in increasing order.

EXAM TIP

Circular wait implies hold and wait (each process in the cycle holds something and wants more), but circular wait is a stronger statement than hold and wait. When a question asks which condition a scheme breaks, match the scheme to the exact wording in the table.

Resource-allocation graphs

A resource-allocation graph (RAG) is a directed graph with process nodes and resource-type nodes . Each instance of is drawn as a dot inside its box.

  • Request edge : is waiting for an instance of .
  • Assignment edge : an instance of is allocated to .

What a cycle tells you

Situation

Cycle in the RAG means

No cycle means

Every resource type has exactly one instance

Deadlock (necessary and sufficient)

No deadlock

Some type has several instances

Deadlock is possible but not certain (necessary, not sufficient)

No deadlock

With several instances, a cycle alone does not settle the question. If an instance of a resource that a process on the cycle is waiting for is held by a process that can eventually finish (for example one outside the cycle that is not waiting for anything), that process will release it and the cycle can break.

A reliable way to test a multi-instance RAG: repeatedly pick a process whose pending requests can all be satisfied from currently free instances, let it finish, and release its allocations. If every process can be removed this way there is no deadlock; the processes left over are deadlocked. This is exactly the detection algorithm described later.

EXAM TIP

Trap: a cycle with multiple instances is NOT automatically a deadlock. Check whether a process outside the cycle holds an instance of a resource on the cycle and is not waiting for anything; if so, it can finish and release it, so there is no deadlock.

Deadlock prevention

Prevention designs the system so that at least one of the four necessary conditions can never hold. It needs no knowledge of future requests, but it often lowers resource utilisation and throughput.

Prevention by denying a condition

Denied condition

Technique

Cost or limitation

Mutual exclusion

Spool or share where the device allows it

Impossible for inherently non-shareable resources

Hold and wait

All-at-once: request every resource before starting; or release all held resources before requesting new ones

Low utilisation, starvation of a process that needs a popular resource, may not know the full set in advance

No preemption

If a request cannot be met, the process releases what it holds and restarts the request later; or the system takes resources from a waiting process

Only suits resources whose state can be saved and restored (CPU registers, memory), not printers or locks

Circular wait

Number all resource types; a process may request a resource only if its number is greater than that of every resource it already holds (all instances of one type in a single request)

Programmers must follow the order; some natural request patterns become awkward

Why resource ordering works. Suppose a circular wait existed, where holds and waits for (indices mod ). Under the ordering rule, waiting for while holding means . Chaining around the cycle gives , which is impossible.

EXAM TIP

The ordering rule restricts the order in which requests are made, not which resources a process may use. A process holding a high-numbered resource that needs a lower-numbered one must release the high one first.

Deadlock avoidance: safe states and the Banker's algorithm

Avoidance lets all four conditions be possible but grants a request only if the resulting state is safe. It needs each process to declare in advance its maximum claim for each resource type.

A state is safe if there exists a safe sequence : an order in which each process can, in turn, get all it may still need from what is free plus what the earlier processes in the sequence will have released, then finish. Otherwise the state is unsafe.

  • Safe state not deadlocked, and the system can keep it free of deadlock by granting only requests that leave the state safe.
  • Deadlock unsafe state.
  • Unsafe state does not mean deadlock; it only means deadlock may happen depending on the future requests.

Single instance of every type: the resource-allocation-graph algorithm. Besides request and assignment edges, draw a claim edge (dashed) for every resource may request in future; all claim edges are declared before starts. When requests , the claim edge becomes a request edge. The request is granted only if turning it into an assignment edge creates no cycle, with claim edges counted; otherwise waits. When some type has several instances, this test is not enough and the Banker's algorithm is used.

Banker's algorithm data for processes and resource types: the vector (length ), and matrices , and . Vector comparison means for every . The algorithm assumes a fixed number of processes and resource instances, maximum claims declared before a process starts, and that a process given all it needs finishes and releases everything in finite time.

NEED MATRIX

Remaining claim of process for resource type .

AVAILABLE VECTOR

Free instances of ; Total is the number of instances of in the system.

Safety algorithm

  1. Set and for all .

  2. Find a process with and . If none exists, go to step 4.

  3. Set and ; go back to step 2. The order in which processes are picked is the safe sequence.

  4. If for every , the state is safe; otherwise unsafe. The algorithm takes operations.

Resource-request algorithm (P_i requests Request_i)

  1. If in any component, raise an error: the process exceeded its declared maximum.

  2. If in any component, must wait: resources are not free.

  3. Otherwise pretend to grant: , , .

  4. Run the safety algorithm on the pretended state. If safe, the request is granted for real; if unsafe, restore the old state and make wait.

WORKED EXAMPLE

A system has resource types A, B, C with totals (8, 5, 6). Four processes have these allocations and maximum claims.

Allocation: = (1,0,2), = (2,1,1), = (1,2,0), = (2,0,1).
Max: = (3,2,4), = (4,2,2), = (3,4,1), = (3,1,2).
(a) Is the state safe? (b) Can 's request for (1,1,1) be granted immediately?

Solution
  1. Allocated in total: A = 1+2+1+2 = 6, B = 0+1+2+0 = 3, C = 2+1+0+1 = 4. So Available = (8-6, 5-3, 6-4) = (2,2,2).

  2. Need = Max - Allocation: = (2,2,2), = (2,1,1), = (2,2,1), = (1,1,1).

  3. Safety: Work = (2,2,2). : need (2,2,2) (2,2,2), run; Work = (2,2,2)+(1,0,2) = (3,2,4).

  4. : need (2,1,1) (3,2,4), run; Work = (3,2,4)+(2,1,1) = (5,3,5). : need (2,2,1) (5,3,5), run; Work = (5,3,5)+(1,2,0) = (6,5,5). : need (1,1,1), run; Work = (8,5,6) = Total, as it must be. Safe sequence , so (a) is safe.

  5. Request check for (b): (1,1,1) Need = (1,1,1), and (1,1,1) Available = (2,2,2). Pretend grant: Available = (1,1,1), Allocation = (3,1,2), Need = (0,0,0).

  6. Safety on the new state: Work = (1,1,1). , and each need 2 units of A but only 1 is free, so none can run. needs (0,0,0), run; Work = (1,1,1)+(3,1,2) = (4,2,3). : (2,2,2) (4,2,3), run; Work = (5,2,5). : (2,1,1) (5,2,5), run; Work = (7,3,6). : (2,2,1) (7,3,6), run; Work = (8,5,6). Safe sequence , so grant.

Answer: (a) Safe; one safe sequence is <P0, P1, P2, P3>. (b) Yes, the request can be granted because the resulting state is still safe (safe sequence <P3, P0, P1, P2>).

EXAM TIP

A request is judged against the state AFTER pretending to grant it, not the current state. A request can be within Need and within Available and still be refused because the new state is unsafe. Also, a safe state usually has several safe sequences; finding one is enough, and the sequence found depends on the order in which you scan processes.

Deadlock detection and recovery

If a system neither prevents nor avoids deadlock, it must detect it and then recover. Detection does not use maximum claims; it works on what processes are holding and currently requesting.

Single instance of every type: wait-for graph. Remove the resource nodes from the RAG and draw an edge when waits for a resource held by . There is a deadlock if and only if the wait-for graph has a cycle, and every process on a cycle is deadlocked. Cycle detection costs for processes (adjacency-matrix search).

Detection algorithm for several instances

  1. Use and a matrix (what each process is currently waiting for). Set .

  2. Set if , else . A process holding nothing cannot be the reason anyone else waits, so the algorithm does not report it; it can still be left waiting for ever if it requests resources held by deadlocked processes.

  3. Find with and . If none, go to step 5.

  4. Set , , and go to step 3 (optimistically assume will finish and release).

  5. Every with is deadlocked. The cost is for resource types and processes.

WORKED EXAMPLE

Two resource types A and B with totals (4, 3). Allocation: = (1,1), = (2,0), = (1,1), = (0,1). Current requests: = (0,1), = (0,0), = (0,1), = (0,1). Which processes are deadlocked?

Solution
  1. Allocated: A = 1+2+1+0 = 4, B = 1+0+1+1 = 3. Available = (0,0). Every process holds something, so all Finish values start false.

  2. Work = (0,0). requests (0,1): B is 0 in Work, cannot proceed. requests (0,0) (0,0): it can finish. Work = (0,0)+(2,0) = (2,0).

  3. Rescan: , and each request (0,1), but Work has B = 0, so none can proceed.

  4. No further process can be chosen. Unfinished: , , . They all wait for B, and all three B instances are held by those same three processes (, , hold one each).

Answer: , and are deadlocked; is not.

When to run detection. Running it at every request catches deadlock immediately but is expensive; running it at fixed intervals, or when CPU utilisation drops, costs less but several cycles may form and it becomes harder to say which request caused the deadlock.

Recovery from deadlock

  1. Process termination, abort all: certain to break the deadlock, but all partial work is lost.

  2. Process termination, abort one at a time: abort a victim, re-run detection, repeat. Choose victims by priority, work done so far, resources held, resources still needed, and number of processes to be terminated.

  3. Resource preemption: take resources from some processes and give them to others. Needs a victim selection rule, a rollback of the victim to a safe state (or a full restart), and a guard against starvation (the same process repeatedly chosen as the victim, for example by counting rollbacks in the cost).

Minimum instances, starvation and livelock

How many instances guarantee no deadlock? Take a single resource type with identical instances, and processes. Each process needs at most instances (and may actually ask for all ), requests them one at a time, and holds what it has while waiting. Deadlock needs every process blocked with nothing free. The most that blocked processes can hold without any of them having its full need is each, giving instances held. If one more instance exists, at least one process can obtain all , finish and release, so deadlock is impossible.

MINIMUM RESOURCES THAT GUARANTEE NO DEADLOCK

Single resource type, processes, each needing at most units (and possibly asking for all ), units requested one at a time and held while waiting. Deadlock is impossible iff ; with a deadlock can occur.

LARGEST NUMBER OF PROCESSES WITH NO POSSIBLE DEADLOCK

Same assumptions, . Follows from .

For different maximum needs the condition becomes .

Deadlock, starvation and livelock

Term

What happens

Is the system stuck for ever?

Deadlock

Each process in a set waits for a resource held by another in the set; none of them runs.

Yes, for all processes in the set, until the system intervenes.

Starvation

A process waits indefinitely because others are always preferred (for example always lower priority), while the system as a whole keeps making progress.

Only that process; the others proceed. Aging (raising priority with waiting time) is the usual cure.

Livelock

Processes keep changing state in response to each other (for example repeatedly retrying and backing off) but make no useful progress; they are not blocked and keep using the CPU.

No useful progress for as long as the pattern lasts; unlike a deadlock, it can end by chance (for example with random back-off).

EXAM TIP

Deadlock is always a circular wait with blocked processes; starvation need not involve any cycle. Deadlocked processes that block while waiting use no CPU (processes that spin on a lock do); livelocked processes keep running. For the counting formula, remember per process, then add one.

PREVIOUS-YEAR QUESTIONS

6 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2022 · GATE CSE 2018

GATE 2026 CS1 Q.29

GATE 2026 · CS1Q.29 · 1 mark · MSQ

With respect to deadlocks in an operating system, which of the following statements is/are FALSE?

  1. (A)

    Banker's algorithm is used to prevent deadlocks

  2. (B)

    Deadlock formation can be prevented by ensuring that the hold and wait condition is not allowed

  3. (C)

    An assignment edge in a resource allocation graph is marked from a process to a resource

  4. (D)

    A safe state guarantees that all processes can finish without formation of a deadlock

Answer and solution
See the question in the paper
GATE 2026 CS1 Q.29: answer and solution

GATE 2026 CS1 Q.35

GATE 2026 · CS1Q.35 · 1 mark · NAT

Consider a system consisting of instances of a resource , being shared by 5 processes. Assume that each process requires a maximum of two instances of resource and a process can request or release only one instance at a time. Further, a process can request the second instance of the resource only after acquiring the first instance.

The minimum value of for the system to be deadlock-free is ________. (answer in integer)

Answer and solution
See the question in the paper
GATE 2026 CS1 Q.35: answer and solution

GATE 2025 CS2 Q.48

GATE 2025 · CS2Q.48 · 2 marks · MSQ

consists of all active processes in an operating system. consists of single instances of distinct types of resources in the system.

The resource allocation graph has the following assignment and claim edges.

Assignment edges: (the assignment edge means resource is assigned to process , and so on for others)

Claim edges: (the claim edge means process is waiting for resource , and so on for others)

Which of the following statement(s) is/are CORRECT?

  1. (A)

    Aborting makes the system deadlock free.

  2. (B)

    Aborting makes the system deadlock free.

  3. (C)

    Aborting makes the system deadlock free.

  4. (D)

    Aborting and makes the system deadlock free.

Answer and solution
See the question in the paper
GATE 2025 CS2 Q.48: answer and solution

GATE 2022 CS Q.26

GATE 2022 · CSQ.26 · 1 mark · MSQ

Which of the following statements is/are TRUE with respect to deadlocks?

  1. (A)

    Circular wait is a necessary condition for the formation of deadlock.

  2. (B)

    In a system where each resource has more than one instance, a cycle in its wait-for graph indicates the presence of a deadlock.

  3. (C)

    If the current allocation of resources to processes leads the system to unsafe state, then deadlock will necessarily occur.

  4. (D)

    In the resource-allocation graph of a system, if every edge is an assignment edge, then the system is not in deadlock state.

Answer and solution
See the question in the paper
GATE 2022 CS Q.26: answer and solution

GATE 2018 CS Q.39

GATE 2018 · CSQ.39 · 2 marks · MCQ

In a system, there are three types of resources: , and . Four processes and execute concurrently. At the outset, the processes have declared their maximum resource requirements using a matrix named Max as given below. For example, is the maximum number of instances of that would require. The number of instances of the resources allocated to the various processes at any given state is given by a matrix named Allocation.

Consider a state of the system with the Allocation matrix as shown below, and in which 3 instances of and 3 instances of are the only resources available.

Allocation:

Max:

From the perspective of deadlock avoidance, which one of the following is true?

GATE 2018 CS question 39: Two side-by-side 4x3 tables.Tap to open full size
Figure from the GATE 2018 CS paper. It is part of the question.
Figure description

Two side-by-side 4x3 tables. Allocation (columns E, F, G): P0 = 1,0,1; P1 = 1,1,2; P2 = 1,0,3; P3 = 2,0,0. Max (columns E, F, G): P0 = 4,3,1; P1 = 2,1,4; P2 = 1,3,3; P3 = 5,4,1.

  1. (A)

    The system is in safe state.

  2. (B)

    The system is not in safe state, but would be safe if one more instance of were available

  3. (C)

    The system is not in safe state, but would be safe if one more instance of were available

  4. (D)

    The system is not in safe state, but would be safe if one more instance of were available

Answer and solution
GATE 2018 CS Q.39: answer and solution

OTHER EXAMS

ISRO: 2 questions

From ISRO's Scientist/Engineer written test: ISRO CS 2025. They are not counted in the GATE numbers on this page.

ISRO CS 2025 Q.66

ISRO 2025 · CSQ.66 · 1 mark · MCQ

Analyze the below resource allocation graph. Which of the following is the corresponding wait-for graph?

ISRO CS 2025 question 66: Resource allocation graph with processes P1 to P5 (circles) and resources R1 to R5 (squares, one instance each).Tap to open full size
Figure from the ISRO CS 2025 paper. It is part of the question.
Figure description

Resource allocation graph with processes P1 to P5 (circles) and resources R1 to R5 (squares, one instance each). Edges: P1 -> R1 (request); R1 -> P2 (assignment); P2 -> R3 (request); R3 -> P5 (assignment); P2 -> R4 (request); R4 -> P3 (assignment); P2 -> R5 (request); P3 -> R5 (request); R5 -> P4 (assignment); P4 -> R2 (request); R2 -> P1 (assignment). Options are wait-for graphs on P1 to P5. (a) P1->P2, P2->P5, P2->P3, P2->P4, P3->P4, P4->P1. (b) P1->P2, P2->P5, P2->P3, P3->P4, P4->P1 (no P2->P4). (c) P1->P2, P2->P5, P2->P3, P2->P4, P4->P1 (no P3->P4). (d) P1->P2, P2->P5, P2->P3, P2->P4, P3->P4 (no P4->P1).

  1. (A)

    Wait-for graph with edges P1->P2, P2->P5, P2->P3, P2->P4, P3->P4, P4->P1

  2. (B)

    Wait-for graph with edges P1->P2, P2->P5, P2->P3, P3->P4, P4->P1 (no edge P2->P4)

  3. (C)

    Wait-for graph with edges P1->P2, P2->P5, P2->P3, P2->P4, P4->P1 (no edge P3->P4)

  4. (D)

    Wait-for graph with edges P1->P2, P2->P5, P2->P3, P2->P4, P3->P4 (no edge P4->P1)

Answer and solution
ISRO CS 2025 Q.66: answer and solution

ISRO CS 2025 Q.69

ISRO 2025 · CSQ.69 · 1 mark · MCQ

Consider a system with 3 processes P0 to P2 and 3 resource types A, B, and C. Resource type A has 10 instances, resource type B has 5 instances and resource type C has 7 instances. The following snapshot of the system has been taken at a given time.

ProcessAllocation (A B C)Max (A B C)Available (A B C)
P00 1 07 5 35 4 5
P12 0 03 2 2
P23 0 29 0 2

Which of the following is true?

  1. (A)

    The system is not in safe state

  2. (B)

    The system is blocked

  3. (C)

    The system is in safe state

  4. (D)

    The request cannot be granted to all the processes

Answer and solution
See the question in the paper
ISRO CS 2025 Q.69: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · NAT

A system has a single resource type with identical units. Each of 5 processes needs at most 4 units, requests them one at a time, and holds what it has while waiting. What is the minimum number of units that guarantees no deadlock?

Answer and solution

Answer:

  1. Use with , .

  2. .

PRACTICE 21 mark · MCQ

Which statement about resource-allocation graphs is correct?

  1. (A)

    If every resource type has exactly one instance, a cycle in the graph is necessary and sufficient for deadlock.

  2. (B)

    If resource types have several instances, any cycle in the graph means the processes on it are deadlocked.

  3. (C)

    If resource types have several instances, a graph with no cycle can still be deadlocked.

  4. (D)

    If every resource type has exactly one instance, a graph with no cycle can still be deadlocked.

Answer and solution

Answer: (A)

  1. With single instances, a cycle means each process on it waits for the only instance held by the next, so deadlock; and deadlock needs a cycle. A is true, D is false.

  2. With several instances a cycle is only necessary, so B is false.

  3. With no cycle, some process can always finish, so no deadlock; C is false.

PRACTICE 31 mark · MCQ

A system requires processes to request resource types in strictly increasing order of a fixed numbering. Which necessary condition for deadlock does this scheme make impossible?

  1. (A)

    Mutual exclusion

  2. (B)

    Hold and wait

  3. (C)

    No preemption

  4. (D)

    Circular wait

Answer and solution

Answer: (D)

  1. Processes still hold resources while waiting, so hold and wait can occur; resources are still non-shareable and non-preemptable.

  2. A cycle of waits would need order numbers to increase around the cycle and return to the start, which is impossible.

  3. The answer is circular wait.

PRACTICE 42 marks · MSQ

One resource type has 10 units. Three processes have maximum claims 7, 5 and 6 units. Currently holds 2, holds 3 and holds 2 units, so 3 units are free. Which single requests can be granted immediately by the Banker's algorithm (each considered separately from the current state)?

  1. (A)

    requests 1 unit

  2. (B)

    requests 2 units

  3. (C)

    requests 3 units

  4. (D)

    requests 3 units

Answer and solution

Answer: (A), (B)

  1. Needs now: = 5, = 2, = 4; Available = 3. Every request below is within its process's Need and within Available, so each is decided by the safety check.

  2. A: allocation (3,3,2), need (4,2,4), available 2. (2) runs, work 5; (4) runs, work 8; (4) runs. Safe, grant.

  3. B: then holds 5 with need 0, available 1. finishes, work 6; (5) runs, work 8; (4) runs. Safe, grant.

  4. C: then holds 5 with need 1, available 0. Needs are 5, 2 and 1, all greater than 0 free units, so no process can finish. Unsafe, refuse.

  5. D: then holds 5 with need 2, available 0. Needs are 2, 2 and 4, all greater than 0, so no process can finish. Unsafe, refuse.

PRACTICE 52 marks · NAT

A system has 7 identical units of a single resource. Each process needs at most 3 units at a time, requests them one at a time, and holds what it has while waiting. What is the largest number of processes for which deadlock is impossible?

Answer and solution

Answer:

  1. Deadlock is impossible iff , so , giving .

  2. Check : holdings 2, 2, 2, 1 use all 7 units and every process is still waiting, so deadlock is possible.

  3. The largest is 3.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

Deadlock

Preparing in Kolkata? IAE Academy's course for GATE CSE.