Skip to content
We areTOP 1%

How does the Banker's algorithm check whether a state is safe?

HardConceptDeadlocks
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

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).

ProcessAllocationMaxNeed
P01 0 14 2 23 2 1
P12 1 03 2 11 1 1
P21 1 12 2 31 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.

More in Deadlocks

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account