Banker's Algorithm: Need Matrix, Safe Sequence and Request Checks, Worked

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Compute Need = Max - Allocation, row by row.
  2. Set Work = Available and mark every process unfinished.
  3. Find an unfinished process whose Need is less than or equal to Work in every column.
  4. Pretend it runs to completion: Work = Work + its Allocation, and mark it finished. Write it down as the next process in the sequence.
  5. 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):

  1. If Request > Need[i] in any column: error, the process exceeded its claim.
  2. If Request > Available in any column: Pi waits, the resources are not free.
  3. 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.

Banker's algorithm safety check: Work starts at Available (2, 1, 2) and grows as P2, P0, P1, P3 and P4 finish and release their allocation, ending at (9, 6, 7), so the state is safe.
The page's running example. At each step the first process whose Need fits in Work runs, then returns its Allocation.

Worked example 3: a request that is granted

P1 requests (0, 1, 0).

  1. Within Need (1, 2, 1)? Yes.
  2. Within Available (2, 1, 2)? Yes.
  3. 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).

  1. Within Need (2, 2, 1)? Yes.
  2. Within Available (2, 1, 2)? Yes.
  3. 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

Common mistakes

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

  1. What is P0's Need?
  2. What is Available?
  3. Using the lowest-index rule, give a safe sequence.
  4. How many processes could be first in a safe sequence?
  5. P0 requests (0, 1, 0). Grant or deny?
  6. P3 requests (0, 0, 1). Grant or deny?
  7. Six processes each need up to 3 units of one resource. Minimum units for guaranteed deadlock-freedom?
  8. 10 units; P0 holds 4 (max 8), P1 holds 3 (max 6), P2 holds 2 (max 5). Safe or unsafe?
  9. Three processes with maximum claims 2, 3 and 4 of one resource. Minimum units that rule out deadlock?
Show answers
  1. (3, 2, 1). (4 - 1, 2 - 0, 3 - 2).
  2. (2, 1, 2). Totals minus allocation sums (4, 4, 4).
  3. P1, P0, P2, P3. Work: (2, 1, 2), (4, 2, 2), (5, 2, 4), (5, 4, 5), (6, 5, 6).
  4. 2: P1 (1, 1, 2) and P2 (2, 1, 0) both fit (2, 1, 2).
  5. Deny. Available becomes (2, 0, 2) and no Need fits.
  6. Grant. A safe sequence remains: P2, P1, P0, P3.
  7. 13. 6 x 2 + 1.
  8. Unsafe. Available is 1 and the Needs are 4, 3 and 3.
  9. 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

