Prevention limits what programs may do, and avoidance needs to know their needs in advance (Lesson 5.2). A third approach asks for nothing: let processes do what they like, and look for deadlock once in a while. If you find one, break it.
This is how a database works. A database cannot know which rows a transaction will touch next, and it cannot insist that every query takes its locks in one fixed order. So it detects deadlocks and fixes them.
Detection: the wait-for graph
Looking for a deadlock means looking for a loop (Lesson 5.1). When every resource has a single instance, the resource boxes are not needed at all. You can remove them and draw only the processes, with an arrow from A to B if A is waiting for a resource that B holds. This is the wait-for graph. A cycle in it is a deadlock.
Here are four transactions in a database, each holding a lock on one row and waiting for the row held by the next one. Play it, and watch the simulator redraw the graph without the rows.
Four transactions in a ring
-
The processes and the resources
Circles are processes and boxes are resources. The dots in a box are its instances: a hollow dot is free, a filled dot is held. Here 4 processs (T1, T2, T3, T4) will use rowA, rowB, rowC and rowD.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 1/1, rowB 1/1, rowC 1/1, rowD 1/1
-
T1 holds rowA
T1 is given rowA. The arrow runs from the resource to the process: it is assigned to T1, and nobody else can use that instance until T1 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 0/1, rowB 1/1, rowC 1/1, rowD 1/1
-
T2 holds rowB
T2 is given rowB. The arrow runs from the resource to the process: it is assigned to T2, and nobody else can use that instance until T2 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 0/1, rowB 0/1, rowC 1/1, rowD 1/1
-
T3 holds rowC
T3 is given rowC. The arrow runs from the resource to the process: it is assigned to T3, and nobody else can use that instance until T3 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 1/1
-
T4 holds rowD
T4 is given rowD. The arrow runs from the resource to the process: it is assigned to T4, and nobody else can use that instance until T4 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
T1 asks for rowB
T1 asks for rowB, but every instance is free. T1 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- none
- Deadlock
- no
- Waiting
- T1
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
T2 asks for rowC
T2 asks for rowC, but every instance is free. T2 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- none
- Deadlock
- no
- Waiting
- T1, T2
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
T3 asks for rowD
T3 asks for rowD, but every instance is free. T3 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- none
- Deadlock
- no
- Waiting
- T1, T2, T3
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
T4 asks for rowA
T4 asks for rowA, but every instance is free. T4 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- T1 → rowB → T2 → rowC → T3 → rowD → T4 → rowA → T1
- Deadlock
- yes
- Waiting
- T1, T2, T3, T4
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
Redraw it as a wait-for graph
The resources are not needed to find a deadlock if you only ask who is waiting for whom. Remove them: an arrow from one process to another now means "waits for something the other one holds".
- Cycle
- none
- Deadlock
- yes
- Waiting
- T1, T2, T3, T4
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
Search for a cycle
Follow the arrows and you come back to where you started: T1 → T2 → T3 → T4 → T1. Each process on it waits for the next, and the last waits for the first.
- Cycle
- T1 → T2 → T3 → T4 → T1
- Deadlock
- yes
- Waiting
- T1, T2, T3, T4
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
Choose a victim: T3
To break the cycle one of its processes must give up what it holds. The system picks T3 as the victim.
- Cycle
- T1 → T2 → T3 → T4 → T1
- Deadlock
- yes
- Waiting
- T1, T2, T3, T4
- Free
- rowA 0/1, rowB 0/1, rowC 0/1, rowD 0/1
-
T3 is aborted
T3 is stopped and everything it held is released. Its arrows disappear, so the others may be able to get what they were waiting for.
- Cycle
- none
- Deadlock
- no
- Waiting
- T1, T4
- Free
- rowA 0/1, rowB 0/1, rowC 1/1, rowD 0/1
-
T2 can finish
T2 asks for rowC and a free instance is there, so it gets it. It runs to the end and releases rowB, which may free somebody else.
- Cycle
- none
- Deadlock
- no
- Waiting
- T4
- Free
- rowA 0/1, rowB 1/1, rowC 1/1, rowD 0/1
-
T1 can finish
T1 asks for rowB and a free instance is there, so it gets it. It runs to the end and releases rowA, which may free somebody else.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 1/1, rowB 1/1, rowC 1/1, rowD 0/1
-
T4 can finish
T4 asks for rowA and a free instance is there, so it gets it. It runs to the end and releases rowD, which may free somebody else.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 1/1, rowB 1/1, rowC 1/1, rowD 1/1
-
No deadlock: everyone finishes
Every process could finish, so the state is not deadlocked.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- rowA 1/1, rowB 1/1, rowC 1/1, rowD 1/1
The detector found the cycle T1, T2, T3, T4. The system then chose a victim, T3, and aborted it. Its lock was released, so T2 could finish, then T1 and then T4. One transaction was sacrificed, and the other three completed. It did not matter much which one was chosen as far as breaking the loop goes, because every transaction in a loop is holding something the next one needs.
When should the detector run?
Looking for a cycle is not free. The detector has to walk the graph, and the graph changes all the time. A system might:
- run it every time a request has to wait, which finds deadlocks at once but costs the most;
- run it at intervals, which is cheaper, but a deadlock can sit unnoticed for a while;
- run it when something looks wrong, for example when the CPU has been idle for a long time while many processes are blocked.
Recovery: breaking the loop
Detection tells you there is a deadlock. Something must then change, because waiting will never fix it. The options:
- Abort all the processes in the cycle. Simple, and it surely breaks the deadlock, but it throws away a lot of work.
- Abort one at a time, as in the simulator, and run the detection again after each, until no cycle remains. It wastes less, but detection runs again and again.
- Roll back one process to an earlier point (a checkpoint) where it did not yet hold the lock, then restart it from there. This is what a database does with a transaction, because it can undo every change with its log.
- Preempt a resource: take it from one process and give it to another. Only possible when the resource’s state can be saved and restored, and this is rarely so for a lock.
Which process should be the victim? A good choice is one that has done the least work, holds the fewest resources, or is the youngest, so the cost of throwing it away is small. There is one trap: if the same process is picked every time, it never completes. That is starvation, and systems prevent it by counting how often a process has been a victim and making it less likely to be chosen again.
Timeouts, and doing nothing
The cheapest detector of all is a clock. If a process has been waiting for a lock for too long, assume the worst: give up, release what you hold, and try again, perhaps after a random pause. This detects some waits that are not deadlocks, but it needs no graph. It is also how network systems cope: a request that gets no answer is retried.
And then there is the approach nearly all general-purpose operating systems take for the locks that programs use: ignore the problem. This is called the ostrich algorithm, after the idea of burying your head in the sand. It sounds irresponsible, but it is a reasoned trade. Real deadlocks are rare. Prevention, avoidance and detection all cost time for every program, including the great majority that never deadlock. So the kernel protects itself (with strict lock ordering, Lesson 5.2, and tools such as lockdep, Lesson 5.1) and leaves user programs to look after their own locks. When one does hang, you kill it and fix the bug.
In the real world
Databases are the place where deadlock detection really runs. PostgreSQL waits for a lock for a short while and then checks for a cycle. If it finds one, it aborts one transaction with:
ERROR: deadlock detected
MySQL’s InnoDB engine detects the cycle too, and picks a victim, often the transaction that has changed the fewest rows. The victim gets:
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
Both messages say the same thing: your transaction was rolled back, none of its changes were kept, and it is safe to run again. That is why well-written applications retry a transaction that fails like this: the other transaction has usually finished by then, and the loop is gone. :::
Recap
- Detection lets deadlocks happen and then looks for them. With single-instance resources, a cycle in the wait-for graph (A → B when A waits for something B holds) is a deadlock.
- Detection costs time, so it runs on a schedule, on every blocked request, or when the system looks idle.
- Recovery aborts all the processes in the cycle, or one victim at a time, or rolls one back to a checkpoint, or preempts a resource.
- A victim should be cheap to lose, and it must not be the same one every time, or it starves.
- Timeouts guess that a long wait is a deadlock. Most operating systems ignore deadlock in user locks (the ostrich algorithm), because it is rare and the cures cost everyone. Databases detect it for real and ask the application to retry.
Quick check
Quick check
-
Wait-for arrows point from the waiter to the holder. A cycle of them means everyone in it is waiting for someone else in it.
-
Aborting one breaks the loop. The database can undo the victim's changes from its log, so the victim can simply try again.
-
Fair victim selection tracks how often a process has been chosen and lowers its chances next time.
-
The error is a normal event in a busy database. Retrying is the standard answer.
Review: what you learned earlier
Coming back to an idea after a break is what makes it stick. These questions come from earlier modules, starting with any you missed. Answer from memory before you look anything up.
Spaced review
This review picks questions for you, so it needs JavaScript.
Module 5 recap
You can now say what deadlock is and when it can happen: a group of processes that each wait for something only another one holds, which needs mutual exclusion, hold and wait, no preemption and circular wait all at once. You can read a resource-allocation graph, and you know that a cycle is a deadlock only when each resource has one instance. You know how to prevent deadlock by breaking one condition, and why lock ordering is the cure that programmers use. You have run the Banker’s algorithm: safe states, safe sequences, and why a request can be refused even when resources are free. And you know how deadlock is detected with a wait-for graph, how a victim is chosen, and why most operating systems leave user locks alone while databases detect and retry.
Next, Module 6: every process thinks it has a whole machine’s memory to itself. You will see how the operating system builds that illusion from base and bounds registers, segments and pages, and what happens when the memory runs out.
Join the discussion