Sub topic 9
Banker's algorithm: granting or delaying a request
This chapter assumes you know the safety algorithm from the previous chapter. That means the Available, Allocation, Max and Need structures, and the idea that a state is safe if some order lets every process finish. Here we use that test as a gatekeeper. Each time a process asks for resources, the system decides whether to grant the request now or make the process wait.
The idea in one paragraph
The Banker's algorithm is a deadlock avoidance method for resource types with multiple instances. Every process declares its maximum claim in advance. When a process makes a request, the system does not ask whether the resources are free. It asks whether the system would still be safe after granting the request. If so, it grants the request. If not, the process waits, even though the resources are physically available.
Data structures
For n processes and m resource types, the algorithm keeps four structures:
| Name | Shape | Meaning |
|---|---|---|
| Available | vector of length m | Free instances of each resource type |
| Max | n x m matrix | Maximum number of instances each process may ever need |
| Allocation | n x m matrix | Instances each process holds now |
| Need | n x m matrix | Need = Max - Allocation, the most each process may still request |
A comparison of vectors is element-wise. X <= Y means X[j] <= Y[j] for every j. A single larger entry makes the comparison false.
The resource-request algorithm
Let Request_i be the vector that process Pi asks for. The algorithm has three steps.
- If
Request_i <= Need_iis false, raise an error. The process has exceeded its declared maximum claim. - If
Request_i <= Availableis false, Pi must wait. The resources are not free yet. - Pretend to grant the request:
Available = Available - Request_iAllocation_i = Allocation_i + Request_iNeed_i = Need_i - Request_i
Run the safety algorithm on this pretend state. If it is safe, the grant becomes real. If it is unsafe, restore the old state and make Pi wait.
Step 1 is a contract check on the process. Steps 2 and 3 are the two reasons a request can be delayed, and they are different. In step 2 the system lacks the resources. In step 3 the system has them but refuses to give them out, because the grant could lead to deadlock.
Worked example
Take 5 processes and 3 resource types A, B, C with totals (10, 5, 7). The current state is:
| Process | Allocation (A B C) | Max (A B C) | Need (A B C) |
|---|---|---|---|
| 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 |
Allocation sums to (7, 2, 5), so Available = (10, 5, 7) - (7, 2, 5) = (3, 3, 2).
Request 1: P1 asks for (1, 0, 2)
- Step 1: (1, 0, 2) <= Need of P1 = (1, 2, 2). Passes.
- Step 2: (1, 0, 2) <= Available = (3, 3, 2). Passes.
- Step 3: the pretend state has Available = (2, 3, 0), Allocation of P1 = (3, 0, 2) and Need of P1 = (0, 2, 0).
Run the safety algorithm with Work = (2, 3, 0):
- P0 needs (7, 4, 3). It does not fit.
- P1 needs (0, 2, 0). It fits. Work becomes (2, 3, 0) + (3, 0, 2) = (5, 3, 2).
- P2 needs (6, 0, 0). It does not fit, because 6 > 5.
- P3 needs (0, 1, 1). It fits. Work becomes (7, 4, 3).
- P4 needs (4, 3, 1). It fits. Work becomes (7, 4, 5).
- On the second pass, P0 fits. Work becomes (7, 5, 5).
- P2 fits. Work becomes (10, 5, 7).
The safe sequence is P1, P3, P4, P0, P2, so the request is granted.
Request 2: P0 asks for (0, 2, 0), after Request 1 was granted
The state now has Available = (2, 3, 0).
- Step 1: (0, 2, 0) <= Need of P0 = (7, 4, 3). Passes.
- Step 2: (0, 2, 0) <= (2, 3, 0). Passes.
- Step 3: pretend Available = (2, 1, 0), Allocation of P0 = (0, 3, 0), Need of P0 = (7, 2, 3).
Now test each process against Work = (2, 1, 0):
- P0 needs (7, 2, 3). No.
- P1 needs (0, 2, 0). No, since 2 > 1 in B.
- P2 needs (6, 0, 0). No.
- P3 needs (0, 1, 1). No, since 1 > 0 in C.
- P4 needs (4, 3, 1). No.
No process can finish, so the state is unsafe. The request is denied and P0 waits, although the resources are free.
Request 3: P4 asks for (3, 3, 0), in the original state
Step 1 passes, since (3, 3, 0) <= (4, 3, 1). Step 2 also passes, since (3, 3, 0) <= (3, 3, 2). Step 3 gives Available = (0, 0, 2). P3 needs (0, 1, 1), which fails on B. P1 needs (1, 2, 2), which fails. P2 needs (6, 0, 0), which fails. P0 and P4 also fail. The state is unsafe, so the request is denied.
Running it
This script combines the two algorithms and replays the requests above. The is_safe function is the safety algorithm. The can_grant function is the three steps of the resource-request algorithm.
def is_safe(available, allocation, need):
n = len(allocation)
m = len(available)
work = list(available)
finish = [False] * n
order = []
progress = True
while progress:
progress = False
for i in range(n):
if not finish[i] and all(need[i][j] <= work[j] for j in range(m)):
for j in range(m):
work[j] += allocation[i][j]
finish[i] = True
order.append(i)
progress = True
return all(finish), order
def can_grant(pid, request, available, allocation, need):
m = len(available)
if any(request[j] > need[pid][j] for j in range(m)):
raise ValueError("process exceeded its declared maximum claim")
if any(request[j] > available[j] for j in range(m)):
return False # resources not free, the process must wait
new_available = [available[j] - request[j] for j in range(m)]
new_allocation = [row[:] for row in allocation]
new_need = [row[:] for row in need]
for j in range(m):
new_allocation[pid][j] += request[j]
new_need[pid][j] -= request[j]
safe, _ = is_safe(new_available, new_allocation, new_need)
return safe
allocation = [[0, 1, 0], [2, 0, 0], [3, 0, 2], [2, 1, 1], [0, 0, 2]]
max_need = [[7, 5, 3], [3, 2, 2], [9, 0, 2], [2, 2, 2], [4, 3, 3]]
available = [3, 3, 2]
need = [[max_need[i][j] - allocation[i][j] for j in range(3)] for i in range(5)]
print(can_grant(1, [1, 0, 2], available, allocation, need)) # True
print(can_grant(4, [3, 3, 0], available, allocation, need)) # False
# Commit P1's request, then test P0's request in the new state
available = [2, 3, 0]
allocation[1] = [3, 0, 2]
need[1] = [0, 2, 0]
print(can_grant(0, [0, 2, 0], available, allocation, need)) # FalseThe function only answers yes or no. A real system would also apply the changes when the answer is yes, as the last block does by hand for P1.
Edge cases and common errors
- Unsafe does not mean deadlocked. An unsafe state is one where deadlock is possible, depending on how the processes behave. Banker's refuses to enter unsafe states, so it is conservative. It may delay a request that would never have caused a deadlock.
- Free resources do not guarantee a grant. Request 2 above passed steps 1 and 2 and still failed step 3. Mixing up "not available" with "would be unsafe" is the most common conceptual mistake.
- Process Pi waiting is not always permanent. A delayed request is retried after another process releases resources. Starvation is still possible if a process is always bypassed, so a real implementation needs fairness, such as aging.
- Check Need before Available. The order of steps 1 and 2 matters only for error handling. A request above the declared maximum is a bug in the process and should never silently wait.
- A request is one vector. A process can ask for several resource types at once. The request is granted entirely or not at all. Do not grant part of it.
- Update Need together with Allocation. After a grant, Allocation goes up and Need goes down by the same amount. Forgetting Need is a common slip in hand calculations.
Assumptions and cost
The algorithm assumes the following:
- Each process declares its maximum claim before it runs.
- The number of processes is fixed.
- The number of resource instances is fixed.
- Every process releases its resources in finite time after it gets its maximum.
Real systems rarely satisfy these, which is why general-purpose operating systems usually do not use the Banker's algorithm.
The safety algorithm scans up to n processes, up to n times, comparing m entries each time. Its cost is O(m n^2). Each request runs it once, so the cost is paid per request.
Exercise
A system has 3 processes and one resource type with 10 instances. Allocation is P0 = 3, P1 = 2, P2 = 2. Max is P0 = 9, P1 = 4, P2 = 7. So Available = 3. Would the Banker's algorithm grant P2 a request for 1 instance?
Show answer
Need before the request is P0 = 6, P1 = 2, P2 = 5. The request of 1 is within Need (5) and Available (3). After a tentative grant, Available = 2, P2 holds 3 and its Need is 4. With Work = 2, P1 (Need 2) can finish, giving Work = 4. Then P2 (Need 4) can finish, giving Work = 7. Then P0 (Need 6) can finish. The safe sequence is P1, P2, P0, so the request is granted.
Exercise
In the worked example's original state, P0 requests (0, 2, 0). Is it granted?
Show answer
Step 1 passes since (0, 2, 0) <= (7, 4, 3). Step 2 passes since (0, 2, 0) <= (3, 3, 2). Pretend Available = (3, 1, 2), P0 Allocation = (0, 3, 0) and P0 Need = (7, 2, 3). With Work = (3, 1, 2), P1 (1, 2, 2) fails on B, but P3 (0, 1, 1) fits, giving Work (5, 2, 3). Then P1 fits, giving (7, 2, 3). P0 (7, 2, 3) fits, giving (7, 5, 3). P2 and P4 then fit. The state is safe, so the request is granted. The same request was denied in Request 2 only because P1's earlier grant had left Available smaller.
Exercise
Why can the Banker's algorithm delay a request even when the requested instances are free?
Show answer
The algorithm grants a request only if the resulting state is safe. If granting would leave no process able to finish with the remaining free resources, the state would be unsafe and deadlock would become possible, so the request is delayed.