Computer Science and IT‹›⌂ 0:00 studied

Module 9 · 14 min read · Free chapter

Deadlock

In short

Deadlock needs mutual exclusion, hold and wait, no preemption and circular wait at the same time. Prevention denies one condition, avoidance keeps the system in a safe state with the Banker's algorithm, and detection finds cycles and recovers. With n processes each needing at most k units, R ≥ n(k − 1) + 1 rules deadlock out.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: gridlock at a crossing

Picture a busy four-way crossing in a city when the signal has failed. Four cars enter the junction at once, each from a different road. Each car now needs the piece of road in front of it, and that piece is occupied by the car on its right.

No car can move forward, and no driver will reverse. Every car waits for another car that is itself waiting, so the wait never ends. Traffic police call this gridlock; operating systems call it deadlock.

Processes get stuck in the same way. One process holds the printer and wants the scanner, while another holds the scanner and wants the printer. In this chapter we learn the exact conditions for deadlock, how to spot it in a graph, and the three ways an OS deals with it: prevent it, avoid it, or detect it and recover.

Why it matters. A deadlocked program does not crash or print an error. It simply stops, which makes deadlock one of the hardest bugs to find in banking, railway and database software.

Resources and the four necessary conditions

A resource is anything a process must hold to make progress: a printer, a file lock, a block of memory or a semaphore. Resources come in types, and a type can have several identical instances, such as three identical printers. A process uses a resource in three steps: it requests it, uses it and releases it.

A set of processes is deadlocked when each one waits for an event that only another process in the same set can cause. Usually the event is the release of a resource. Deadlock can happen only if four conditions hold at the same time.

Condition Meaning Crossing example
Mutual exclusion a resource is used by one process at a time one car per patch of road
Hold and wait a process holds one resource and waits for more each car holds its patch and wants the next
No preemption a resource cannot be taken away; it is released only by the holder no crane lifts a car away
Circular wait a cycle of processes, each waiting for the next four cars blocking each other in a ring

These are necessary conditions: if any one of them is missing, deadlock cannot occur. That fact is the basis of every prevention method, which simply makes sure one condition never holds.

Deadlock is different from starvation. In starvation a process waits a very long time because others keep being preferred, but the system as a whole keeps working. In deadlock nobody in the set can ever move.

Why it matters. GATE often asks which condition a given rule breaks. If you can name the four conditions and match each to a fix, such questions take a few seconds.

The resource-allocation graph

A resource-allocation graph (RAG) draws the current state. Each process is a circle and each resource type is a box, with one dot inside for each instance. A request edge P → R means process P is waiting for R, and an assignment edge R → P means one instance of R is held by P.

Reading the graph needs three rules. First, if the graph has no cycle, there is no deadlock. Second, if every resource type has exactly one instance, a cycle means deadlock for sure.

Third, if some resource types have several instances, a cycle shows only that deadlock is possible. Take R1 and R2, with P1 holding R1 and requesting R2, and P2 holding one instance of R2 and requesting R1. If R2 has a second instance held by P3, which needs nothing more, then P3 will finish and release its instance, P1 will get it, and the cycle dissolves.

Worked example 1. Single instances: P1 holds R1 and requests R2; P2 holds R2 and requests R3; P3 holds R3 and requests R1. The edges form the cycle P1 → R2 → P2 → R3 → P3 → R1 → P1, and since each resource has one instance, the three processes are deadlocked.

Why it matters. The graph turns a vague worry into something you can check by eye. Detection algorithms in real systems are just fast ways of searching such a graph for cycles.

Deadlock prevention

Prevention designs the rules so that at least one of the four conditions can never hold. Each condition has its own fix, with its own cost.

Condition denied How Cost
Mutual exclusion make resources sharable, such as read-only files impossible for printers and write locks
Hold and wait request all resources at the start, or only when holding none poor utilisation and possible starvation
No preemption if a request cannot be met, release what you hold, or let others take it works only for resources whose state can be saved, such as CPU registers
Circular wait number all resource types and request them only in increasing order programmers must follow the order

The last method is the one used most in practice. Suppose tape = 1, disk = 2 and printer = 3. A process holding the disk may ask for the printer but never for the tape, so every chain of waiting climbs upward and cannot close into a loop.

Why it is never used everywhere. Prevention wastes resources or puts heavy rules on programs. That is why most general-purpose operating systems, including Unix-like systems and Windows, simply ignore deadlock among ordinary user resources and leave it to applications, which is jokingly called the ostrich approach.

Why it matters. Lock ordering is the everyday rule in kernels and databases: always take lock A before lock B. Knowing why it works tells you which rule-of-thumb in an exam option really prevents deadlock.

Avoidance: safe states and the Banker's algorithm

Avoidance allows all four conditions but checks every request before granting it. Each process declares in advance the maximum it may ever need. The OS grants a request only if the system stays in a safe state.

A state is safe if there is a safe sequence: an order of all processes in which each one can obtain its remaining need from what is available now plus what the earlier processes in the order will release. A safe state cannot deadlock. An unsafe state is not yet a deadlock, but it may lead to one if processes ask for their maximum.

