Sub topic 3
The four necessary conditions
Prerequisites and scope
This chapter assumes you know what a process and a critical section are, and that you have used locks or semaphores for mutual exclusion. It also assumes you can read a directed graph and spot a cycle. The previous chapter defined deadlock as a set of processes where each one waits for a resource held by another process in the set. Here we ask what must be true of a system for that to happen.
In 1971, E. G. Coffman and his coauthors showed that deadlock can occur only if four conditions hold at the same time. Every prevention technique in later chapters works by making at least one of them false. GATE often tests this directly, so you need to state each condition precisely and know which technique targets which.
The four conditions
- Mutual exclusion. At least one resource is non-sharable. Only one process can use it at a time. A process that requests it while it is held must wait.
- Hold and wait. A process holds at least one resource and also waits to acquire more resources that other processes hold.
- No preemption. A resource cannot be taken away from a process by force. Only the holder can release it, and it does so voluntarily after finishing its task.
- Circular wait. There is a set of waiting processes {P0, P1, ..., Pn} such that P0 waits for a resource held by P1, P1 waits for one held by P2, and so on. Pn waits for one held by P0.
The conditions are necessary, not sufficient. If any one is false, deadlock cannot occur. If all four are true, deadlock may or may not have occurred. With single-instance resources, circular wait together with the other three does mean deadlock. With multiple instances it does not always, as the example below shows.
Looking at each condition
Mutual exclusion
Some resources are inherently non-sharable. A mutex, a printer being written to, or a tape drive can serve one process at a time. Other resources are sharable. Several processes can read a read-only file at once, and such a file never causes a process to wait, so it cannot take part in a deadlock.
You usually cannot negate this condition in general, because many resources are non-sharable by nature. If two writers could update the same file at once, the data would be corrupted.
Hold and wait
The process must hold something while it waits. If a process requests everything it needs at once, or holds nothing when it requests, this condition fails. A process that requests a resource while holding nothing can block, but it cannot be part of a hold-and-wait chain.
No preemption
If the system can take a resource from a waiting process, give it to someone else, and return it later, the waiting process no longer blocks others forever. Preemption works well for state that can be saved and restored, such as CPU registers or memory pages. It works badly for a lock held in the middle of updating a shared structure, or a printer halfway through a page.
Circular wait
This is the condition that appears as a cycle in the resource-allocation graph. It is a consequence of how processes happen to request resources. Hold and wait plus no preemption plus mutual exclusion give processes the chance to block each other, and circular wait is the final arrangement that closes the loop.
A worked example with locks
Two threads take two locks in opposite orders. This program can deadlock. If thread one gets lock_a and thread two gets lock_b before either takes its second lock, neither can proceed.
import threading
import time
lock_a = threading.Lock()
lock_b = threading.Lock()
def task_one():
with lock_a:
time.sleep(0.1) # widen the window so the deadlock is likely
with lock_b:
print("task_one done")
def task_two():
with lock_b:
time.sleep(0.1)
with lock_a:
print("task_two done")
t1 = threading.Thread(target=task_one, daemon=True)
t2 = threading.Thread(target=task_two, daemon=True)
t1.start()
t2.start()
t1.join(timeout=2)
t2.join(timeout=2)
print("deadlocked" if t1.is_alive() and t2.is_alive() else "finished")The daemon=True flag and the join timeout let the program exit even when the threads are stuck. It prints deadlocked because all four conditions hold:
| Condition | How it holds here |
|---|---|
| Mutual exclusion | A `threading.Lock` can have only one owner. |
| Hold and wait | Each thread holds one lock and waits for the other. |
| No preemption | Python cannot force a thread to release a lock. |
| Circular wait | task_one waits for task_two, and task_two waits for task_one. |
If both functions took lock_a first and then lock_b, circular wait would be impossible, and the program would always finish. The ordered-locking example in the topic spine does exactly this.
Which technique breaks which condition
Prevention is the strategy of designing the system so that at least one condition can never hold. The table summarizes the usual approaches. A later chapter covers them in detail.
| Condition negated | Typical technique | Main cost |
|---|---|---|
| Mutual exclusion | Make resources sharable, for example read-only files or spooling | Not possible for inherently non-sharable resources |
| Hold and wait | Request all resources before starting, or release everything before requesting more | Low utilization, and starvation of processes that need popular resources |
| No preemption | Take resources from a waiting process, or preempt from the holder when a higher-priority process asks | Only works for resources whose state can be saved and restored |
| Circular wait | Number the resource types and require requests in increasing order | The programmer must follow the fixed order |
Of these four, negating circular wait is the one used most in practice, because it needs no special hardware and no change to how resources behave.
Why the conditions are not sufficient
Having all four conditions at one moment does not prove that processes are deadlocked when a resource type has several instances. Consider a resource type R with two instances. Process P1 holds one instance and waits for a resource of type S. Process P2 holds the other instance of R. Process P3 holds the only instance of S, but P3 is not waiting for anything and will soon release it.
A cycle might look possible in the graph, but a process outside the cycle can release an instance and break it. Here P3 finishes, releases S, P1 obtains S, completes, and releases R. No process waits forever. Mutual exclusion, hold and wait, and no preemption all hold. The waiting is only temporary because someone outside the waiting set can still make progress.
The practical rule for single-instance resources is that a cycle in the resource-allocation graph is both necessary and sufficient for deadlock. For multiple instances, a cycle is necessary but not sufficient. The next chapter on resource-allocation graphs works through this in detail.
Common errors
- Treating the conditions as sufficient. Four true conditions do not always mean a deadlock exists. They mean a deadlock is possible.
- Confusing hold and wait with circular wait. Hold and wait is about one process, which holds something and wants more. Circular wait is about a loop across several processes.
- Saying mutual exclusion can always be removed. It cannot, for non-sharable resources. Prevention schemes therefore focus on the other three conditions.
- Mixing up deadlock with starvation. In starvation a process waits indefinitely, but other processes keep running. In deadlock, every process in the set is blocked and none can make progress without outside help.
- Assuming preemption is the same as time slicing. Time slicing preempts the CPU. The no-preemption condition concerns the other resources a process holds, such as locks and devices.
Checking your understanding
Exercise
A system uses the rule "if a process requests a resource that is not available, all resources it currently holds are released, and it restarts the request later with its old resources included in the new request." Which Coffman condition does this negate, and what is one drawback?
Show answer
It negates no preemption, because the system takes the held resources from a waiting process. A drawback is that the process loses the work it did with those resources unless their state can be saved, and it can starve if it keeps being forced to release and retry.
Exercise
Four processes each need two units of the same resource type, and the system has five units. Can all four Coffman conditions hold at once, and does a deadlock necessarily follow?
Show answer
Yes, all four can hold. Mutual exclusion applies because units are used by one process at a time, and each process can hold one unit while waiting for a second. If each process holds one unit, one unit remains free. Any process that receives it can finish, so no deadlock results. The conditions held, but with five units a deadlock cannot occur. A deadlock needs eight units to be short, with four processes holding one unit each and none free, so that needs only four units in total. With five units it cannot happen.