Skip to content
GATEiit

GATE CSE · Operating System

Deadlock

The four necessary conditions for deadlock, resource-allocation graphs, prevention, avoidance with the Banker's algorithm, detection and recovery, and the minimum-resources formula that GATE asks most often.

Before you start

This topic comes before: Transactions and concurrency control

Concept notes

1. What a deadlock is and the four conditions

A set of processes is deadlocked when each is waiting for an event (usually a resource release) that only another process in the set can cause.

A deadlock can happen only if all four conditions hold at once (Coffman conditions):

  1. Mutual exclusion: at least one resource can be held by only one process at a time.
  2. Hold and wait: a process holds some resources while waiting for others.
  3. No preemption: a resource is released only voluntarily by its holder.
  4. Circular wait: there is a cycle P1→P2→⋯→Pn→P1 where each waits for a resource held by the next.

They are necessary, not sufficient, in general.

Resource-allocation graph (RAG): circles for processes, boxes for resource types with one dot per instance. A request edge goes P→R; an assignment edge goes R→P.

  • If every resource has one instance: a cycle means deadlock, and no cycle means no deadlock.
  • If some resource has several instances: no cycle still means no deadlock, but a cycle need not mean deadlock (another instance may be freed by a process outside the cycle).

With single instances you can collapse the RAG into a wait-for graph (edge Pi→Pj if Pi waits for a resource Pj holds) and look for a cycle in O(n2).

P1P2R1R2P1 requests R2R2 held by P2P2 requests R1R1 held by P1
Single-instance RAG with a cycle P1 → R2 → P2 → R1 → P1: deadlock

2. Handling deadlock: prevent, avoid, detect, or ignore

Strategy Idea Cost
Prevention make one of the four conditions impossible low utilisation or starvation
Avoidance grant a request only if the state stays safe (Banker's) needs each process's maximum demand in advance
Detection and recovery let deadlocks happen, find them, then break them detection overhead, lost work
Ignore (ostrich) assume deadlocks are rare; reboot if one happens used by most general-purpose OSs

Prevention, one condition at a time:

  • Mutual exclusion: usually cannot be removed (a printer cannot be shared), except by spooling.
  • Hold and wait: request all resources at once before starting, or release everything before requesting more. Leads to poor utilisation and possible starvation.
  • No preemption: if a request cannot be met, release what you hold (works for CPU registers and memory, not printers).
  • Circular wait: number the resource types and require every process to request them in increasing order. This is the most practical method.

Recovery after detection: abort all deadlocked processes, or abort them one at a time until the cycle breaks, or preempt resources and roll a victim back. Choose victims to avoid always picking the same process (starvation).

3. Banker's algorithm

For n processes and m resource types the OS keeps:

  • Available [m]: free instances of each type.
  • Max [n][m]: the most each process may ever request.
  • Allocation [n][m]: what each process holds now.
  • Need =Max−Allocation.

Safety algorithm: set Work = Available. Repeatedly find an unfinished Pi with Needi≤Work (every component); pretend it runs to completion and set Work=Work+Allocationi. If every process finishes, the state is safe and the order found is a safe sequence. Time O(mn2).

Resource-request algorithm for a request Reqi:

  1. If Reqi>Needi: error (exceeds the declared maximum).
  2. If Reqi>Available: Pi must wait.
  3. Otherwise pretend to grant it (Available −, Allocation +, Need −) and run the safety algorithm. Grant only if the new state is safe; otherwise roll back and make Pi wait.

A safe state has no deadlock. An unsafe state is not necessarily deadlocked; it only means the OS can no longer guarantee avoiding one. A safe sequence need not be unique.

4. Minimum resources to rule out deadlock

Suppose n processes share R identical instances of one resource type, and process i may need up to ki instances.

The worst case is when every process holds one less than it needs, ki−1, and waits for one more. If even then one instance is free, some process can finish and release everything, and the rest follow. So the system is deadlock-free if and only if

R≥∑i=1n(ki−1)+1.

When all processes need the same maximum k:

  • Minimum resources: Rmin⁡=n(k−1)+1.
  • Maximum number of processes for a given R: nmax⁡=⌊R−1k−1⌋.
  • With R=n(k−1) exactly, deadlock is possible (everyone holds k−1).

This assumes each process requests one instance at a time and releases everything when it finishes.

Formulas to remember

Need matrix
Need=Max−Allocation
Safety step
Needi≤Work⇒Work←Work+Allocationi
Deadlock-free conditionR instances, process i needs at most ki
R≥∑i=1n(ki−1)+1
Minimum instances, equal demand
Rmin⁡=n(k−1)+1
Maximum processes, equal demand
nmax⁡=⌊R−1k−1⌋
Banker's safety check costn processes, m resource types
O(m⋅n2)

Common mistakes

  • Treating any RAG cycle as a deadlock; that is true only when every resource has a single instance.
  • Calling an unsafe state a deadlock; it is only a state from which deadlock cannot be ruled out.
  • Using nk or n(k−1) for the minimum resources; the answer is n(k−1)+1.
  • Forgetting to add the finished process's Allocation (not its Need or Max) to Work in the safety check.
  • Granting a Banker's request just because it fits in Available, without running the safety check.
  • Mixing up which condition each prevention method removes (resource ordering removes circular wait; all-at-once requests remove hold and wait).

Worked examples

Try each step yourself, then reveal the working.

Example 1: Is this state safe?

numerical

Resources A, B, C have 8, 6, 7 instances in total. Allocation: P0 (1, 1, 0), P1 (2, 0, 1), P2 (1, 2, 1), P3 (0, 1, 2), P4 (2, 1, 1). Max: P0 (4, 3, 2), P1 (3, 2, 2), P2 (2, 4, 3), P3 (2, 2, 4), P4 (5, 2, 2). Is the state safe?

  1. Step 1: Find Available.

    Try it on paper first.

Example 2: Should the request be granted?

numerical

In the same state, P4 requests (1, 0, 0). Should the Banker's algorithm grant it?

  1. Step 1: Check the request against Need and Available.

    Try it on paper first.

Example 3: Minimum tape drives

numerical

Four processes each need at most 3 tape drives. What is the smallest number of drives that guarantees no deadlock? With 13 drives, how many such processes could run with that guarantee if each needed at most 4?

  1. Step 1: Build the worst case for the first part.

    Try it on paper first.

Example 4: A cycle that is not a deadlock

conceptual

Resource R1 has two instances, one held by P1 and one by P3. R2 has one instance, held by P2. P1 requests R2 and P2 requests R1. The RAG has the cycle P1 → R2 → P2 → R1 → P1. Is the system deadlocked?

  1. Step 1: Is anyone outside the cycle able to finish?

    Try it on paper first.

Previous year questions

11 official GATE CSE questions on this topic, newest first.

Practise these 11 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.