Sub topic 3
Avoidance with Claim Edges, Detection, Recovery, and GATE Patterns
This chapter assumes you know processes, locks, directed graphs and cycles, and element-wise comparison of vectors.
Avoidance with claim edges for single-instance resources
A resource-allocation graph (RAG) shows processes as circles and resources as squares. A claim edge is a dashed arrow from a process to a resource, meaning the process may request that resource later. A claim edge turns into a request edge when the process asks for the resource, and into an assignment edge when the grant happens.
The system grants a request only if converting the request edge into an assignment edge does not create a cycle in the graph, counting claim edges too. If it would create a cycle, the process waits, because the state would be unsafe.
Detection with a wait-for graph
When each resource type has one instance, you can remove the resource nodes. Draw an edge from Pi to Pj if Pi waits for a resource that Pj holds. A cycle in this wait-for graph means deadlock.
Detection when resources have multiple instances
With several instances per resource type, a cycle is not enough. The detection algorithm uses three inputs: Available, Allocation, and Request. It repeatedly finishes any process whose request fits in Work, then adds that process's allocation back to Work. Processes that never finish are deadlocked.
def detect_deadlock(available, allocation, request):
n = len(allocation)
m = len(available)
work = list(available)
# A process holding nothing cannot be part of a deadlock
finish = [all(x == 0 for x in allocation[i]) for i in range(n)]
progress = True
while progress:
progress = False
for i in range(n):
if not finish[i] and all(request[i][j] <= work[j] for j in range(m)):
for j in range(m):
work[j] += allocation[i][j]
finish[i] = True
progress = True
return [f"P{i}" for i in range(n) if not finish[i]]
available = [0, 0]
allocation = [[1, 0], [0, 1]]
request = [[0, 1], [1, 0]]
print(detect_deadlock(available, allocation, request)) # ['P0', 'P1']Recovery: terminating processes and preempting resources
After detection, the system can break the deadlock in two ways:
- Process termination: Abort all deadlocked processes, or abort them one at a time until no deadlock remains. Choose victims by cost, such as work done so far.
- Resource preemption: Take a resource from a process and give it to another. The preempted process must roll back to a safe earlier state.
Preemption risks starvation if the same process is always chosen as the victim.
GATE problem patterns and shortcuts
GATE often asks for the minimum number of resource instances that guarantees no deadlock. If n processes each need at most k instances of one resource type, the answer is:
n(k − 1) + 1
For example, 3 processes each needing 2 instances require 3(2 − 1) + 1 = 4 instances. With 4 instances, at least one process can always finish and release its resources.
Exercise
Exercise
A system has 5 processes, each needing at most 3 instances of one resource type. What is the minimum number of instances that guarantees no deadlock?
Show answer
n(k − 1) + 1 = 5(3 − 1) + 1 = 11 instances.