Sub topic 4
Resource-allocation graphs: when a cycle means deadlock
This chapter assumes you know what a deadlock is and can state the four Coffman conditions. It also assumes you can read a directed graph and spot a cycle in it. A resource-allocation graph (RAG) draws the system's state so you can see who holds what and who is waiting. Its main use is a quick test for deadlock, and that test depends on how many instances each resource has.
How a resource-allocation graph is drawn
A RAG is a directed graph with two kinds of vertices.
- Process vertices are drawn as circles: P1, P2, and so on.
- Resource vertices are drawn as rectangles. Each rectangle contains one dot per instance of that resource type. A resource type R1 with two printers has two dots.
There are two kinds of edges.
| Edge | Direction | Meaning |
|---|---|---|
| Request edge | Process to resource type | The process has asked for one instance of R and is blocked until it gets one. |
| Assignment edge | Instance of a resource type to process | One instance of R is currently allocated to the process. |
A request edge points at the rectangle as a whole, because the process will accept any free instance. An assignment edge starts at a specific dot, because a specific instance is held.
When a request can be satisfied, the request edge turns into an assignment edge. When the process releases the resource, the assignment edge disappears. A resource-allocation graph is therefore a snapshot of the current state, not a history.
This chapter uses only request and assignment edges. A third kind, the claim edge, is used for avoidance and appears in a later chapter.
The cycle rules
Cycles are the key to reading a RAG. The rules are:
- No cycle means no deadlock. Every process can eventually finish, because the chain of waiting always ends at a process that is not blocked.
- A cycle and one instance per resource type means deadlock. Each resource in the cycle has a single instance, and that instance is held by the next process in the cycle. Nothing outside the cycle can free it. The cycle is necessary and sufficient.
- A cycle and several instances for some resource type means possible deadlock. The cycle is necessary but not sufficient. An instance might be held by a process outside the cycle, and that process may finish and release it.
| Situation | Cycle present? | Deadlock? |
|---|---|---|
| Any graph | No | No |
| Every resource type has a single instance | Yes | Yes |
| Some resource type has several instances | Yes | Maybe. Check further. |
Rule 2 is the reason a wait-for graph (a later chapter) can reduce deadlock detection to cycle detection when every resource has one instance.
A cycle that is a deadlock
Suppose R1 and R2 each have one instance. P1 holds R1 and requests R2. P2 holds R2 and requests R1.
The cycle is R1 → P1 → R2 → P2 → R1. Both resources have one instance, so rule 2 applies and the system is deadlocked. Neither process can proceed, and neither will release what it holds. This is exactly the combination of hold and wait, no preemption, mutual exclusion, and circular wait.
A cycle that is not a deadlock
Now give R1 two instances and add a third process. P2 and P3 each hold one instance of R1. P1 holds the only instance of R2. P1 requests R1. P2 requests R2. P3 requests nothing.
The cycle is P1 → R1 → P2 → R2 → P1, but the graph is not deadlocked. P3 is not waiting for anything, so it will finish and release its instance of R1. P1 can then take that instance and finish. When P1 finishes, it releases R2, and P2 can finish.
The test is whether some instance on the cycle is held by a process that is not stuck. P3 is that process here.
Testing for deadlock by graph reduction
For multiple instances, use graph reduction. Repeat these steps:
- Find a process whose outstanding requests can all be met from the currently free instances.
- Let it finish. Remove it and its edges, which returns the instances it held to the free pool.
- Stop when no such process remains.
Any processes left over are deadlocked. If none are left, there is no deadlock. This is the same idea as the safety algorithm, using free instances and requests instead of Work and Need.
The function below implements it. Each assignment is a pair (resource, process) and each request is a pair (process, resource).
from collections import Counter
def deadlocked_processes(instances, assigned, requests):
free = dict(instances)
held = {}
procs = set()
for r, p in assigned:
free[r] -= 1
held.setdefault(p, []).append(r)
procs.add(p)
for p, r in requests:
procs.add(p)
active = set(procs)
progress = True
while progress:
progress = False
for p in sorted(active):
needs = Counter(r for q, r in requests if q == p)
if all(needs[r] <= free[r] for r in needs):
for r in held.get(p, []):
free[r] += 1
active.remove(p)
progress = True
return sorted(active)
# Single-instance cycle: both processes are stuck
print(deadlocked_processes(
{"R1": 1, "R2": 1},
[("R1", "P1"), ("R2", "P2")],
[("P1", "R2"), ("P2", "R1")],
)) # ['P1', 'P2']
# Cycle with a two-instance resource: P3 breaks the deadlock
print(deadlocked_processes(
{"R1": 2, "R2": 1},
[("R1", "P2"), ("R1", "P3"), ("R2", "P1")],
[("P1", "R1"), ("P2", "R2")],
)) # []In the first call neither process can ever have its request met, so both remain. In the second call P3 has no requests, so it finishes first and frees an instance of R1. P1 then finishes, and finally P2.
When every resource has one instance, a non-empty result means the graph has a cycle, and an empty result means it has none. The reduction works for any instance counts, which is why it is the general method.
Reading a RAG quickly in an exam
For a GATE question, work through these steps in order:
- Count the instances (dots) of each resource type.
- Look for a cycle. If none exists, answer "no deadlock" immediately.
- If a cycle exists and every resource on it has a single instance, answer "deadlock".
- If a resource on the cycle has several instances, look at each dot. Is its holder inside the cycle, or outside it?
- For each holder outside the cycle, check that it is not itself waiting on something that is stuck. If it can finish, it releases an instance, and you then re-check the cycle.
- For anything complicated, run the reduction by hand.
Statements about the graph often hide a trap. A cycle with a multi-instance resource does not guarantee deadlock. Likewise, "no cycle implies no deadlock" is always true, so a "no cycle" answer is safe to give at once.
Common mistakes
- Reversing edge directions. A request edge goes from the process to the resource. An assignment edge goes from the resource to the process. If you flip them, a cycle may appear that does not exist.
- Counting a process as stuck because it appears in the graph. A process with only assignment edges and no request edges is not waiting and will finish.
- Forgetting that a request edge points at the resource type. Any free instance can satisfy the request, so one free dot anywhere in the rectangle means the process can proceed.
- Treating "unsafe" and "deadlocked" as the same thing. A RAG with a cycle describes the current state. Safe and unsafe states, covered in a later chapter, describe what might happen in the future.
Exercise
R1 has one instance and R2 has two instances. P1 holds R1 and requests R2. P2 holds one instance of R2 and requests R1. P3 holds the other instance of R2 and requests nothing. Is the system deadlocked? Is there a cycle?
Show answer
There is a cycle: R1 → P1 → R2 → P2 → R1. R2 has two instances, so the cycle alone does not prove deadlock. P3 holds the second instance of R2 and is not waiting, so it will finish and release it. P1 can then take that instance and finish, which releases R1 for P2. The system is not deadlocked.
Exercise
A graph has no cycle. Each of three single-instance resources is held by a different process, and one more process is waiting for one of them. Can this system be deadlocked?
Show answer
No. A graph with no cycle cannot be deadlocked, whatever the number of instances. Some process in any chain of waiting is not blocked, so it finishes and releases its resources, and the waiting processes then proceed one after another.
Exercise
Run deadlocked_processes on a state where R1 has one instance, P1 holds R1, P2 requests R1, and P1 requests nothing. What does it return, and why?
Show answer
It returns an empty list. P1 has no requests, so it passes the check, finishes, and frees R1. P2's request can then be met, so P2 also finishes. No process remains, so there is no deadlock.