Sub topic 9
Banker's Algorithm: Granting or Delaying a Request
Prerequisites: processes, resource instances, locks, directed graphs, and element-wise comparison of vectors.
The decision problem
The Banker's algorithm is a deadlock-avoidance algorithm. It decides whether a process's resource request can be granted without making the system unsafe.
It assumes that each process declares its maximum possible demand before execution. The system grants a request only if the resulting state still has some order in which every process can finish.
The main data structures are:
- Available: instances of each resource type that are currently free.
- Allocation: resources currently held by each process.
- Max: maximum resources each process may need.
- Need: resources still required to reach the maximum.
For every process and resource type:
Need[i][j] = Max[i][j] - Allocation[i][j]
A state is safe if at least one sequence lets every process obtain its remaining Need, finish, and release its Allocation. A safe state is not necessarily free of current waiting, but it has a guaranteed completion order.
The resource-request algorithm
When process Pi requests vector Requesti, the algorithm follows these checks:
- If
Requesti > Need[i], the process exceeded its declared maximum. Reject the request. - If
Requesti > Available, the resources are not currently free. Make the process wait. - Pretend to grant the request:
- subtract it from Available,
- add it to Allocation[i],
- subtract it from Need[i].
- Run the safety algorithm on the tentative state.
- Keep the grant only if the tentative state is safe. Otherwise, restore the old state and delay the request.
The comparison is element-wise. For example, [1, 0, 2] <= [3, 3, 2] is true.
Worked safety check
Consider the classic state below. The entries in each row correspond to resource types A, B, and C.
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | [0, 1, 0] | [7, 5, 3] | [7, 4, 3] |
| P1 | [2, 0, 0] | [3, 2, 2] | [1, 2, 2] |
| P2 | [3, 0, 2] | [9, 0, 2] | [6, 0, 0] |
| P3 | [2, 1, 1] | [2, 2, 2] | [0, 1, 1] |
| P4 | [0, 0, 2] | [4, 3, 3] | [4, 3, 1] |
Available = [3, 3, 2]
Start with Work = Available. P1 can finish because [1, 2, 2] <= [3, 3, 2]. After it finishes, return its allocation, giving Work = [5, 3, 2].
Next, P3 can finish, then P4, P0, and P2. Therefore, one safe sequence is:
P1, P3, P4, P0, P2
A request from P1 for [1, 0, 2] passes the first two checks. After tentative allocation, the safety test still finds a completion sequence, so the request can be granted.
def is_safe(available, allocation, need):
work = list(available)
finished = [False] * len(allocation)
order = []
changed = True
while changed:
changed = False
for i, row in enumerate(need):
if not finished[i] and all(row[j] <= work[j] for j in range(len(work))):
work = [work[j] + allocation[i][j] for j in range(len(work))]
finished[i] = True
order.append(i)
changed = True
return all(finished), order
available = [3, 3, 2]
allocation = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
maximum = [[7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3]]
need = [[maximum[i][j] - allocation[i][j] for j in range(3)] for i in range(5)]
print(is_safe(available, allocation, need))
# (True, [1, 3, 4, 0, 2])Exercise
If a tentative request leaves Available = [1, 1] and every unfinished process has Need greater than [1, 1] in at least one component, should the request be granted?
Show answer
No. The tentative state is unsafe, so the system must undo the tentative grant and delay the request.