Deadlock avoidance is a dynamic approach that requires the operating system to know in advance the maximum resource requirements of each process and to make careful allocation decisions that keep the system in a safe state—i.e., a state from which all processes can complete without ever entering a deadlock. Unlike prevention (which statically forbids one of the four necessary conditions) or detection (which lets deadlocks occur and then recovers), avoidance permits all four conditions but refuses resource requests that could lead to an unsafe state .


Principle of the Banker's Algorithm

Dijkstra’s Banker’s algorithm models processes as bank customers and resources as cash in the bank.


Data Structures in Banker's Algorithm

Let there be m resource types and n processes. The Banker’s algorithm maintains:

  1. Total\_Res\[1..m]: Total instances of each resource.
  2. Max\[n]\[m]: Maximum demand of each process for each resource.
  3. Alloc\[n]\[m]: Currently allocated instances to each process.
  4. Avail\[1..m]: Available instances = Total\_Res – ∑(Alloc rows).
  5. Need\[n]\[m]: Remaining need = Max – Alloc .

Safety Algorithm of Banker's Algorithm

To check if the system is in a safe state:

  1. Work ← Avail; Finish\[i] ← false for all i.
  2. Find a process Pᵢ such that Finish\[i] = false and Need\[i] ≤ Work.
  3. If found, Work ← Work + Alloc\[i]; Finish\[i] ← true; record Pᵢ in the safe sequence, repeat step 2.