What is the Banker's Algorithm?

Answer

The Banker's Algorithm (Dijkstra) is a deadlock avoidance algorithm that dynamically examines resource allocation to ensure the system never enters an unsafe state — a state from which deadlock is possible. It works like a banker who grants loans only if the bank can still satisfy all clients' maximum future needs. Data structures: Available[m] — available resources of each type; Max[n][m] — maximum demand of each process; Allocation[n][m] — currently allocated; Need[n][m] = Max - Allocation. Safety algorithm: Work = Available; Finish[i] = false for all i; while (some process not Finish): find i: Finish[i]==false AND Need[i] <= Work; if found: Work = Work + Allocation[i]; Finish[i] = true; if Finish[i] = true for all i: SAFE STATE. Resource request algorithm: when process Pi requests resources Request[i]: (1) If Request[i] ≤ Need[i], proceed; (2) If Request[i] ≤ Available, proceed; (3) Pretend to allocate: Available -= Request[i]; Allocation[i] += Request[i]; Need[i] -= Request[i]; (4) Run safety algorithm: if safe → grant request; if unsafe → deny and restore. Example: 5 processes, 3 resource types. Determine if a request can be safely granted. Limitations: must know maximum resource needs in advance (usually unknown); doesn't handle new processes arriving; overhead of running safety algorithm per request; doesn't work with resources requested in arbitrary order. Practice: most systems use detection+recovery rather than avoidance because of Banker's impracticality.