The Banker's algorithm works with four tables for nn processes and mm resource types: Available, Max, Allocation and Need, where

Need=Max−Allocation\mathit{Need} = \mathit{Max} - \mathit{Allocation}
Work = Available; Finish[i] = false for all i
repeat: find i with Finish[i] == false and Need[i] <= Work
        Work = Work + Allocation[i]; Finish[i] = true
until no such i
safe if Finish[i] is true for every i

Worked example 2. Three resource types A, B, C have totals (8,6,7)(8, 6, 7). The table gives Allocation and Max; Need is Max minus Allocation.

Process Allocation (A B C) Max (A B C) Need (A B C)
P0 1 0 2 4 3 3 3 3 1
P1 2 1 1 3 2 2 1 1 1
P2 1 2 0 5 2 2 4 0 2
P3 2 1 2 2 3 3 0 2 1

The allocations add up to (6,4,5)(6, 4, 5), so Available is (8,6,7)−(6,4,5)=(2,2,2)(8, 6, 7) - (6, 4, 5) = (2, 2, 2). Scan for a process whose Need fits in Work. P1 needs (1,1,1)≤(2,2,2)(1, 1, 1) \le (2, 2, 2), so it finishes and Work becomes (4,3,3)(4, 3, 3); then P0 fits and Work becomes (5,3,5)(5, 3, 5); then P2 fits and Work becomes (6,5,5)(6, 5, 5); finally P3 fits and Work becomes (8,6,7)(8, 6, 7).

So P1, P0, P2, P3 is a safe sequence and the state is safe. Other safe sequences may exist, such as P1, P3, P0, P2, and any one is enough.

Worked example 3 (a request). P0 asks for (2,2,0)(2, 2, 0). The request is within its Need (3,3,1)(3, 3, 1) and within Available (2,2,2)(2, 2, 2), so we pretend to grant it: Available becomes (0,0,2)(0, 0, 2) and P0's Need becomes (1,1,1)(1, 1, 1). Now no process's Need fits in (0,0,2)(0, 0, 2), because every Need asks for some A or B, so the state is unsafe and the request is refused.

If instead P0 asks for (1,0,1)(1, 0, 1), Available becomes (1,2,1)(1, 2, 1) and P0's Need becomes (2,3,0)(2, 3, 0). P1 fits first, giving Work (3,3,2)(3, 3, 2), and then P0, P2 and P3 finish in turn, so the request is granted.

The safety check costs on the order of m×n2m \times n^2 steps. That is affordable for a few processes but too slow and too demanding for a general OS, since few programs know their maximum needs in advance.

Why it matters. The Banker's algorithm is the most asked deadlock numerical in GATE and IT officer papers. The method is always the same: compute Need, compute Available, and grow Work one process at a time.

How many resources rule out deadlock?

A favourite GATE question gives one resource type with RR identical units shared by nn processes, each needing at most kk units. It then asks for the smallest RR that makes deadlock impossible.

Think of the worst case. Deadlock needs every process to be stuck, so each holds as much as it can without finishing, which is k−1k - 1 units, and the pool is empty. That uses n(k−1)n(k - 1) units. One more unit lets some process reach kk, finish, and release everything, so

R≥n(k−1)+1R \ge n(k - 1) + 1

Worked example 4. Three processes each need at most 4 tape drives. The smallest safe number is 3×(4−1)+1=103 \times (4 - 1) + 1 = 10. With 9 drives, each process could hold 3 and wait for a fourth that never comes.

Worked example 5. A system has 6 drives and each process needs at most 3. Deadlock is impossible while n(3−1)+1≤6n(3 - 1) + 1 \le 6, that is 2n≤52n \le 5, so at most n=2n = 2 processes can be allowed.

When the processes need different amounts m1,m2,…,mnm_1, m_2, \dots, m_n, the same argument gives R≥∑(mi−1)+1R \ge \sum (m_i - 1) + 1. For needs of 2, 3 and 4 units, the answer is 1+2+3+1=71 + 2 + 3 + 1 = 7.

Why it matters. This one-line formula answers a whole family of exam questions. It is also a quick sanity check when sizing a pool of connections or buffers in real software.

Detection and recovery

A third strategy lets deadlock happen, finds it, and then breaks it. This suits systems where deadlock is rare and avoidance would cost more than an occasional recovery.

Detection with single instances. Remove the resource boxes from the resource-allocation graph and draw an edge Pi → Pj whenever Pi waits for a resource held by Pj. This is the wait-for graph, and a cycle in it means deadlock; finding a cycle takes about n2n^2 steps for nn processes.

Detection with several instances. Use a Banker-like algorithm, but with each process's current Request in place of Need. Processes holding nothing are marked finished at the start. Any process still unfinished when no more progress is possible is deadlocked.

How often should detection run? Running it on every request catches deadlock at once but is costly. Running it every few minutes, or when CPU use falls sharply, is cheaper but lets a deadlock sit unnoticed for a while.

