Your program stops responding. It has not crashed, there is no error message, and the CPU is idle. If you attach a debugger, every thread is waiting for a lock. Nobody is doing anything wrong at the moment you look: each thread is politely waiting for a lock that another thread holds. Yet none of them will ever get what it wants.
This is deadlock: a set of processes or threads, each waiting for something that only another one in the set can give, so that none of them can ever move again.
Two threads, two locks
Locks (Module 4) keep two threads from changing the same data at once. The trouble starts when a thread needs two locks. Thread 1 takes lock A and then asks for lock B. Thread 2 takes lock B and then asks for lock A. If each one gets its first lock before the other asks for its second, both wait for ever.
A real deadlock would freeze this lesson’s build, so the program below uses pthread_mutex_trylock, which gives up instead of waiting. A barrier makes sure that both threads hold their first lock before either tries for the second one. Then each trylock fails, and the program tells you what a plain lock would have done: wait.
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER;
pthread_barrier_t holding_first, tried_second;
void *thread1(void *arg) {
pthread_mutex_lock(&a); /* take A first */
pthread_barrier_wait(&holding_first); /* wait until thread 2 holds B */
if (pthread_mutex_trylock(&b) != 0) /* ask for B: it is not free */
printf("thread 1 holds A and cannot get B\n");
pthread_barrier_wait(&tried_second); /* both have tried */
pthread_mutex_unlock(&a);
return NULL;
}
void *thread2(void *arg) {
pthread_mutex_lock(&b); /* take B first */
pthread_barrier_wait(&holding_first);
if (pthread_mutex_trylock(&a) != 0) /* ask for A: it is not free */
printf("thread 2 holds B and cannot get A\n");
pthread_barrier_wait(&tried_second);
pthread_mutex_unlock(&b);
return NULL;
}
int main(void) {
pthread_t t1, t2;
pthread_barrier_init(&holding_first, NULL, 2);
pthread_barrier_init(&tried_second, NULL, 2);
pthread_create(&t1, NULL, thread1, NULL);
pthread_create(&t2, NULL, thread2, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}
Output (one run)
thread 1 holds A and cannot get B thread 2 holds B and cannot get A
Each thread holds one lock and wants the other. With pthread_mutex_lock in place of trylock, this program would never print anything and never end.
The picture: a resource-allocation graph
Drawing this as a graph makes the problem easy to see. The resource-allocation graph has a circle for every process (or thread) and a box for every resource. Two kinds of arrow connect them:
- An arrow from a resource to a process means the resource is assigned: the process holds it.
- An arrow from a process to a resource means a request: the process wants it and is waiting.
Press play and watch the arrows appear one by one. The dots in a box are its instances, filled when held. After a few steps the arrows will form a loop.
Two threads, two locks
-
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 2 processs (T1, T2) will use lockA and lockB.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- lockA 1/1, lockB 1/1
-
T1 holds lockA
T1 is given lockA. 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
- lockA 0/1, lockB 1/1
-
T2 holds lockB
T2 is given lockB. 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
- lockA 0/1, lockB 0/1
-
T1 asks for lockB
T1 asks for lockB, 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
- lockA 0/1, lockB 0/1
-
T2 asks for lockA
T2 asks for lockA, 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
- T1 → lockB → T2 → lockA → T1
- Deadlock
- yes
- Waiting
- T1, T2
- Free
- lockA 0/1, lockB 0/1
-
Search for a cycle
Follow the arrows and you come back to where you started: T1 → lockB → T2 → lockA → T1. Every resource on it has one instance, so each process waits for something only the next one can give back. Nobody on the cycle can ever move: this is a deadlock.
- Cycle
- T1 → lockB → T2 → lockA → T1
- Deadlock
- yes
- Waiting
- T1, T2
- Free
- lockA 0/1, lockB 0/1
-
Deadlock: T1 and T2 wait for ever
T1 and T2 can never get what they ask for: each one needs something another of them holds, and none of them will let go. Only an outside action (killing one, or a timeout) can break this.
- Cycle
- T1 → lockB → T2 → lockA → T1
- Deadlock
- yes
- Waiting
- T1, T2
- Free
- lockA 0/1, lockB 0/1
Follow the arrows from T1: T1 asks for lockB, lockB is held by T2, T2 asks for lockA, lockA is held by T1, and you are back where you started. That is a cycle. In a graph where every resource has a single instance, a cycle is the same thing as a deadlock.
The simulator searched for the cycle and then checked whether either thread could finish. Neither could. Deadlock is not about being slow. It is a state from which waiting alone can never get you out.
The four conditions
For this to happen, four things must all be true at once. They are known as the Coffman conditions, after the researcher who listed them.
- Mutual exclusion. A resource can be held by only one process at a time. A lock is exactly that.
- Hold and wait. A process holds a resource while it waits for another. T1 kept lockA while it asked for lockB.
- No preemption. A resource cannot be taken away from its holder: only the holder can release it. Nobody could take lockB from T2.
- Circular wait. There is a loop of processes, each waiting for a resource held by the next. That is the cycle in the graph.
All four are necessary. Take away any one and deadlock cannot happen, and that is the whole idea behind preventing it (Lesson 5.2). If a resource could be shared by many (no mutual exclusion), nobody would wait. If a thread had to take both locks or neither (no hold and wait), nobody would sit holding one. If the OS could take a lock away (preemption), a waiting thread could grab it. And if everybody takes locks in the same order, no loop can form.
A cycle is not always a deadlock
A resource may have more than one instance: a pool of identical things, like several free connections to a database, or two identical printers. A request for the resource can be met by any free instance. That changes the story: with several instances, a cycle can exist while somebody outside it will soon give an instance back.
Here, R1 has two instances. P1 and P3 each hold one. P2 holds R2. P1 asks for R2 and P2 asks for R1. The arrows form the same loop as before.
A cycle that is not a deadlock
-
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 3 processs (P1, P3, P2) will use R1 (2 instances) and R2.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 2/2, R2 1/1
-
P1 holds R1
P1 is given R1. The arrow runs from the resource to the process: it is assigned to P1, and nobody else can use that instance until P1 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 1/2, R2 1/1
-
P3 holds R1
P3 is given R1. The arrow runs from the resource to the process: it is assigned to P3, and nobody else can use that instance until P3 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 0/2, R2 1/1
-
P2 holds R2
P2 is given R2. The arrow runs from the resource to the process: it is assigned to P2, and nobody else can use that instance until P2 lets go.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 0/2, R2 0/1
-
P1 asks for R2
P1 asks for R2, but every instance is free. P1 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- none
- Deadlock
- no
- Waiting
- P1
- Free
- R1 0/2, R2 0/1
-
P2 asks for R1
P2 asks for R1, but every instance is free. P2 must wait. The arrow runs from the process to the resource: it is a request, not yet an assignment.
- Cycle
- P1 → R2 → P2 → R1 → P1
- Deadlock
- no
- Waiting
- P1, P2
- Free
- R1 0/2, R2 0/1
-
Search for a cycle
Follow the arrows and you come back to where you started: P1 → R2 → P2 → R1 → P1. A resource on this cycle has more than one instance, so the cycle alone proves nothing. Check whether anyone can finish and give an instance back.
- Cycle
- P1 → R2 → P2 → R1 → P1
- Deadlock
- no
- Waiting
- P1, P2
- Free
- R1 0/2, R2 0/1
-
P3 can finish
P3 needs nothing more. It runs to the end and releases R1, which may free somebody else.
- Cycle
- P1 → R2 → P2 → R1 → P1
- Deadlock
- no
- Waiting
- P1
- Free
- R1 1/2, R2 0/1
-
P2 can finish
P2 asks for R1 and a free instance is there, so it gets it. It runs to the end and releases R2, which may free somebody else.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 1/2, R2 1/1
-
P1 can finish
P1 asks for R2 and a free instance is there, so it gets it. It runs to the end and releases R1, which may free somebody else.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 2/2, R2 1/1
-
No deadlock: everyone finishes
Every process could finish, so the state is not deadlocked.
- Cycle
- none
- Deadlock
- no
- Waiting
- nobody
- Free
- R1 2/2, R2 1/1
The reduction steps show the chain: P3 finishes and releases its instance, P2 now gets R1 and finishes and releases R2, and finally P1 gets R2. The rule is:
- If every resource has one instance, a cycle means deadlock.
- If a resource has several instances, a cycle is necessary but not sufficient. You have to check whether the processes outside the loop can finish and free an instance.
- No cycle at all always means no deadlock.
Now press Try it and turn this into a real deadlock. Remove the line hold P3 R1 and change resource R1 x2 to resource R1. The loop is the same, but now nobody outside it can release anything. Then add the line hold P3 R1 back without the x2, and read the error: a one-instance resource cannot be held twice.
In the real world
Deadlocks are one of the classic bugs in programs with threads. Java makes them easy to find: jstack (or kill -3 on the Java process) prints every thread’s state, and when it finds a cycle of locks it prints a section headed Found one Java-level deadlock, with the threads and the locks they hold and want.
The Linux kernel has its own checker, lockdep, which records the order in which every kind of lock is taken. It warns of a possible deadlock the first time two code paths take the same locks in opposite orders, even if no deadlock has actually happened yet.
Recap
- Deadlock: a group of processes where each waits for something only another one in the group can give, so none can ever move. The CPU is idle and nothing crashes.
- It needs all four conditions: mutual exclusion, hold and wait, no preemption and circular wait.
- In a resource-allocation graph, an arrow from a resource to a process means held, and an arrow from a process to a resource means requested.
- With one instance per resource, a cycle means deadlock. With several instances, a cycle is necessary but not sufficient: you must check whether anyone can finish and give an instance back.
- A waiting process is not necessarily deadlocked. It is only deadlocked if the one it waits for can never finish.
Quick check
Quick check
-
Circular wait means a chain of processes in which each one waits for a resource held by the next, and the last waits for the first. The other conditions must hold too, but the loop is what makes the wait endless.
-
All four conditions are necessary, so removing any single one makes deadlock impossible. Which one is practical to remove depends on the resource.
-
Arrows into a resource are requests. Arrows out of a resource are assignments.
-
With several instances, a cycle is only a warning sign. The reduction (let everyone who can finish do so) decides whether anyone is really stuck.
Join the discussion