Definition
Banker's Algorithm
The Banker’s algorithm is a deadlock avoidance algorithm that ensures the system always remains in a safe state by testing whether resource requests would leave the system capable of completing all processes.
The name derives from a bank that never lends more than it can satisfy if all customers simultaneously demanded their remaining credit lines — using repayments from completed customers to satisfy others.
Data Structures
For a system with processes and resource types:
| Symbol | Name | Definition |
|---|---|---|
| Resource vector | Total resources: | |
| Available vector | Free resources: | |
| Claim matrix | Maximum need: = max of process for resource | |
| Allocation matrix | Currently held: = amount of resource held by process | |
| Need matrix | Remaining requirement: |
Safety Algorithm
The algorithm determines whether a state is safe by simulating process completion.
Bool isSafe()
W = V // work vector
Finish[0..n-1] = {false, ..., false} // all unfinished
while (exists i such that !Finish[i] && N[i] <= W) {
W = W + A[i] // process i completes
Finish[i] = true
}
return (all i: Finish[i]) // true if all can finish
Greedy Completion
A state is safe if we can find an ordering of processes where each process can obtain its remaining needs from currently available resources plus those released by earlier processes in the sequence.
Resource Request Protocol
When process requests resources :
Bool request(int k, Vector Q)
if (Q > N[k]) return false // exceeds declared maximum
if (Q > V) return false // insufficient resources available
// Tentative allocation
V = V - Q
A[k] = A[k] + Q
N[k] = N[k] - Q
if (isSafe()) {
return true // grant request
} else {
// Rollback
V = V + Q
A[k] = A[k] - Q
N[k] = N[k] + Q
return false // block process
}
Examples
Safety Check
System: processes, resource types, , .
. Only satisfies .
- finishes: .
- satisfies : .
- satisfies : .
- satisfies : completes.
All processes complete in order . The state is safe.