Sub topic 3
The Four Necessary Conditions
This chapter assumes you know what processes, threads, locks, semaphores, and critical sections are. You should also be comfortable reading directed graphs. A deadlock is a state in which a group of processes waits forever because each process needs an event that another process in the group must cause.
The four conditions below are called the Coffman conditions. Every condition must hold at the same time for a deadlock to occur. If an operating system prevents even one condition, deadlock cannot occur.
Mutual exclusion
Mutual exclusion means that at least one resource can be used by only one process at a time. The resource has a single usable instance, or its use is protected so that simultaneous access is not allowed.
For example, one process may hold a printer, a file lock, or a mutex. Another process must wait until the first process releases it.
Mutual exclusion is often necessary for correctness. Two threads cannot safely update the same data structure at the same time unless access is synchronized. Therefore, removing mutual exclusion is not always practical.
Hold and wait
Hold and wait means that a process is holding at least one resource while waiting to acquire another resource.
For example:
- Process P1 holds lock A.
- P1 requests lock B.
- P1 keeps holding A while waiting for B.
A prevention technique is to require a process to request all resources before it starts. Another technique is to require it to release every resource before requesting a new one. Both techniques break hold and wait, but they can reduce resource utilization or cause a process to wait longer than necessary.
No preemption
No preemption means that the operating system cannot forcibly take a resource from a process. The process must release the resource voluntarily.
This condition commonly applies to locks. If P1 owns a mutex, the operating system usually cannot safely remove that ownership while P1 is inside a critical section. It might leave shared data inconsistent.
A system can break this condition when resources support safe rollback or interruption. For example, the operating system may preempt a CPU, or it may discard and restart a transaction. Preemption is much harder for resources with visible side effects, such as a printer currently printing a page.
Circular wait
Circular wait means that processes form a cycle of dependencies. Each process waits for a resource held by the next process in the cycle.
For example:
- P1 holds A and waits for B.
- P2 holds B and waits for A.
P1 cannot continue until P2 releases B. P2 cannot continue until P1 releases A. Neither process can make progress.
A common prevention method is to impose a global order on resource types. Suppose A has order 1 and B has order 2. Every process must request A before B. Then no process can hold B while waiting for A, so a circular wait cannot form.
Putting the conditions together
| Condition | Meaning | Typical prevention method |
|---|---|---|
| Mutual exclusion | A resource cannot be shared simultaneously | Make the resource shareable where safe |
| Hold and wait | A process holds resources while requesting more | Request all resources at once |
| No preemption | Resources cannot be forcibly taken | Preempt or roll back when safe |
| Circular wait | Waiting dependencies form a cycle | Use a fixed resource-order rule |
The conditions are necessary, not individually sufficient. Mutual exclusion alone does not create deadlock. Deadlock requires all four conditions together.
GATE shortcut
When a question asks which condition a technique breaks, match the wording directly:
- Request every resource before execution: breaks hold and wait.
- Release resources before requesting new ones: breaks hold and wait.
- Forcefully take a resource: breaks no preemption.
- Assign an increasing order to resource requests: breaks circular wait.
- Allow shared read access: may break mutual exclusion.
Do not conclude that a cycle always means deadlock when resource types have multiple instances. For a single instance of each resource, a cycle is strong evidence of deadlock. With multiple instances, a cycle may exist while another instance can satisfy a waiting process.
Exercise
P1 holds resource A and requests B. P2 holds B and requests A. Which Coffman conditions are present, and what rule would prevent this deadlock pattern?
Show answer
Mutual exclusion, hold and wait, no preemption, and circular wait are all present. Requiring every process to request resources in one global order, such as A before B, breaks circular wait.