Sub topic 10
Avoidance with claim edges for single-instance resources
This chapter assumes you know the resource-allocation graph from the earlier chapter (process nodes, resource nodes, request edges, assignment edges). It also assumes you know what a safe state is. The Banker's algorithm handles resources with many instances. This chapter covers the simpler case where every resource type has exactly one instance. There, the Banker's matrices can be replaced by a graph and a cycle check.
Why a graph is enough for single instances
In the earlier chapters, a cycle in a resource-allocation graph meant deadlock only when each resource type had one instance. The same restriction makes avoidance easy. With one instance per type, an unsafe state corresponds to a cycle in a graph that also contains the processes' future needs. So the OS only has to ask: "If I grant this request, will the graph contain a cycle?"
To answer that, the graph needs a way to represent future requests. That is the job of the claim edge.
The three edge types
A claim edge Pi ⇢ Rj means that Pi may request Rj at some time in the future. It points in the same direction as a request edge and is drawn dashed. Each edge type changes into the next as the process progresses.
| Edge | Direction | Meaning | Becomes |
|---|---|---|---|
| Claim | Pi → Rj (dashed) | Pi may request Rj later | Request edge, when Pi requests Rj |
| Request | Pi → Rj | Pi is waiting for Rj | Assignment edge, when Rj is granted |
| Assignment | Rj → Pi | Pi holds Rj | Claim edge, when Pi releases Rj |
The last conversion is easy to miss. After a release, the edge goes back to a claim edge, not to nothing. The process may still ask for that resource again, so the future need remains in the graph.
Claim edges must be known in advance. A process declares all its claims before it starts executing. The textbook rule allows a new claim edge to appear later only if all of that process's other edges are also claim edges. In practice, treat the claims as fixed up front.
The grant rule
When Pi requests Rj, the OS applies two tests in order:
- If Rj is currently assigned to another process, Pi waits. This is the normal mutual-exclusion rule.
- If Rj is free, the OS tentatively converts the request edge Pi → Rj into an assignment edge Rj → Pi. If the resulting graph has no cycle, the request is granted. If it has a cycle, the state would be unsafe, so Pi waits even though Rj is free.
Claim edges count when looking for a cycle. This is the whole mechanism. A cycle that passes through a claim edge does not yet mean deadlock, because that claim has not been requested. It does mean that some order of future requests can deadlock, so the state is unsafe.
The result matches the safe-state definition:
- No cycle (counting claim edges) means the state is safe.
- A cycle means the state is unsafe.
Cycle detection on a graph with V nodes and E edges takes O(V + E) time. The textbook states it as O(n²) for n processes, since edges can be dense.
A worked example
Take two processes and two resources, each with one instance.
- P1 holds R1 and has a claim edge to R2.
- P2 has claim edges to both R1 and R2, and holds nothing.
- P2 now requests R2, which is free.
If the OS grants it, the graph gains the assignment edge R2 → P2. Then P2 holds R2 and still has a claim on R1. P1 holds R1 and still has a claim on R2. That is a cycle, even though nobody is blocked yet.
Follow the cycle: P1 ⇢ R2 → P2 ⇢ R1 → P1. The OS therefore delays P2, and P2 waits even though R2 is free.
Nothing is deadlocked after the delay. P1 can still request R2, get it, finish, and release both resources. Then P2 can run. Here is what happens to a deadlock that might otherwise occur:
- If the OS had granted R2 to P2, then P1 could request R2 and block, and P2 could request R1 and block. That is a deadlock.
- Because the OS refused, the deadlock cannot happen. The price is that P2 waited while R2 sat idle.
Implementing the check
The code below follows the grant rule directly. The graph is a dictionary from each node to the set of nodes it points to. Claim and request edges are both stored as process → resource, since they point the same way and only differ in meaning. Assignment edges are stored as resource → process.
def has_cycle(graph):
WHITE, GRAY, BLACK = 0, 1, 2
color = {}
def visit(u):
color[u] = GRAY
for v in graph.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(graph))
def can_grant(graph, proc, res):
if res not in graph.get(proc, set()):
raise ValueError("no claim or request edge from process to resource")
if graph.get(res):
return False # resource already assigned, the process must wait
trial = {node: set(targets) for node, targets in graph.items()}
trial[proc].discard(res) # claim/request edge disappears
trial.setdefault(res, set()).add(proc) # assignment edge appears
return not has_cycle(trial)
graph = {
"R1": {"P1"}, # R1 assigned to P1
"P1": {"R2"}, # P1 claims R2
"P2": {"R1", "R2"}, # P2 claims R1 and R2
}
print(can_grant(graph, "P2", "R2")) # False: granting creates a cycle
print(can_grant(graph, "P1", "R2")) # True: no cycle, P1 can proceedTwo details in the code matter:
can_grantworks on a copy (trial). If the grant is refused, the real graph is unchanged. The OS only commits the edge change after the check passes.- The second call shows the safe case. Granting R2 to P1 produces edges R1 → P1, R2 → P1 and P2 ⇢ R1, P2 ⇢ R2. There is no path leading back from P1, so no cycle exists.
Where this method stops working
Claim-edge avoidance is valid only when each resource type has a single instance. With multiple instances, a cycle is necessary but not sufficient for deadlock. Likewise, a cycle through claim edges no longer proves that the state is unsafe, because a spare instance might let a process finish and break the cycle. For those cases you need the Banker's algorithm with its Available, Allocation, Max, and Need matrices.
The method has costs even in the single-instance case:
- Every process must declare its maximum future needs ahead of time, as in the Banker's algorithm.
- The set of processes must be fixed, because a new process adds new claim edges and could create a cycle unnoticed.
- It is conservative. It may delay a request that would never have deadlocked, so resources can sit idle.
- The cycle check runs on every request that finds its resource free.
Exam patterns and common errors
For GATE-style questions, use this routine:
- Draw each process's claim edges as dashed arrows, along with the current assignment edges.
- For the requested edge, convert it to an assignment edge in your drawing.
- Look for a cycle, following dashed edges as if they were solid.
- If a cycle exists, the answer is "do not grant". Otherwise grant.
Common mistakes:
- Treating a cycle with claim edges as an actual deadlock. A deadlock needs real request edges that are all blocked. A claim-edge cycle only shows an unsafe state.
- Forgetting that the check applies even when the resource is free. A free resource can still be refused.
- Dropping the claim edge after a release. It reverts to a claim edge, not to no edge.
- Using this method for a resource with more than one instance. If a question mentions several instances, use the safety algorithm.
Exercise
Resources R1 and R2 have one instance each. P1 holds R1 and has a claim edge to R2. P2 has only a claim edge to R2, and no other edges. P2 requests R2, which is free. Should the OS grant it?
Show answer
Yes. After converting P2's request into an assignment edge, the graph has R1 → P1, P1 ⇢ R2 and R2 → P2. P2 has no outgoing claim edge back to R1, so no path returns to P1 and there is no cycle. The state is safe. P2 can finish and release R2, and then P1 can take R2 later.
Exercise
Why can the cycle-in-graph test not be used to decide safety when R1 has two instances?
Show answer
With two instances, a cycle through R1 does not imply that every process in it is stuck. A process outside the cycle, or one holding the second instance, may finish and release an instance, which lets the cycle resolve. A cycle is then necessary but not sufficient for unsafety, so you must run the safety algorithm on the Allocation, Max, and Need matrices instead.