Sub topic 6
Prevention: breaking one Coffman condition
This chapter assumes you know the four Coffman conditions (mutual exclusion, hold and wait, no preemption, circular wait), the basic resource model of processes and resource types, and how to use locks in a program. Prevention is the strictest of the handling strategies. It designs the system so that at least one condition can never hold. If one necessary condition is impossible, deadlock is impossible.
The idea in one table
Each prevention technique attacks exactly one condition. GATE questions often ask you to match them.
| Condition broken | Technique | Main cost |
|---|---|---|
| Mutual exclusion | Make resources sharable, or spool them | Not possible for many resources |
| Hold and wait | Request everything at once, or release before requesting more | Low utilization, starvation |
| No preemption | Take resources away from a waiting process | Only works for state that can be saved and restored |
| Circular wait | Number resource types and request in increasing order | Fixed order is hard to design |
Breaking mutual exclusion
Mutual exclusion means a resource can be used by only one process at a time. If a resource is sharable, processes never have to wait for it, so it cannot take part in a deadlock. Read-only files are the standard example. Any number of processes can open them at once.
The difficulty is that some resources are inherently non-sharable. A mutex lock protecting a critical section is non-sharable by definition. A printer cannot interleave the output of two jobs. For this reason, prevention can almost never rely on this technique alone.
Spooling softens the problem for some devices. Processes write their output to a spool file on disk. Only one daemon process talks to the printer. No process ever waits for the printer itself, so the printer cannot be part of a cycle. The spool area is still a limited resource, though. If the disk fills up while several jobs are half written, those jobs can deadlock on disk space.
Breaking hold and wait
Hold and wait means a process holds at least one resource while waiting for another. There are two classic ways to remove it.
- All-at-once allocation. A process requests all the resources it will need before it starts, and it runs only when every request is granted. It never holds something while waiting, because it either has everything or has nothing.
- Release before requesting. A process may request resources only when it holds none. If it needs more, it must release everything it holds and then request the larger set again.
Both have drawbacks that you should be able to state in an exam answer.
- Low resource utilization. A process might need a tape drive only at the end of a long computation, but under all-at-once allocation it holds the drive from the start. Nobody else can use it.
- Starvation. A process that needs several popular resources may never see them all free at the same moment. Other processes keep grabbing one or two of them.
- Advance knowledge. The process must know its full needs before it runs. This is often unrealistic, because needs can depend on input data.
Starvation is a different problem from deadlock. The system keeps making progress overall, but one process waits indefinitely. The tradeoff is that prevention removes deadlock but may introduce starvation.
Breaking no preemption
If a process holding resources requests another one that is not available, a preemption rule can take its held resources away. There are two common variants.
- Self-preemption. If the request cannot be granted, the requesting process releases everything it holds. It restarts the request later with its old resources added to the list of things it is waiting for. It resumes only when it can get all of them back plus the new one.
- Preemption from a waiter. When process P requests a resource that is held by process Q, the system checks whether Q is itself waiting for some other resource. If it is, the system takes the resource from Q and gives it to P. If Q is running, P waits.
This technique suits resources whose state can be saved and restored cheaply. CPU registers and memory pages are good examples. It is a poor fit for resources like a mutex held in the middle of a critical section, or a printer in the middle of a page. Taking those away would corrupt data or waste work.
Breaking circular wait
Circular wait means there is a cycle of processes, each waiting for a resource held by the next one. The standard prevention is to impose a total order on all resource types.
Assign each resource type a unique integer through a function F. For example, F(tape drive) = 1, F(disk) = 5, F(printer) = 12. Then enforce one rule: a process may request a resource only if its number is higher than every resource it currently holds.
An equivalent form of the rule is that a process that wants a lower-numbered resource must first release the higher-numbered ones it holds.
Why this removes cycles
Suppose a cycle P0, P1, ..., Pk existed, where Pi holds a resource Ri and requests R(i+1), and Pk requests R0. By the rule, each request goes to a higher number than the resource already held. So F(R0) < F(R1) < ... < F(Rk) < F(R0). That is impossible, because a number cannot be less than itself. Therefore no cycle can form.
The proof only works if the order is truly global. If two code paths disagree about which resource comes first, a cycle becomes possible again.
Lock ordering in code
The same idea is used for locks in ordinary programs. Every thread that needs both locks acquires them in the same order.
import threading
lock_a = threading.Lock()
lock_b = threading.Lock()
def task_one():
with lock_a:
with lock_b:
pass # use both resources
def task_two():
# Same global order (a, then b) as task_one, so no circular wait can form
with lock_a:
with lock_b:
pass # use both resources
threads = [threading.Thread(target=task_one), threading.Thread(target=task_two)]
for t in threads:
t.start()
for t in threads:
t.join()If tasktwo took lockb first and then lock_a, each thread could grab its first lock and wait forever for the other. That is a circular wait with two processes.
Limits of ordering
- The order must match the natural order in which processes use resources. A bad order forces processes to acquire resources long before they need them, which has the same utilization problem as all-at-once allocation.
- Programmers must follow the order everywhere. Nothing in the language checks it unless a tool does.
- Locks chosen at run time, such as locking two accounts in a transfer, need a rule to compute the order. A common one is to lock the account with the smaller ID first.
import threading
class Account:
def __init__(self, ident, balance):
self.ident = ident
self.balance = balance
self.lock = threading.Lock()
def transfer(src, dst, amount):
first, second = (src, dst) if src.ident < dst.ident else (dst, src)
with first.lock:
with second.lock:
src.balance -= amount
dst.balance += amount
a = Account(1, 100)
b = Account(2, 100)
t1 = threading.Thread(target=transfer, args=(a, b, 10))
t2 = threading.Thread(target=transfer, args=(b, a, 20))
t1.start(); t2.start()
t1.join(); t2.join()
print(a.balance, b.balance) # 110 90Without the ordering rule, transfer(a, b, ...) and transfer(b, a, ...) running at the same time could deadlock. With the rule, both threads lock account 1 first.
Common mistakes
- Saying that prevention breaks "deadlock". It breaks one of the four conditions. Name the condition.
- Claiming that mutual exclusion can usually be removed. It cannot for most resources, so it is seldom the chosen technique.
- Forgetting that hold-and-wait prevention can cause starvation, not only low utilization.
- Applying a resource ordering only to some resources. The order must cover every resource type that can be part of a wait.
- Confusing ordering with avoidance. Ordering needs no knowledge of future requests, only a fixed rule. Avoidance, covered in later chapters, needs each process's maximum claim in advance.
Exam shortcuts
- If a question asks which condition a technique breaks, match by keyword: "request all at once" gives hold and wait, "numbering" or "ordering" gives circular wait, "take away" gives no preemption, "spooling" or "sharable" gives mutual exclusion.
- Prevention is static. It restricts how requests are made, whether or not a deadlock was ever going to happen. This is why it is conservative and can reduce utilization.
- To check whether a given request sequence respects an ordering, compare each new request's number with the highest number the process currently holds.
Exercise
Four resource types have numbers F(R1) = 2, F(R2) = 4, F(R3) = 7, F(R4) = 9. Process P holds R2 and R3. Under the rule "request only resources with a higher number than all held ones", which of R1, R4 can P request next, and why does the rule rule out a circular wait?
Show answer
P can request R4 (9 > 7, the highest number it holds) but not R1 (2 < 7). To use R1, P must first release R2 and R3. A cycle would need F(R0) < F(R1) < ... < F(R0) around the loop, which is impossible, so circular wait cannot occur.
Exercise
A system forces every process to request all its resources before it starts running. Which Coffman condition does this break, and name two disadvantages.
Show answer
It breaks hold and wait, because a process either holds everything it needs or holds nothing while waiting. Disadvantages: low resource utilization (resources are held long before they are used) and possible starvation (a process needing many popular resources may never find them all free at once). Needing to know all requirements in advance is a third.
Exercise
Why is breaking mutual exclusion rarely used as the main prevention technique for locks and printers?
Show answer
Locks and printers are inherently non-sharable. A mutex exists to give one thread exclusive access, and a printer cannot print two jobs at once. Spooling can hide the printer from user processes, but the spool disk space itself can still become a contended resource.