Sub topic 11
Detection with a wait-for graph
This chapter assumes you can read a resource-allocation graph (RAG), where a request edge goes from a process to a resource and an assignment edge goes from a resource to a process. It also assumes you know what a cycle in a directed graph is, and that you have met the four Coffman conditions. Here the system has only one instance of each resource type. In that case deadlock detection reduces to finding a cycle in a smaller graph.
From a RAG to a wait-for graph
A RAG has two kinds of nodes, processes and resources. For detection you only need to know which process is blocked on which other process. A wait-for graph (WFG) keeps only the process nodes. It has an edge Pi → Pj when Pi is waiting for a resource that Pj currently holds.
You build it from a RAG by collapsing the resource nodes. If the RAG has Pi → Rq (Pi requests Rq) and Rq → Pj (Rq is assigned to Pj), replace the pair with Pi → Pj, then delete every resource node.
Consider this system:
- P1 holds R1 and requests R2.
- P2 holds R2 and requests R3.
- P3 holds R3 and requests R1.
- P4 holds R4 and requests R1.
Collapsing the resources gives P1 → P2, P2 → P3, P3 → P1 and P4 → P1.
P1, P2 and P3 are deadlocked. P4 is blocked too, but it is not on the cycle. If the cycle is broken, P1 releases R1 and P4 can proceed. P4 is therefore not part of the deadlock itself. It is a victim of it.
Why a cycle is enough here
With a single instance per resource type, a cycle in the WFG corresponds to a cycle in the RAG. Every resource on that cycle has exactly one holder, and that holder is itself waiting. Nobody on the cycle can release anything, so all four Coffman conditions hold and the processes on the cycle are deadlocked.
The converse also holds. If there is a deadlock, the processes involved wait on each other in a closed chain, which is a cycle. So for single instances:
- A cycle in the WFG is both necessary and sufficient for deadlock.
- No cycle means no deadlock.
This is what separates the WFG from the multiple-instance case. When a resource type has several instances, a cycle in the RAG is necessary but not sufficient. The WFG cannot represent that case, because "Pi waits for the holder of Rq" is ambiguous when Rq has several holders and any one of them might release an instance. That case needs the matrix-based algorithm in the next chapter.
Finding the cycle with depth-first search
The detector runs depth-first search (DFS) over the WFG and colors each node:
- White: not visited yet.
- Gray: on the current DFS path.
- Black: fully explored, with no cycle reachable from it.
If DFS follows an edge to a gray node, that node is an ancestor on the current path, so the edge closes a cycle. An edge to a black node cannot close a cycle, because everything reachable from a black node was already explored and found acyclic.
The code below builds the WFG from "who holds what" and "who wants what", then runs the check.
def build_wait_for(holds, requests):
# holds: resource -> process holding it (single instance per resource)
# requests: process -> list of resources it is waiting for
wait_for = {}
for p, wanted in requests.items():
wait_for[p] = [holds[r] for r in wanted if r in holds and holds[r] != p]
return wait_for
def has_cycle(wait_for):
# wait_for maps each process to the list of processes it is waiting on
WHITE, GRAY, BLACK = 0, 1, 2
color = {}
def visit(u):
color[u] = GRAY
for v in wait_for.get(u, []):
state = color.get(v, WHITE)
if state == GRAY:
return True
if state == WHITE and visit(v):
return True
color[u] = BLACK
return False
return any(color.get(u, WHITE) == WHITE and visit(u) for u in list(wait_for))
holds = {"R1": "P1", "R2": "P2", "R3": "P3", "R4": "P4"}
requests = {"P1": ["R2"], "P2": ["R3"], "P3": ["R1"], "P4": ["R1"]}
wf = build_wait_for(holds, requests)
print(wf) # {'P1': ['P2'], 'P2': ['P3'], 'P3': ['P1'], 'P4': ['P1']}
print(has_cycle(wf)) # True
requests["P3"] = [] # P3 no longer waits for R1
print(has_cycle(build_wait_for(holds, requests))) # FalseTracing the first call: DFS starts at P1 (gray), moves to P2 (gray), then P3 (gray). P3 has an edge to P1, which is gray, so the function returns True. P4 is never visited, and the answer is already known.
Cost
DFS visits each node and edge once, so the cost is O(V + E) with adjacency lists, or O(n²) with an n × n adjacency matrix. GATE questions usually state the cost as O(n²) for n processes, because a WFG can have up to about n² edges. Building the WFG from a RAG is the same order of work.
Details that decide exam answers
Which processes are deadlocked
Report only the processes that lie on a cycle. Processes that merely point into a cycle, like P4 above, are blocked but are not deadlocked. Their waiting ends when the cycle is broken. If the question asks "which processes are deadlocked", do not include them.
Several cycles
A WFG can contain more than one cycle. Terminating one process may break one cycle and leave another. If two cycles share a process, removing that shared process breaks both. Recovery cost depends on how the cycles overlap, which the recovery chapter covers.
Self-loops
With single-instance resources, a process cannot wait for a resource it already holds, so a self-loop Pi → Pi should not appear. That is why build_wait_for skips holders equal to the requester. If a question's graph shows a self-loop, treat it as a deadlock of that one process.
A process that holds but does not wait
A process that holds resources and is not requesting anything has no outgoing edge. It is running or will finish, so it cannot be on a cycle. Many students add edges for held resources by mistake. Edges come only from requests.
The graph changes over time
The WFG is a snapshot. A system can maintain it dynamically: add an edge when a request blocks, and remove it when the request is granted or the process releases. It runs the cycle check periodically or at chosen events, not continuously.
When to run the detector
Detection has a cost, so the OS must choose how often to pay it.
| Strategy | Benefit | Drawback |
|---|---|---|
| Run on every blocked request | Finds the deadlock at the moment it forms, and the request that closed the cycle identifies the culprit | Highest overhead |
| Run at fixed intervals | Cheap and predictable | A deadlock can sit unnoticed until the next run, and several cycles may have formed, so it is hard to tell which request caused them |
| Run when CPU utilization drops | Deadlocked processes stop using the CPU, so a drop is a useful signal | Detection can be late if other processes keep the CPU busy |
Running at an arbitrary point means the graph may contain many cycles. The OS cannot say which of the deadlocked processes "caused" the deadlock, which makes recovery choices harder.
Common mistakes
- Using a WFG with multi-instance resources. The cycle test stops being sufficient.
- Drawing edges in the wrong direction. The edge goes from the waiter to the holder, Pi → Pj when Pi waits on Pj.
- Listing every process reachable from a cycle as deadlocked. Only the processes on the cycle are deadlocked.
- Treating a cycle in a RAG with multiple instances as a deadlock. That cycle is only a warning sign.
- Forgetting that detection only reports the problem. The OS still has to recover, as the next chapters describe.
Exercise
A system has single-instance resources R1 to R4. P1 holds R1 and requests R2. P2 holds R2 and requests R3. P3 holds R3 and requests R4. P4 holds R4 and requests R1. P5 holds nothing and requests R2. Draw the wait-for graph and state which processes are deadlocked.
Show answer
Edges: P1 → P2 (R2 is held by P2), P2 → P3 (R3 is held by P3), P3 → P4 (R4 is held by P4), P4 → P1 (R1 is held by P1) and P5 → P2 (R2 is held by P2). P1 → P2 → P3 → P4 → P1 is a cycle, so P1, P2, P3 and P4 are deadlocked. P5 waits on P2 but is not on any cycle, so it is blocked, not deadlocked.
Exercise
The WFG of a system with single-instance resources has edges P1 → P2, P2 → P3, P3 → P4 and P1 → P4. Is there a deadlock? What single new edge would create one?
Show answer
There is no cycle, because every path leads toward P4, which has no outgoing edge. So there is no deadlock. Adding P4 → P1 creates the cycle P1 → P2 → P3 → P4 → P1. Adding P4 → P2 or P4 → P3 would also create a cycle.
Exercise
Why can the cycle test not be used directly on a RAG where one resource type has two instances?
Show answer
With two instances, a process waiting on that type may be satisfied when either holder releases an instance. A cycle through that resource may therefore not be a deadlock, because a holder outside the cycle may release an instance and let the waiter continue. A cycle is necessary but not sufficient, so the matrix-based detection algorithm for multiple instances must be used instead.