Sub topic 6
Deadlock Prevention: Breaking One Coffman Condition
This chapter assumes you know processes, threads, critical sections, locks or semaphores, directed cycles, and basic vector or matrix comparisons. The goal is to prevent deadlock by ensuring that at least one necessary condition can never hold.
The prevention principle
A deadlock requires all four Coffman conditions:
- Mutual exclusion: at least one resource is non-shareable.
- Hold and wait: a process holds resources while waiting for more.
- No preemption: a resource cannot be forcibly taken from its holder.
- Circular wait: processes form a cycle in which each waits for a resource held by the next process.
These conditions are necessary, but none alone is sufficient. Deadlock prevention deliberately breaks one condition. This can reduce resource utilization or make programs more complex, so the chosen technique must fit the system.
| Condition to break | Prevention technique | Main cost |
|---|---|---|
| Mutual exclusion | Make resources shareable where possible | Not possible for inherently exclusive devices |
| Hold and wait | Request all resources together or release before requesting more | Lower concurrency and possible starvation |
| No preemption | Preempt resources when a request cannot be satisfied | Some resources cannot be safely preempted |
| Circular wait | Impose a global order on resource acquisition | Programs must follow the ordering rule |
Breaking mutual exclusion
Some resources can be shared safely. For example, multiple processes can read the same read-only file. Spooling can also turn an exclusive device, such as a printer, into a queue of requests handled later.
This condition cannot usually be removed for a mutex-protected data structure, a printer while printing, or a device that supports only one active operation. Therefore, operating systems more often break one of the other three conditions.
Breaking hold and wait
A process can be required to request every resource it will need before it begins. If all resources are unavailable, it waits without holding any resource.
Another policy is to require a process to release all currently held resources before requesting a new one. Both policies prevent a process from holding one resource while waiting for another.
The disadvantages are important:
- The process may hold resources for longer than necessary.
- The process may not know its maximum resource needs in advance.
- Resource utilization can decrease.
- A process may repeatedly lose resources and wait again.
Breaking no preemption
If a process holding resources requests another unavailable resource, the system can force it to release its held resources. The process later retries after obtaining the resources it needs.
This works best for resources whose state can be saved and restored, such as CPU registers or memory pages. It is unsafe or impractical for some devices and partially completed I/O operations.
Breaking circular wait
Assign every resource type a unique number. A process may request resources only in increasing order. If a process holds resource 2, it cannot later request resource 1.
A cycle would require resource numbers to increase around the cycle and eventually return to the starting point. That is impossible. Thus, the ordering rule breaks circular wait.
import threading
first = threading.Lock()
second = threading.Lock()
def worker(name):
# Every worker acquires locks in the same global order.
with first:
with second:
print(f"{name} used both resources")
threads = [
threading.Thread(target=worker, args=("P1",)),
threading.Thread(target=worker, args=("P2",)),
]
for thread in threads:
thread.start()
for thread in threads:
thread.join()The locks may still make a thread wait, but they cannot create a circular wait if every thread follows the same order.
GATE shortcut
To identify a prevention method, ask which condition the rule directly makes impossible. “Request all resources at once” breaks hold and wait. “Release resources if the next request is unavailable” breaks hold and wait or enables preemption. “Use a fixed lock order” breaks circular wait.
Exercise
Two processes can each request locks A and B. What rule prevents deadlock without requiring either process to know its maximum future resource needs?
Show answer
Assign an order, such as A before B, and require every process to acquire locks only in that order. This breaks circular wait.