Recovery has two families. Process termination aborts all deadlocked processes, which is quick but throws away all their work, or aborts them one at a time and reruns detection after each, which is slower but loses less. Resource preemption takes resources from a chosen victim, rolls it back to a safe earlier state and restarts it later.

Choosing the victim considers priority, how long the process has run, how many resources it holds and how many more it needs. To avoid starvation, the same process must not be picked every time, so the number of rollbacks is counted as part of the cost.

Why it matters. Databases use exactly this approach: they detect a deadlock between transactions and roll one back. Understanding the costs explains why the victim is usually the youngest or cheapest transaction.

Descriptive lens: how written papers ask this

Written papers for IT officer posts, state Assistant Programmer exams and university-level tests ask about deadlock in four ways:

  1. State and explain the four necessary conditions with an everyday example.
  2. Compare prevention, avoidance, detection and the ostrich approach, with their costs.
  3. Solve a Banker's algorithm table, find a safe sequence and decide whether a request can be granted.
  4. Draw a resource-allocation graph or wait-for graph and decide whether the system is deadlocked.

Practice questions (our own, not previous-year questions).

  1. State the four necessary conditions for deadlock and explain how each can be denied, with the cost of each method. (10 marks)
  2. Distinguish between a safe state, an unsafe state and a deadlocked state. Is every unsafe state a deadlock? Justify with an example. (10 marks)
  3. For five processes and three resource types with given Allocation and Max tables, find Need and Available, find a safe sequence, and decide whether a given request should be granted. (15 marks)

Answer plan for question 3.

  • Write the rule Need=Max−Allocation\mathit{Need} = \mathit{Max} - \mathit{Allocation} and compute the Need table row by row.
  • Add the Allocation columns and subtract the sum from the total resources to get Available.
  • Set Work equal to Available and repeatedly pick an unfinished process whose Need is at most Work, adding its Allocation to Work each time.
  • Write down the order of finishing processes as the safe sequence, or state that the state is unsafe if the scan gets stuck.
  • For the request, check it against the process's Need and against Available, and then subtract it to form the pretend state.
  • Run the safety check again on the pretend state and conclude whether the request is granted or the process must wait.

Quick revision

  • Deadlock means every process in a set waits for an event that only another process in the set can cause.
  • The four necessary conditions are mutual exclusion, hold and wait, no preemption and circular wait, and all four must hold together.
  • In a resource-allocation graph, no cycle means no deadlock, and with single instances a cycle means deadlock.
  • Prevention denies one condition, and ordering resource types is the common way to deny circular wait.
  • A safe state has a safe sequence, while an unsafe state may, but need not, lead to deadlock.
  • The Banker's algorithm uses Need=Max−Allocation\mathit{Need} = \mathit{Max} - \mathit{Allocation} and grows Work by each finishing process's Allocation.
  • With nn processes each needing at most kk units, deadlock is impossible when R≥n(k−1)+1R \ge n(k - 1) + 1.
  • Detection uses a wait-for graph for single instances and a Banker-like algorithm for several instances.
  • Recovery aborts processes or preempts resources from a victim with rollback.
  • Most general-purpose systems ignore deadlock among user resources, which is the ostrich approach.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. A set of processes is deadlocked when:

  2. Which is NOT one of the four necessary conditions for deadlock?

  3. Deadlock can occur only if:

  4. A resource-allocation graph has a cycle and some resource types have several instances. Then:

  5. Numbering resource types and requesting them only in increasing order prevents deadlock by denying:

  6. Requiring a process to request all its resources before it starts denies:

  7. Which statement is TRUE?

  8. In the Banker's algorithm, Need equals:

  9. When a process finishes during the safety check, Work is increased by its:

  10. Totals (8,6,7)(8, 6, 7); allocations P0 (1,0,2)(1, 0, 2), P1 (2,1,1)(2, 1, 1), P2 (1,2,0)(1, 2, 0), P3 (2,1,2)(2, 1, 2). Available is:

  11. In the same state, with Needs P0 (3,3,1)(3, 3, 1), P1 (1,1,1)(1, 1, 1), P2 (4,0,2)(4, 0, 2), P3 (0,2,1)(0, 2, 1), P0 requests (2,2,0)(2, 2, 0). The request is:

  12. A wait-for graph can detect deadlock correctly when:

  13. Three processes each need at most 4 units of a resource. The minimum units that rule out deadlock is:

  14. A system has 6 drives and each process needs at most 3. The largest number of processes with no deadlock possible is:

  15. Ignoring deadlock and leaving it to applications is called the:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

What are the four necessary conditions for deadlock?

Mutual exclusion, hold and wait, no preemption and circular wait, and all four must hold together.

Is every unsafe state a deadlock?

No, an unsafe state only means deadlock cannot be ruled out; deadlocked states are a subset of unsafe states.

How is Need calculated in the Banker's algorithm?

Need equals Max minus Allocation for each process and each resource type.

How many resources make deadlock impossible?

For n processes each needing at most k units of one resource, at least n(k − 1) + 1 units are needed.

More in this module: Operating systems

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Operating Systems (IIT Madras, 106106144): deadlocks