Sub topic 10
Avoidance with Claim Edges for Single-Instance Resources
This chapter assumes you can read a resource-allocation graph, follow directed edges, and detect cycles. You should also know that a resource may have one or more instances. Here, every resource type has exactly one instance, and each process declares the resources it may request.
Why Claim Edges Matter
A claim edge is a dashed edge from a process to a resource. It means that the process may request the resource later. It does not mean that the process is waiting now.
A resource-allocation graph uses these edge meanings:
| Edge | Meaning |
|---|---|
| `P → R` | Process `P` is requesting resource `R` |
| `R → P` | Process `P` currently holds resource `R` |
| `P -.-> R` | Process `P` may request resource `R` later |
For example, if P1 holds R1 and may later request R2, the graph contains R1 → P1 and a dashed P1 -.-> R2.
A claim edge is useful because the operating system can evaluate a future request before granting it. This is deadlock avoidance: the system grants a request only when the resulting state remains safe.
The Claim-Edge Avoidance Algorithm
When process P requests resource R, follow these steps:
- Convert the claim edge from
PtoRinto a request edge. - If
Ris unavailable, makePwait. - If
Ris available, tentatively grant it. Replace the request edge with an assignment edge fromRtoP. - Check the resulting graph for a cycle.
- If there is no cycle, keep the assignment. If there is a cycle, undo the tentative assignment and delay the request.
For single-instance resources, a cycle made from current request and assignment edges represents deadlock. During avoidance, the system also rejects a tentative assignment that would create a cycle involving claim edges. Such a cycle represents an unsafe future, even if no process is deadlocked yet.
A claim-edge cycle is therefore not automatically a current deadlock. Dashed edges describe possible future requests, not present waits.
A Small Implementation
The following program treats each graph entry as a directed edge. It removes the claim edge, adds a tentative assignment, and checks for a cycle.
def has_cycle(graph):
WHITE, GRAY, BLACK = 0, 1, 2
color = {}
def visit(node):
color[node] = GRAY
for neighbor in graph.get(node, set()):
state = color.get(neighbor, WHITE)
if state == GRAY:
return True
if state == WHITE and visit(neighbor):
return True
color[node] = BLACK
return False
return any(
color.get(node, WHITE) == WHITE and visit(node)
for node in graph
)
def safe_to_grant(graph, process, resource):
trial = {node: set(edges) for node, edges in graph.items()}
if resource in trial.get(resource, set()):
return False # resource is already assigned
if resource not in trial.get(process, set()):
raise ValueError("no claim exists for this request")
trial[process].remove(resource) # remove the claim
trial.setdefault(resource, set()).add(process) # assign resource to process
return not has_cycle(trial)
graph = {
"P1": {"R2"}, # P1 claims R2
"R1": {"P1"}, # P1 currently holds R1
"R2": set(), # R2 is free
}
print(safe_to_grant(graph, "P1", "R2")) # TrueLimits of the Rule
This graph method relies on one instance per resource type. With multiple instances, a cycle does not by itself provide enough information to decide whether the system is deadlocked or safe. Use the matrix-based safety and detection algorithms for that case.
Exercise
P1 holds R1 and has a claim edge to R2. P2 holds R2 and has a claim edge to R1. Is the system currently deadlocked?
Show answer
No. The dashed edges are only possible future requests. However, the graph describes an unsafe state because if both processes request their claimed resource, each process will wait for the other.