Sub topic 12
Detection When Resources Have Multiple Instances
This chapter assumes you know the safety algorithm from earlier in the tutorial, including the Available, Allocation and Need matrices. You should also be comfortable comparing vectors element by element and reading a wait-for graph.
Why the wait-for graph is not enough
A wait-for graph detects deadlock by finding a cycle. That works only when every resource type has exactly one instance. With several identical instances (for example three printers or five tape drives), a cycle in the resource-allocation graph is necessary for deadlock but not sufficient. Another process outside the cycle may hold a free instance and release it later, which breaks the cycle.
For multiple instances, the system uses a matrix-based detection algorithm. It is structurally close to the safety algorithm, but it answers a different question.
Data structures
Let there be n processes and m resource types.
Availableis a vector of lengthm. It holds the number of free instances of each type.Allocationis ann × mmatrix. Entry[i][j]is how many instances of typejprocessicurrently holds.Requestis ann × mmatrix. Entry[i][j]is how many more instances of typejprocessiis waiting for right now.
The matrix is Request, not Need. Need = Max − Allocation describes what a process might ask for in the future. Request describes what it is blocked on at this moment. Detection looks at the present, not at declared maximums.
The algorithm
- Set
Work = Available. For each processi, setFinish[i] = trueifAllocation[i]is all zeros, otherwisefalse. - Find a process
iwithFinish[i] == falseandRequest[i] <= Work. If none exists, go to step 4. - Set
Work = Work + Allocation[i]andFinish[i] = true. Return to step 2. - Every process with
Finish[i] == falseis deadlocked. If all aretrue, there is no deadlock.
Why the algorithm is optimistic
When a process's request fits in Work, the algorithm assumes the process will finish and give back everything it holds. It does not check whether the process might later ask for more. The algorithm makes no assumption about the future. If the assumption is wrong and the process asks again later, the next run of detection will find the deadlock.
This is the main conceptual difference from the safety algorithm, which is pessimistic. The safety algorithm assumes every process will ask for its full Need.
Why zero-allocation processes start as finished
A process that holds nothing cannot be blocking anyone. Even if it is waiting for a resource, it contributes nothing to Work and cannot be part of a circular wait. Marking it finished at the start keeps it out of the deadlocked set. Without this rule, such a process would be reported as deadlocked whenever its request could not be met. The process is only waiting, so it is not deadlocked.
Worked example
There are five processes and three resource types A, B and C with 7, 2 and 6 instances in total.
| Process | Allocation (A B C) | Request (A B C) |
|---|---|---|
| P0 | 0 1 0 | 0 0 0 |
| P1 | 2 0 0 | 2 0 2 |
| P2 | 3 0 3 | 0 0 0 |
| P3 | 2 1 1 | 1 0 0 |
| P4 | 0 0 2 | 0 0 2 |
The allocation columns sum to 7, 2 and 6, so Available = (0, 0, 0).
Work = (0,0,0). P0 hasRequest = (0,0,0), which fits.Work = (0,1,0).- P1 needs
(2,0,2), which does not fit. P2 hasRequest = (0,0,0), which fits.Work = (3,1,3). - P3 needs
(1,0,0), which fits.Work = (5,2,4). - P1 needs
(2,0,2), which fits.Work = (7,2,4). - P4 needs
(0,0,2), which fits.Work = (7,2,6).
All processes finish, so there is no deadlock. One safe completion order is P0, P2, P3, P1, P4.
Now suppose P2 makes one extra request for a single instance of C, so Request[P2] = (0,0,1). P0 still finishes and gives Work = (0,1,0). After that:
- P1 needs
(2,0,2). It does not fit. - P2 needs
(0,0,1). C is 0 inWork, so it does not fit. - P3 needs
(1,0,0). It does not fit. - P4 needs
(0,0,2). It does not fit.
No further process can proceed, so P1, P2, P3 and P4 are deadlocked. P0 is not deadlocked, even though the system as a whole is stuck.
Implementation
def detect_deadlock(available, allocation, request):
n, m = len(allocation), len(available)
work = list(available)
# A process holding nothing cannot take part in a deadlock
finish = [all(a == 0 for a 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 [i for i in range(n) if not finish[i]]
allocation = [[0, 1, 0], [2, 0, 0], [3, 0, 3], [2, 1, 1], [0, 0, 2]]
request = [[0, 0, 0], [2, 0, 2], [0, 0, 0], [1, 0, 0], [0, 0, 2]]
available = [0, 0, 0]
print(detect_deadlock(available, allocation, request)) # []
request[2] = [0, 0, 1]
print(detect_deadlock(available, allocation, request)) # [1, 2, 3, 4]The function returns the list of deadlocked process indices. An empty list means no deadlock.
Safety algorithm versus detection algorithm
| Aspect | Safety algorithm | Detection algorithm |
|---|---|---|
| Matrix compared with `Work` | `Need = Max − Allocation` | `Request` (current outstanding request) |
| Assumption | Each process may still ask for its whole `Need` | Each process asks for nothing beyond its current `Request` |
| Starting `Finish` | All `false` | `true` where `Allocation` is zero |
| Question answered | Can the system always avoid deadlock? | Is there a deadlock right now? |
| Result | Safe or unsafe | The set of deadlocked processes |
| Used by | Avoidance (Banker's algorithm) | Detection and recovery |
The cost of both is O(m · n²) operations. Each pass over the processes is O(m · n), and up to n passes may be needed.
When to run detection
Running detection costs CPU time, so the system must choose how often to invoke it. There are two common policies.
- Run it on every request that cannot be granted immediately. This finds a deadlock the moment it forms and identifies the process whose request closed the circle. The cost is high.
- Run it at fixed intervals, or when CPU utilisation falls below a threshold. This is cheaper, but deadlocked processes sit idle for longer. With arbitrary invocation, many cycles may exist by the time detection runs, and the system cannot tell which process "caused" the deadlock.
Relationship to the single-instance case
If each resource type has one instance, the detection algorithm and the wait-for graph cycle test give the same answer. A request that cannot be met means the process waits for exactly the holder of that instance, and that holder is a single edge in the wait-for graph. Use the cycle test when it applies, because it is faster in practice. Use the matrix algorithm when any resource type has more than one instance.
GATE-style checks
- A request vector of all zeros means the process can always finish at its turn. This makes it the natural first step in tracing.
- When tracing by hand, order does not matter for the final answer. Any process that can finish gives back resources, and giving back resources can only help other processes. The set of deadlocked processes is therefore the same for every valid trace.
- To decide whether a given process is deadlocked, finish everything possible and check whether it remains.
- To find the largest value of an unknown entry that keeps the system deadlock-free, set up the inequality
Request[i] <= Workat the step where the unknown first matters.
Exercise
Two resource types X and Y. Available = (0, 0). Allocation: P0 = (1, 0), P1 = (0, 1), P2 = (1, 1). Request: P0 = (0, 1), P1 = (1, 0), P2 = (1, 1). Is the system deadlocked, and which processes are involved? Then change P2's request to (0, 0) and decide again.
Show answer
With Work = (0,0), no request fits: P0 needs (0,1), P1 needs (1,0), P2 needs (1,1). All three are deadlocked. If P2's request is (0,0), P2 finishes first and Work becomes (1,1). P0 then fits and finishes, giving Work (2,1). P1 then fits and finishes. No process is deadlocked.
Exercise
Why does the detection algorithm mark a process with zero Allocation as finished before it starts, and what would go wrong if it did not?
Show answer
A process holding no resources cannot be part of a circular wait, because nobody is waiting on it. If it were left unfinished and its request could not be met, the algorithm would report it as deadlocked. It is only waiting, so the report would be a false positive.