Picture a narrow corridor where two people meet face to face. Neither can pass without the other stepping aside, and neither will step aside first, because each is waiting for the other to move. The two people are simply stuck, and standing still will not fix anything. That quiet stand-off is the heart of a deadlock.

A deadlock is a state in which two or more processes are each waiting for something that another waiting process holds. A process is a program that is running, or a part of one. Processes need resources to finish their work: a file, a piece of memory, a printer, a connection. When one process is using a resource, another that needs it must wait. A deadlock is what happens when the waiting goes around in a circle and no one in it can move first.

What a deadlock is

Imagine two processes, A and B. Process A holds one resource and needs a second one to finish. Process B holds that second resource and needs the first one to finish. A waits for B. B waits for A. Neither can give up what it holds, because neither has finished, and neither can finish, because neither can get what it is waiting for. The circle is closed, and time does not open it.

The important thing is that a deadlock is not a crash. A crashed program has stopped because something went wrong inside it. A deadlocked program has not gone wrong in that way. It is still running, still healthy, and still doing exactly what it was told to do, which is to wait until the thing it needs becomes free. The trouble is that the thing it needs will never become free, because the process holding it is itself waiting for something else. Both sides behave correctly, yet nothing moves.

How the stand-off forms

A deadlock needs a few conditions to be true at the same time. The first is that a resource can only be used by one process at a time. A file open for writing cannot be written by two processes at once without the contents becoming a mess, so the system gives it to one process and makes the others wait. It is also the first ingredient of a deadlock.

The second condition is that a process can hold one resource while waiting for another. It does not have to give up what it already has before asking for more. But when two processes each hold something the other wants, holding while waiting turns a simple queue into a circle.

The third condition is that a resource cannot simply be taken away from the process holding it. Resources are released only when the holding process finishes its work. In a deadlock, that moment never arrives. The fourth condition is the circle itself: the waiting forms a loop, and the chain comes back to where it started. Take away any one of these four conditions and the deadlock cannot form. That fact is the key to prevention.

A closed dark laptop and a single coral-red notebook on a dark wooden desk, lit by a warm desk lamp in a near-black room

A simple example

Consider two small programs that both need to update the same two records in a database. The first locks the first record, intending to update it and then move on to the second. The second locks the second record, intending to update it and then move on to the first. But now the first waits for the second record, which the second program holds, and the second waits for the first record, which the first program holds. Both sit still, each holding half of what both need, and neither can finish.

Timing plays a large part. Deadlocks often depend on the exact order in which things happen. Run the same two programs a hundred times and they may deadlock only once, on the run where the first program locked its record a fraction of a second before the second locked its own. That is what makes deadlocks so hard to track down.

How teams spot a deadlock

Spotting a deadlock usually starts with noticing that something has gone quiet. A task that normally finishes in seconds has been running for minutes. A page on a server that normally loads at once never arrives. But when the quiet lasts, and the same work keeps stalling in the same place, a deadlock becomes a serious suspect. Teams then look at what each process is doing and what it is waiting for. If process A waits for a resource held by process B, and B waits for one held by A, the circle is visible, and the diagnosis is certain.

Two open blank notebooks facing each other on a dark wooden table, with a single coral-red pen lying between them

How teams break the stand-off

Once a deadlock is found, breaking it is straightforward in principle and delicate in practice. Somebody has to give way. The usual approach is to choose one stuck process, stop it, and take back the resources it held. The other process can then finish, release its own resources, and the system can carry on. The stopped process can be started again afterwards, and will usually finish the second time. Some work is lost.

Choosing which process to stop is a judgement call. Teams often pick the one that has done the least work, or whose work is easiest to repeat. The part of the computer that manages processes and resources is the operating system, and at its centre sits the kernel, the core that decides who gets what, and when. In some systems the kernel chooses; in others, a person does.

Stopping deadlocks before they start

Breaking a deadlock is useful, but preventing one is better, and prevention comes back to the four conditions that allow one to form. The simplest prevention is to make processes ask for everything they need at once, before they start work, rather than collecting resources one at a time. Another approach is to put the resources in a fixed order, and require every process to ask for them in that order. If every process asks for the first record before the second, two processes can never hold opposite halves of the same pair. A third approach is to let a process hold resources only briefly, and make it give them up if it cannot get what it needs, then try again later.

A coiled black cable on a dark wooden surface with a single coral-red plug at its end

A quiet kind of failure

A deadlock is a quiet kind of failure. There is no crash, no alarm, and no error to point at. Two processes simply wait, each holding what the other needs, each doing exactly what it was asked to do. What breaks it is never patience, because patience changes nothing. What breaks it is a decision: one process gives way, or is made to give way, and the other can finally finish. Ask for resources in a steady order, or ask for them all at once, or give them up gracefully when the wait goes on too long. And when a stand-off does form, as it sometimes will, break it quickly and let the work carry on.

Back to the journalDiscuss a project