What is a resource-allocation graph, and how is it used to detect deadlock?
MediumConceptDeadlocks
Answer
A resource-allocation graph (RAG) is a directed graph with a node for each process (drawn as a circle) and for each resource type (a box with one dot per instance). A request edge P → R means P is waiting for R; an assignment edge R → P means an instance of R is held by P.
- No cycle means no deadlock.
- If every resource has a single instance, a cycle means deadlock. The OS can simplify the graph into a wait-for graph (P1 → P2 if P1 waits for P2) and find cycles with depth-first search.
- With multiple instances, a cycle means deadlock is possible but not certain, so a Banker's-style detection algorithm is used instead.
- Recovery: abort a victim process in the cycle, chosen by cost such as priority or work done, or preempt its resources and roll it back.
Example: P1 holds R1 and requests R2, while P2 holds R2 and requests R1:
P1 → R2 → P2 → R1 → P1
Each resource has one instance, so this cycle is a deadlock; aborting P2 frees R2 and lets P1 finish. Now suppose R2 has two instances, held by P2 and P3, and P3 is not waiting for anything. The same cycle exists, but when P3 finishes it releases its instance to P1, so there is no deadlock.