Module 9 · 14 min read · Free chapter
Deadlock
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.
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 processes and resource types: Available, Max, Allocation and Need, where
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 . 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 , so Available is . Scan for a process whose Need fits in Work. P1 needs , so it finishes and Work becomes ; then P0 fits and Work becomes ; then P2 fits and Work becomes ; finally P3 fits and Work becomes .
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 . The request is within its Need and within Available , so we pretend to grant it: Available becomes and P0's Need becomes . Now no process's Need fits in , because every Need asks for some A or B, so the state is unsafe and the request is refused.
If instead P0 asks for , Available becomes and P0's Need becomes . P1 fits first, giving Work , and then P0, P2 and P3 finish in turn, so the request is granted.
The safety check costs on the order of 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 identical units shared by processes, each needing at most units. It then asks for the smallest 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 units, and the pool is empty. That uses units. One more unit lets some process reach , finish, and release everything, so
Worked example 4. Three processes each need at most 4 tape drives. The smallest safe number is . 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 , that is , so at most processes can be allowed.
When the processes need different amounts , the same argument gives . For needs of 2, 3 and 4 units, the answer is .
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 steps for 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:
- State and explain the four necessary conditions with an everyday example.
- Compare prevention, avoidance, detection and the ostrich approach, with their costs.
- Solve a Banker's algorithm table, find a safe sequence and decide whether a request can be granted.
- Draw a resource-allocation graph or wait-for graph and decide whether the system is deadlocked.
Practice questions (our own, not previous-year questions).
- State the four necessary conditions for deadlock and explain how each can be denied, with the cost of each method. (10 marks)
- Distinguish between a safe state, an unsafe state and a deadlocked state. Is every unsafe state a deadlock? Justify with an example. (10 marks)
- 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 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 and grows Work by each finishing process's Allocation.
- With processes each needing at most units, deadlock is impossible when .
- 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.