How does the Banker's algorithm check whether a state is safe?
HardConceptDeadlocks
Answer
The Banker's algorithm is a deadlock-avoidance method. Each process declares its maximum need in advance, and the OS grants a request only if the resulting state is safe: there is some order (a safe sequence) in which every process can get what it still needs, finish and release its resources. It uses Available (free instances), Max, Allocation and Need = Max − Allocation.
- Set Work = Available, then find an unfinished process with Need ≤ Work for every resource type.
- Assume it finishes and releases everything: Work = Work + its Allocation. Repeat.
- If all processes finish, the state is safe; otherwise it is unsafe, and deadlock may follow.
Example: resources A, B, C with Available = (1, 1, 1).
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | 1 0 1 | 4 2 2 | 3 2 1 |
| P1 | 2 1 0 | 3 2 1 | 1 1 1 |
| P2 | 1 1 1 | 2 2 3 | 1 1 2 |
Only P1 fits (1 1 1 ≤ 1 1 1), so Work = (3, 2, 1). P0 then fits, giving (4, 2, 2), and finally P2, giving (5, 3, 3). Safe sequence: P1 → P0 → P2. If P0 now asked for (1, 0, 0), Available would drop to (0, 1, 1) and no Need would fit, so the state would be unsafe and P0 must wait.