Sub topic 12
Detecting Deadlock with Multiple Resource Instances
Prerequisites and working model
Assume you know processes, threads, locks or semaphores, directed graphs, and element-wise comparison of vectors.
A resource type can have multiple identical instances. For example, a system might have four tape drives and three printers. A process can hold some instances while waiting for more.
Deadlock detection asks whether the current state contains a set of processes that can never finish. Unlike the Banker's algorithm, detection does not require each process to declare its maximum future claim. It uses the resources currently available and the resources each process is currently requesting.
The detector uses three vectors or matrices:
| Structure | Meaning |
|---|---|
| Available | Instances of each resource type not currently allocated |
| Allocation | Instances currently held by each process |
| Request | Additional instances each process currently needs |
Request[i][j] <= Work[j] means that all resources currently requested by process i can be supplied from the temporary vector Work.
Matrix-based detection algorithm
The algorithm simulates possible completions:
- Set
WorktoAvailable. - Set
Finish[i]to false for each process that holds resources. A process with no allocation can be marked true because it cannot keep resources from other processes. - Find an unfinished process whose
Requestfits inWork. - Pretend that process finishes. It releases its allocation, so add
Allocation[i]toWork. - Mark the process finished and repeat.
- If every process finishes, there is no deadlock. Any process still unfinished belongs to the deadlocked set.
The release step is essential. A process can provide resources to later processes only after it completes.
def detect_deadlock(available, allocation, request):
process_count = len(allocation)
resource_count = len(available)
work = available[:]
finish = [not any(row) for row in allocation]
sequence = []
changed = True
while changed:
changed = False
for i in range(process_count):
can_finish = (
not finish[i]
and all(request[i][j] <= work[j]
for j in range(resource_count))
)
if can_finish:
for j in range(resource_count):
work[j] += allocation[i][j]
finish[i] = True
sequence.append(i)
changed = True
deadlocked = [i for i in range(process_count) if not finish[i]]
return deadlocked, sequence
available = [0, 0]
allocation = [[1, 0], [0, 1], [1, 0]]
request = [[0, 1], [1, 0], [1, 1]]
print(detect_deadlock(available, allocation, request))
# ([0, 1, 2], [])Here, no process can satisfy its request. Therefore, all three processes are deadlocked.
Common errors
A cycle in a graph is not the direct test for multiple-instance detection. A cycle can exist while another instance of a resource lets one process finish. The matrix algorithm accounts for all available instances.
Do not add Request to Work when a process finishes. A process returns its current Allocation, not the resources it was still requesting.
Detection describes the present state. It does not guarantee that a future request will be safe. After detecting deadlock, the operating system must recover by terminating processes or preempting resources.
> callout note > For multiple instances, keep the distinction clear: Allocation is held now, Request is still wanted, and Available is free now.
Exercise
Available is [1, 0]. Process P0 has Allocation [0, 1] and Request [1, 0]. Process P1 has Allocation [1, 0] and Request [0, 1]. Does the detection algorithm find deadlock?
Show answer
Yes. P0 needs one unit of the first resource, while P1 needs one unit of the second. Neither request fits in Available, so both processes remain unfinished.