Banker's Algorithm Step by Step: Need Matrix, Safe Sequence and Requests
Work through the Banker's algorithm: build the need matrix, find a safe sequence, decide whether to grant a request and detect deadlock, with an example.

Banker's Algorithm Step by Step: Need Matrix, Safe Sequence and Requests
A deadlock is a traffic jam inside a computer: process A holds a resource that B needs, while B holds one that A needs, and neither can continue. Edsger Dijkstra's Banker's algorithm avoids this by behaving like a cautious bank. It only lends resources if, afterwards, it could still satisfy every customer's maximum demand in some order. This guide works through the algorithm with the standard textbook example, which you can load in the free Banker's algorithm calculator.
The data
Five processes P0 to P4 share three resource types A, B and C.
| Allocation | Max | |
|---|---|---|
| P0 | 0 1 0 | 7 5 3 |
| P1 | 2 0 0 | 3 2 2 |
| P2 | 3 0 2 | 9 0 2 |
| P3 | 2 1 1 | 2 2 2 |
| P4 | 0 0 2 | 4 3 3 |
Available: 3 3 2.
Step 1: the Need matrix
Need = Max − Allocation, row by row:
- P0: 7 4 3
- P1: 1 2 2
- P2: 6 0 0
- P3: 0 1 1
- P4: 4 3 1
Need is what each process might still ask for before it finishes.
Step 2: the safety algorithm
Set Work = Available = (3, 3, 2). Then repeatedly look for an unfinished process whose Need is less than or equal to Work in every resource. When you find one, assume it finishes and returns its allocation: add its Allocation to Work.
First pass, in order:
- P0 needs (7, 4, 3) > Work: wait.
- P1 needs (1, 2, 2) ≤ (3, 3, 2): finish. Work = (3, 3, 2) + (2, 0, 0) = (5, 3, 2).
- P2 needs (6, 0, 0) > (5, 3, 2): wait.
- P3 needs (0, 1, 1): finish. Work = (7, 4, 3).
- P4 needs (4, 3, 1): finish. Work = (7, 4, 5).
Second pass:
- P0 needs (7, 4, 3) ≤ (7, 4, 5): finish. Work = (7, 5, 5).
- P2 needs (6, 0, 0): finish. Work = (10, 5, 7).
Everyone can finish, so the state is safe with the sequence P1, P3, P4, P0, P2. Other safe sequences exist, such as P1, P3, P4, P2, P0; any one proves safety.
Step 3: handling a request
Suppose P1 requests (1, 0, 2).
- Is it within P1's Need (1, 2, 2)? Yes.
- Is it within Available (3, 3, 2)? Yes.
- Pretend to grant it: Available becomes (2, 3, 0), P1's Allocation (3, 0, 2), P1's Need (0, 2, 0).
- Run the safety algorithm on the new state. It is still safe, so grant the request.
Now suppose P4 asks for (3, 3, 0). It exceeds the new Available (2, 3, 0), so P4 must wait. And if P0 asks for (0, 2, 0), the pretend state turns out to be unsafe, so P0 must also wait even though the resources are physically free. That last case is the heart of the algorithm: it refuses requests that could lead to deadlock later.
The calculator lets you apply a granted request with one button and continue, just like a multi-part exam question.
Safe, unsafe and deadlocked
- Safe: there is an order in which everyone can finish.
- Unsafe: there is no such guaranteed order; deadlock may happen if processes ask for their maximums.
- Deadlocked: processes are already stuck.
Unsafe is a warning, not a deadlock.
Deadlock detection
Avoidance needs to know each process's maximum in advance, which real systems rarely do. Detection instead looks at the current requests: assume any process whose request can be met will finish and release its resources, and repeat. Processes left over are deadlocked, and the system must recover, for example by terminating one. The detection tab of the calculator runs this algorithm on a Request matrix.
Exam tips
- Show the Need matrix explicitly.
- Write Work after every process that finishes.
- For a request, always state the two checks before the pretend allocation.
Limits
The Banker's algorithm assumes a fixed set of processes and resources and known maximum needs, so it is used mainly for teaching rather than in general-purpose operating systems. The calculator handles up to eight processes and five resource types.