Interactive quiz loading. The first 10 questions are shown below.

  1. In the banker's algorithm, how is the Need matrix computed?

    1. Need = Max - Allocation
    2. Need = Available - Allocation
    3. Need = Max - Available
    4. Need = Allocation - Max
    Show answer

    Answer: A. Need = Max - Allocation

    Need[i][j] is how many more instances of resource j process i may still request: its declared maximum claim minus what it already holds. Available does not enter the Need calculation.

  2. A system state is called safe in the banker's algorithm when:

    1. No process is currently waiting for a resource
    2. There is at least one order in which every process can obtain its maximum need and finish
    3. Available is at least as large as every row of Allocation
    4. Every process's need can be met from Available at the same time
    Show answer

    Answer: B. There is at least one order in which every process can obtain its maximum need and finish

    Safe means a safe sequence exists: an ordering where each process's remaining need can be met from what is free plus what earlier processes release. It does not require meeting all needs at once.

  3. Which statement about an unsafe state is correct?

    1. It may lead to deadlock, but the system is not necessarily deadlocked yet
    2. It means the system is already deadlocked
    3. It can never lead to deadlock if Available is non-zero
    4. It means at least one process has requested more than its maximum claim
    Show answer

    Answer: A. It may lead to deadlock, but the system is not necessarily deadlocked yet

    Deadlocked states are a subset of unsafe states. In an unsafe state the OS can no longer guarantee completion, but processes might release resources early or never use their full claim, so deadlock is possible, not certain.

  4. What must each process declare before the banker's algorithm can manage it?

    1. Its maximum claim of each resource type
    2. Its total CPU burst time
    3. The order in which it will request resources
    4. Its priority relative to other processes
    Show answer

    Answer: A. Its maximum claim of each resource type

    The algorithm needs the Max matrix: the most of each resource type a process may ever hold. Without it, Need cannot be computed and safety cannot be checked. This advance-knowledge requirement is its main practical drawback.

  5. Five processes share identical units of one resource, and each may need up to 4 units at once. What is the smallest number of units that guarantees deadlock can never occur?

    1. 15
    2. 20
    3. 5
    4. 16
    Show answer

    Answer: D. 16

    In the worst case every process holds 3 units (one short of its maximum): 5 x 3 = 15 units, all blocked. One more unit lets some process finish and release its 4. So the answer is n(k - 1) + 1 = 5 x 3 + 1 = 16.

  6. For n processes and m resource types, what is the worst-case time complexity of the banker's safety algorithm?

    1. O(m x n)
    2. O(n log n)
    3. O(m x n^2)
    4. O(m^2 x n)
    Show answer

    Answer: C. O(m x n^2)

    Each pass scans up to n processes and compares an m-element Need row with Work, and up to n passes may be needed before every process is marked finished. That gives m x n x n, as Silberschatz states.

  7. Process P1 has Allocation (1, 3, 2) and Max (3, 4, 4) for resources A, B, C. What is P1's Need?

    1. (4, 7, 6)
    2. (2, 1, 2)
    3. (1, 3, 2)
    4. (-2, -1, -2)
    Show answer

    Answer: B. (2, 1, 2)

    Need = Max - Allocation, column by column: (3 - 1, 4 - 3, 4 - 2) = (2, 1, 2).

  8. A system has 10 instances of A, 5 of B and 7 of C. Current allocations are P0 (0, 1, 0), P1 (3, 0, 2), P2 (2, 1, 1), P3 (1, 0, 2). What is the Available vector?

    1. (6, 2, 5)
    2. (5, 3, 2)
    3. (4, 3, 2)
    4. (5, 3, 4)
    Show answer

    Answer: C. (4, 3, 2)

    Sum the allocation columns: A = 0 + 3 + 2 + 1 = 6, B = 2, C = 5, so the total allocated is (6, 2, 5). Available = total - allocated = (4, 3, 2).

  9. Available (1, 2, 1). P0 alloc (1, 2, 2) max (3, 4, 3); P1 alloc (2, 1, 2) max (3, 4, 5); P2 alloc (1, 2, 1) max (1, 3, 2); P3 alloc (2, 2, 0) max (5, 2, 0); P4 alloc (1, 2, 0) max (3, 3, 3). Which process can be the first in a safe sequence?

    1. P0
    2. P1
    3. P2
    4. P3
    Show answer

    Answer: C. P2

    Needs are P0 (2, 2, 1), P1 (1, 3, 3), P2 (0, 1, 1), P3 (3, 0, 0), P4 (2, 1, 3). Only P2's need fits within Available (1, 2, 1), so it must run first.

  10. Available (1, 2, 1). P0 alloc (1, 2, 2) max (3, 4, 3); P1 alloc (2, 1, 2) max (3, 4, 5); P2 alloc (1, 2, 1) max (1, 3, 2); P3 alloc (2, 2, 0) max (5, 2, 0); P4 alloc (1, 2, 0) max (3, 3, 3). P4 requests (0, 1, 0). What does the banker's algorithm do?

    1. Denies it, because the resulting state is unsafe
    2. Grants it, because the resulting state is still safe
    3. Rejects it as an error, because it exceeds P4's need
    4. Makes P4 wait, because the resources are not available
    Show answer

    Answer: B. Grants it, because the resulting state is still safe

    The request is within P4's need (2, 1, 3) and within Available. Pretending to grant it leaves Available (1, 1, 1), and a safe sequence still exists (P2, P0, P1, P3, P4), so it is granted.

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.