Sub topic 4
Resource-Allocation Graphs: When a Cycle Means Deadlock
Assume you know processes, threads, locks or semaphores, directed graphs, and basic vector notation. This chapter uses a resource-allocation graph, or RAG, to represent which resources processes hold and which resources they request.
Graph model
A resource-allocation graph is a directed graph with two kinds of nodes:
- A process is shown as a circle, such as
P1. - A resource type is shown as a rectangle, such as
R1.
A resource type may have one or more identical instances. For example, R1 might represent two printers or four memory buffers.
The edges have different meanings:
- A request edge
P1 → R1means thatP1is waiting for an instance ofR1. - An assignment edge
R1 → P1means that an instance ofR1has been allocated toP1.
For a resource with several instances, the resource node represents the type, while each instance is conceptually represented by a dot inside that node. An assignment edge consumes one instance.
Interpreting cycles
When every resource type has exactly one instance, a cycle in the RAG means deadlock.
Consider the cycle:
P1 → R2 → P2 → R1 → P1
P1 waits for R2, but R2 is held by P2. P2 waits for R1, but R1 is held by P1. Neither process can continue, release its resource, and break the cycle.
For single-instance resources, cycle detection is therefore enough:
| Resource instances | Meaning of a cycle |
|---|---|
| One instance per type | The cycle implies deadlock |
| Multiple instances possible | The cycle may or may not imply deadlock |
Why multiple instances change the result
With multiple instances, a cycle is only a warning. Another free instance may satisfy a request even though the graph contains a cycle.
Suppose R1 has two instances. P1 holds one instance of R1 and waits for R2. P2 holds R2 and waits for R1. If the second instance of R1 is free, the system can grant it to P2. Then P2 finishes and releases R2, allowing P1 to finish. The cycle does not cause deadlock.
Thus, for multi-instance resources, the graph must be combined with the number of available instances and the processes' remaining needs. A matrix-based detection algorithm is more reliable than simply searching for cycles.
Common mistakes
Do not treat every directed edge as the same. A process-to-resource edge represents a request. A resource-to-process edge represents an allocation. Reversing these directions can produce a false cycle.
Also distinguish a cycle from a deadlock:
- With one instance per resource type, a cycle and deadlock are equivalent.
- With multiple instances, deadlock implies a blocking dependency, but a cycle alone is not sufficient proof.
- A process waiting for a resource without forming a cycle may be experiencing ordinary delay or starvation, not deadlock.
A GATE-style shortcut
For a single-instance RAG, follow alternating edges from a process:
- Follow a request edge to the resource it needs.
- Follow the assignment edge from that resource to its current holder.
- Repeat until you revisit a process.
Revisiting a process proves that the wait dependency is circular. Therefore, the processes in that cycle are deadlocked.
Exercise
A system has one instance each of R1 and R2. P1 holds R1 and requests R2. P2 holds R2 and requests R1. Is the system deadlocked?
Show answer
Yes. The graph contains the cycle P1 → R2 → P2 → R1 → P1, and every resource type has one instance.