Banker's Algorithm: Need Matrix, Safe Sequence and Request Checks, Worked
The banker's algorithm is Dijkstra's deadlock-avoidance method: before granting any request, the OS checks whether the system would still be in a safe state, meaning some order exists in which every process can get its maximum need and finish. Exam questions give you Allocation, Max and Available tables and ask for the Need matrix, a safe sequence, or whether a request should be granted. Operating Systems carries 5% of the NSCT according to HEC's Areas of Competencies, and it had the lowest accuracy of any area in 2026, at 33.8%, per the HEC NSCT report and APP.
The theory (Coffman conditions, prevention versus avoidance versus detection) lives in Deadlocks. This page is the arithmetic.
The four data structures
For n processes and m resource types (Silberschatz's notation):
| Structure | Size | Meaning |
|---|---|---|
| Available | 1 x m | Free instances of each resource type now |
| Max | n x m | Most of each type a process may ever hold (declared in advance) |
| Allocation | n x m | What each process holds now |
| Need | n x m | Need = Max - Allocation: what it may still request |
Total instances = Available + the column sums of Allocation. Questions often give the total and expect you to derive Available.
The method, step by step
Safety algorithm
- Compute Need = Max - Allocation, row by row.
- Set Work = Available and mark every process unfinished.
- Find an unfinished process whose Need is less than or equal to Work in every column.
- Pretend it runs to completion: Work = Work + its Allocation, and mark it finished. Write it down as the next process in the sequence.
- Repeat steps 3-4. If every process finishes, the state is safe and your list is a safe sequence. If you get stuck, the state is unsafe.
Resource-request algorithm (process Pi asks for vector Request):
- If Request > Need[i] in any column: error, the process exceeded its claim.
- If Request > Available in any column: Pi waits, the resources are not free.
- Otherwise pretend to grant: Available -= Request, Allocation[i] += Request, Need[i] -= Request. Run the safety algorithm. Safe: grant. Unsafe: roll back, and Pi waits.
The running example
Five processes, three resource types with totals A = 9, B = 6, C = 7.
| Process | Allocation (A B C) | Max (A B C) | Need (A B C) |
|---|---|---|---|
| P0 | 1 1 2 | 4 3 3 | 3 2 1 |
| P1 | 2 0 1 | 3 2 2 | 1 2 1 |
| P2 | 3 1 1 | 5 1 3 | 2 0 2 |
| P3 | 0 2 0 | 2 4 2 | 2 2 2 |
| P4 | 1 1 1 | 3 3 2 | 2 2 1 |
Available. Allocation column sums are (7, 5, 5), so Available = (9, 6, 7) - (7, 5, 5) = (2, 1, 2).
Worked example 1: the Need matrix
Subtract column by column. For P2: (5 - 3, 1 - 1, 3 - 1) = (2, 0, 2). The full Need column is in the table above. A quick check: no Need entry can be negative; if one is, you subtracted the wrong way round.
Worked example 2: is the state safe?
Start with Work = (2, 1, 2) and always pick the lowest-numbered process that fits.
| Step | Can run? | Picked | Work after |
|---|---|---|---|
| 1 | P0 (3, 2, 1): no, A short. P1 (1, 2, 1): no, B short. P2 (2, 0, 2): yes | P2 | (2, 1, 2) + (3, 1, 1) = (5, 2, 3) |
| 2 | P0 (3, 2, 1): yes | P0 | (5, 2, 3) + (1, 1, 2) = (6, 3, 5) |
| 3 | P1 (1, 2, 1): yes | P1 | (6, 3, 5) + (2, 0, 1) = (8, 3, 6) |
| 4 | P3 (2, 2, 2): yes | P3 | (8, 3, 6) + (0, 2, 0) = (8, 5, 6) |
| 5 | P4 (2, 2, 1): yes | P4 | (8, 5, 6) + (1, 1, 1) = (9, 6, 7) |
Safe. One safe sequence is P2, P0, P1, P3, P4, and Work ends equal to the total (9, 6, 7), a built-in check on your additions.
Safe sequences are rarely unique. In this example P2 is the only process that can start, but once it finishes, any order of the remaining four works: there are 24 safe sequences, all beginning with P2. In an MCQ, test each option rather than hunting for "the" sequence.

Worked example 3: a request that is granted
P1 requests (0, 1, 0).
- Within Need (1, 2, 1)? Yes.
- Within Available (2, 1, 2)? Yes.
- Pretend: Available = (2, 0, 2), P1's Allocation = (2, 1, 1), P1's Need = (1, 1, 1).
Safety check: P2 fits (2, 0, 2), giving Work (5, 1, 3). Now P1 (1, 1, 1) fits, giving (7, 2, 4). Then P0 (3, 2, 1) gives (8, 3, 6), P3 gives (8, 5, 6) and P4 gives (9, 6, 7). Safe sequence P2, P1, P0, P3, P4, so the request is granted.
Worked example 4: a request that is denied
P4 requests (1, 0, 0).
- Within Need (2, 2, 1)? Yes.
- Within Available (2, 1, 2)? Yes.
- Pretend: Available = (1, 1, 2), P4's Need becomes (1, 2, 1).
Safety check: P0 needs 3 of A, P1 needs 2 of B, P2 needs 2 of A, P3 needs 2 of A and 2 of B, and P4 needs 2 of B. Nothing fits (1, 1, 2), so the new state is unsafe. The request is denied and P4 waits, even though one unit of A is sitting free. That is the whole point of avoidance: P2 needs both free units of A to finish, and giving one away could leave everyone stuck.
Single resource type: the quick bound
With one resource type and n processes that may each need up to k units, deadlock is impossible if there are at least
n(k - 1) + 1 units.
In the worst case every process holds k - 1 units and waits for one more; a single extra unit lets one finish and release its k. Example: 4 processes, each needing up to 3 units, need 4 x 2 + 1 = 9 units. With 8, all four could each hold 2 and block. If the maxima differ, add up (max - 1) for every process and add 1.
A safety example with one type: 10 printers; P0 holds 3 (max 7), P1 holds 2 (max 4), P2 holds 3 (max 6). Available = 2, Needs = 4, 2, 3. P1 finishes first (Work 4), then P0 (Work 7), then P2. Safe: P1, P0, P2.
Speed tricks
- Work must end at the total. If it does not, you made an addition error.
- Find the first process fast: compare each Need row with Available and cross out any row with a larger entry. Usually only one or two survive.
- Error before wait before safety: check Request against Need first, then Available, and only then run the safety check.
- For "which is a safe sequence" MCQs, check the first process of each option first; most wrong options fail at step one.
- Need cannot be negative, and Allocation can never exceed Max.
Common mistakes
- Computing Need as Max - Available, or Available - Allocation.
- Adding the finished process's Need to Work instead of its Allocation.
- Comparing only the total of a row instead of every column separately.
- Treating "unsafe" as "deadlocked". Unsafe means deadlock is possible, not certain.
- Granting a request just because the resources are free, without the safety check.
- Using the banker's algorithm on a detection question. Detection uses the current Request matrix, not Need, and runs on the actual state.
Practice set
Use this state: totals (6, 5, 6); Allocation P0 (1, 0, 2), P1 (2, 1, 0), P2 (0, 2, 1), P3 (1, 1, 1); Max P0 (4, 2, 3), P1 (3, 2, 2), P2 (2, 3, 1), P3 (3, 1, 4).
- What is P0's Need?
- What is Available?
- Using the lowest-index rule, give a safe sequence.
- How many processes could be first in a safe sequence?
- P0 requests (0, 1, 0). Grant or deny?
- P3 requests (0, 0, 1). Grant or deny?
- Six processes each need up to 3 units of one resource. Minimum units for guaranteed deadlock-freedom?
- 10 units; P0 holds 4 (max 8), P1 holds 3 (max 6), P2 holds 2 (max 5). Safe or unsafe?
- Three processes with maximum claims 2, 3 and 4 of one resource. Minimum units that rule out deadlock?
Show answers
- (3, 2, 1). (4 - 1, 2 - 0, 3 - 2).
- (2, 1, 2). Totals minus allocation sums (4, 4, 4).
- P1, P0, P2, P3. Work: (2, 1, 2), (4, 2, 2), (5, 2, 4), (5, 4, 5), (6, 5, 6).
- 2: P1 (1, 1, 2) and P2 (2, 1, 0) both fit (2, 1, 2).
- Deny. Available becomes (2, 0, 2) and no Need fits.
- Grant. A safe sequence remains: P2, P1, P0, P3.
- 13. 6 x 2 + 1.
- Unsafe. Available is 1 and the Needs are 4, 3 and 3.
- 7. (1 + 2 + 3) + 1.
Practise
The quiz below has 20 banker's algorithm questions, filterable by tier and difficulty. Each Need matrix, safety check and request decision was computed by a script.
Practise: 20 questions
FAQs
Is a safe sequence unique?
No. A safe state usually has several safe sequences; the running example has 24. Any one proves safety. A well-set MCQ asking for "a safe sequence" has exactly one valid option among the four, so check each option step by step rather than assuming the lowest-index order.
What is the difference between deadlock avoidance and detection?
Avoidance (the banker's algorithm) checks every request in advance and refuses any that would make the state unsafe, using the Max claims. Detection lets requests through, then periodically checks the current Allocation and Request matrices for processes that can never finish, and recovers by killing or preempting them.
Why is the banker's algorithm rarely used in real operating systems?
It needs every process to declare its maximum claim in advance, assumes a fixed number of processes and resources, and costs O(m x n^2) per check. General-purpose systems such as Linux and Windows usually ignore deadlock or prevent it by design, such as lock ordering, instead.
What does "Request > Need" mean in the request algorithm?
It means the process is asking for more than it declared it would ever need. That is an error, not a wait: the process has broken its contract with the OS. This check comes before checking availability or running the safety algorithm.
Next step
Review the theory in Deadlocks, then practise synchronisation traces in Semaphores and Classic Problems. Head back to the Operating Systems MCQ hub or try the full NSCT mock test.