Sub topic 11
Detection with a Wait-for Graph
Assume you know processes, resource-allocation graphs, locks, and directed graph cycles. This chapter focuses on systems where each resource type has exactly one instance.
What a wait-for graph represents
A wait-for graph shows which process is waiting for which other process. It removes resource nodes from a resource-allocation graph.
- Each node represents a process.
- An edge
P1 → P2means thatP1is waiting for a resource currently held byP2. - A cycle means that every process in the cycle is waiting for another process in the same cycle.
For example, consider this situation:
P1holds lockL1and waits forL2.P2holdsL2and waits forL1.
The wait-for graph contains P1 → P2 and P2 → P1. The cycle proves that the two processes are deadlocked.
Detecting a cycle
A cycle is present if graph traversal reaches a node that is already on the current traversal path. A depth-first search (DFS) can detect this condition.
Use three states:
WHITE: the process has not been visited.GRAY: the process is on the current DFS path.BLACK: the process and all reachable edges have been fully checked.
If DFS follows an edge to a GRAY process, it has found a cycle.
def has_cycle(wait_for):
# wait_for maps each process to the processes it is waiting on
WHITE, GRAY, BLACK = 0, 1, 2
color = {}
def visit(process):
color[process] = GRAY
for other in wait_for.get(process, []):
state = color.get(other, WHITE)
if state == GRAY:
return True
if state == WHITE and visit(other):
return True
color[process] = BLACK
return False
for process in wait_for:
if color.get(process, WHITE) == WHITE:
if visit(process):
return True
return False
print(has_cycle({
"P1": ["P2"],
"P2": ["P3"],
"P3": ["P1"],
})) # TrueWith n processes and e wait-for edges, this algorithm takes O(n + e) time. It checks each process and edge at most a constant number of times.
When a cycle proves deadlock
For single-instance resource types, a cycle in the wait-for graph is both necessary and sufficient for deadlock.
| Graph result | Meaning |
|---|---|
| No cycle | No deadlock exists in the represented waits |
| One or more cycles | The processes in each cycle are deadlocked |
A process outside the cycle might still be able to run. Detection should therefore report the processes that belong to a cycle, not automatically label every process as deadlocked.
Why multiple instances change the rule
The simple wait-for graph rule depends on having one instance of each resource type. With multiple instances, a process can wait for a resource type while another process holding one instance may release it before the wait becomes circular. A cycle can then exist without proving deadlock.
For multiple-instance resources, use a matrix-based detection algorithm with Available, Allocation, and Request matrices. That algorithm tests whether some unfinished process can receive its remaining request and release its allocation.
Exam shortcut
For a single-instance system:
- Build one edge from each waiting process to the process holding its required resource.
- Search for directed cycles.
- Report the processes on those cycles as deadlocked.
- Ignore acyclic chains unless they lead into a cycle.
Exercise
A wait-for graph contains edges P1 → P2, P2 → P3, P3 → P4, and P4 → P2. Which processes are deadlocked?
Show answer
P2, P3, and P4 are deadlocked because they form the cycle P2 → P3 → P4 → P2. P1 is waiting, but it is not part of the deadlocked cycle.