OS Notes
Complete explanation of the Banker\
Introduction
Imagine you are a banker with limited cash reserves. Multiple customers have approved credit lines (maximum needs). When a customer requests a loan, you must decide: "If I give this loan, will I still have enough cash to satisfy all other customers if they simultaneously request their full credit limits?" If the answer is yes, you approve the loan. If not, you ask the customer to wait. This is exactly how the Banker's Algorithm works.
The Banker's Algorithm, developed by Edsger Dijkstra in 1965, is the most famous deadlock avoidance algorithm. It handles systems with multiple resource types, each having multiple instances. When a process requests resources, the algorithm determines whether granting the request would leave the system in a safe state.
Data Structures
The algorithm maintains four key data structures. Let n = number of processes, m = number of resource types.
| Available[m] | Vector of available instances of each resource type |
| Maximum[n][m] | Maximum demand of each process for each resource |
| Allocation[n][m] | Resources currently allocated to each process |
| Need[n][m] | Remaining resource need of each process |
| Relationship | Need[i][j] = Maximum[i][j] - Allocation[i][j] |
The Safety Algorithm
This algorithm checks whether the current state is safe (a safe sequence exists):
The Resource Request Algorithm
When process Pi makes a request Request[i]:
Complete Worked Example
System with 5 processes (P0-P4) and 3 resource types (A=10, B=5, C=7 total instances):
Initial State
| Process | Allocation (A B C) | Maximum (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 |
Available = Total - Sum(Allocation) = (10,5,7) - (7,2,5) = (3, 3, 2)
Finding the Safe Sequence
Work = (3, 3, 2), Finish = [F, F, F, F, F]
Iteration 1: Check each process:
- P0: Need(7,4,3) ≤ Work(3,3,2)? 7>3, NO
- P1: Need(1,2,2) ≤ Work(3,3,2)? Yes! P1 can finish.
- Work = (3,3,2) + (2,0,0) = (5, 3, 2), Finish[P1] = T
Iteration 2:
- P0: Need(7,4,3) ≤ Work(5,3,2)? 7>5, NO
- P2: Need(6,0,0) ≤ Work(5,3,2)? 6>5, NO
- P3: Need(0,1,1) ≤ Work(5,3,2)? Yes! P3 can finish.
- Work = (5,3,2) + (2,1,1) = (7, 4, 3), Finish[P3] = T
Iteration 3:
- P0: Need(7,4,3) ≤ Work(7,4,3)? Yes! P0 can finish.
- Work = (7,4,3) + (0,1,0) = (7, 5, 3), Finish[P0] = T
Iteration 4:
- P2: Need(6,0,0) ≤ Work(7,5,3)? Yes! P2 can finish.
- Work = (7,5,3) + (3,0,2) = (10, 5, 5), Finish[P2] = T
Iteration 5:
- P4: Need(4,3,1) ≤ Work(10,5,5)? Yes! P4 can finish.
- Work = (10,5,5) + (0,0,2) = (10, 5, 7), Finish[P4] = T
All processes finished! Safe sequence: P1 → P3 → P0 → P2 → P4
Now: P1 Requests (1, 0, 2)
Check: Request(1,0,2) ≤ NeedP1? Yes (1≤1, 0≤2, 2≤2) Check: Request(1,0,2) ≤ Available(3,3,2)? Yes (1≤3, 0≤3, 2≤2)
Tentatively allocate:
- Available = (3,3,2) - (1,0,2) = (2, 3, 0)
- Allocation[P1] = (2,0,0) + (1,0,2) = (3, 0, 2)
- Need[P1] = (1,2,2) - (1,0,2) = (0, 2, 0)
Run safety algorithm on new state... (I will spare the details but the sequence P1→P3→P0→P2→P4 still works)
Result: SAFE → Request GRANTED
Time Complexity
- Safety Algorithm: O(n² × m) — for each of n iterations, check n processes across m resources
- Resource Request: O(n² × m) — dominated by the safety check
For a system with 100 processes and 10 resource types, this is 100,000 operations per request — fast enough for most systems.
Limitations of the Banker's Algorithm
- Maximum needs must be declared in advance — Many processes do not know this
- Fixed number of processes — Processes cannot dynamically enter/leave
- Fixed number of resources — Devices cannot be added/removed
- Processes must eventually terminate — Infinite loops break the assumption
- Conservative — May deny safe requests, reducing utilization
Key Takeaways
- The Banker's Algorithm decides whether granting a resource request keeps the system safe
- It maintains Available, Maximum, Allocation, and Need matrices
- The Safety Algorithm finds if a safe sequence exists by simulating process completions
- A request is granted only if the resulting state remains safe; otherwise, the process waits
- Time complexity is O(n²×m), practical for moderate system sizes
- Main limitation: requires advance declaration of maximum resource needs
- Despite limitations, it is the definitive example of deadlock avoidance in OS education
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Banker\.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Operating Systems topic.
Search Terms
operating-systems, operating systems, operating, systems, deadlocks, bankers, algorithm, banker\
Related Operating Systems Topics