Banker's Algorithm Safe-State Analysis for Processes P0P_0 Through P4P_4

Banker's Algorithm Safe-State Analysis for Processes P0P_0 Through P4P_4

Verified Sources
May 27, 2026

The Banker's algorithm is a deadlock avoidance technique for systems with multiple instances of each resource type. It uses four core data structures: Available, Allocation, Max, and Need.3 A system is in a safe state if there exists some sequence in which every process can finish with the resources that are currently available plus those released by previously completed processes.2

For the given snapshot, we must determine whether the state is safe by computing the Need matrix and then applying the safety test using

Need[i,j]=Max[i,j]Allocation[i,j].\mathrm{Need}[i,j] = \mathrm{Max}[i,j] - \mathrm{Allocation}[i,j].

If we can find a full completion order for P0P_0 through P4P_4, the state is safe; otherwise, it is unsafe.3

The given system state is:

ProcessAllocation (A B C D)(A\ B\ C\ D)Max (A B C D)(A\ B\ C\ D)
P0P_00 0 1 20\ 0\ 1\ 20 0 1 20\ 0\ 1\ 2
P1P_11 0 0 01\ 0\ 0\ 01 7 5 01\ 7\ 5\ 0
P2P_21 3 5 41\ 3\ 5\ 42 3 5 62\ 3\ 5\ 6
P3P_30 6 3 20\ 6\ 3\ 20 6 5 20\ 6\ 5\ 2
P4P_40 0 1 40\ 0\ 1\ 40 6 5 60\ 6\ 5\ 6

and

Available=(1, 5, 2, 0).\mathrm{Available} = (1,\ 5,\ 2,\ 0).

A compact view of the safety idea is:

Footnotes

  1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept. 2 3

  2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test. 2 3

  3. Operating Systems: Deadlocks - UIC Course Notes - Defines Banker's algorithm data structures and the formal safety algorithm. 2

Banker's Algorithm for Deadlock Avoidance: Worked Example

Decision Criterion

The question is not whether a process can run immediately forever, but whether there exists at least one complete safe sequence for all processes.2

Footnotes

  1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept.

  2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

Step 1: Compute the Need Matrix

Using

Need=MaxAllocation,\mathrm{Need} = \mathrm{Max} - \mathrm{Allocation},

we obtain:

ProcessNeed (A B C D)(A\ B\ C\ D)Calculation
P0P_00 0 0 00\ 0\ 0\ 0(0,0,1,2)(0,0,1,2)(0,0,1,2) - (0,0,1,2)
P1P_10 7 5 00\ 7\ 5\ 0(1,7,5,0)(1,0,0,0)(1,7,5,0) - (1,0,0,0)
P2P_21 0 0 21\ 0\ 0\ 2(2,3,5,6)(1,3,5,4)(2,3,5,6) - (1,3,5,4)
P3P_30 0 2 00\ 0\ 2\ 0(0,6,5,2)(0,6,3,2)(0,6,5,2) - (0,6,3,2)
P4P_40 6 4 20\ 6\ 4\ 2(0,6,5,6)(0,0,1,4)(0,6,5,6) - (0,0,1,4)

So the Need matrix is:

ProcessABCD
P0P_000000000
P1P_100775500
P2P_211000022
P3P_300002200
P4P_400664422

This Need computation is the standard first step in the Banker's safety algorithm.3

Footnotes

  1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept.

  2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

  3. Operating Systems: Deadlocks - UIC Course Notes - Defines Banker's algorithm data structures and the formal safety algorithm.

Applying the Safety Algorithm to the Given Snapshot

  1. 1
    Step 1

    Set Work=Available=(1,5,2,0)Work = Available = (1,5,2,0) and mark all processes unfinished. The algorithm then searches for any process whose Need is less than or equal to Work component-wise.2

    Footnotes

    1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept.

    2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

  2. 2
    Step 2

    Need0=(0,0,0,0)(1,5,2,0)Need_0 = (0,0,0,0) \leq (1,5,2,0), so P0P_0 can finish immediately. After completion, release its allocation (0,0,1,2)(0,0,1,2). New Work=(1,5,3,2)Work = (1,5,3,2).

  3. 3
    Step 3

    Need2=(1,0,0,2)(1,5,3,2)Need_2 = (1,0,0,2) \leq (1,5,3,2), so P2P_2 can finish. Release allocation (1,3,5,4)(1,3,5,4). New Work=(2,8,8,6)Work = (2,8,8,6).

  4. 4
    Step 4

    Need1=(0,7,5,0)(2,8,8,6)Need_1 = (0,7,5,0) \leq (2,8,8,6), so P1P_1 can finish. Release allocation (1,0,0,0)(1,0,0,0). New Work=(3,8,8,6)Work = (3,8,8,6).

  5. 5
    Step 5

    Need3=(0,0,2,0)(3,8,8,6)Need_3 = (0,0,2,0) \leq (3,8,8,6), so P3P_3 can finish. Release allocation (0,6,3,2)(0,6,3,2). New Work=(3,14,11,8)Work = (3,14,11,8).

  6. 6
    Step 6

    Need4=(0,6,4,2)(3,14,11,8)Need_4 = (0,6,4,2) \leq (3,14,11,8), so P4P_4 can finish. Release allocation (0,0,1,4)(0,0,1,4). New Work=(3,14,12,12)Work = (3,14,12,12).

  7. 7
    Step 7

    Because all five processes can be marked finished, the system is in a safe state. One valid safe sequence is P0,P2,P1,P3,P4\langle P_0, P_2, P_1, P_3, P_4 \rangle.3

    Footnotes

    1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept.

    2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

    3. Operating Systems: Deadlocks - UIC Course Notes - Defines Banker's algorithm data structures and the formal safety algorithm.

Safety Table

The sequence above can be summarized in tabular form:

StepChosen ProcessNeedWork BeforeAllocation ReleasedWork After
11P0P_0(0,0,0,0)(0,0,0,0)(1,5,2,0)(1,5,2,0)(0,0,1,2)(0,0,1,2)(1,5,3,2)(1,5,3,2)
22P2P_2(1,0,0,2)(1,0,0,2)(1,5,3,2)(1,5,3,2)(1,3,5,4)(1,3,5,4)(2,8,8,6)(2,8,8,6)
33P1P_1(0,7,5,0)(0,7,5,0)(2,8,8,6)(2,8,8,6)(1,0,0,0)(1,0,0,0)(3,8,8,6)(3,8,8,6)
44P3P_3(0,0,2,0)(0,0,2,0)(3,8,8,6)(3,8,8,6)(0,6,3,2)(0,6,3,2)(3,14,11,8)(3,14,11,8)
55P4P_4(0,6,4,2)(0,6,4,2)(3,14,11,8)(3,14,11,8)(0,0,1,4)(0,0,1,4)(3,14,12,12)(3,14,12,12)

Therefore, yes, the system is in a safe state.3

A key theoretical point is that safety requires the existence of at least one such sequence, not uniqueness. Other safe sequences may also exist, but one valid sequence is sufficient to prove safety.2

Footnotes

  1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept. 2

  2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

  3. Operating Systems: Deadlocks - UIC Course Notes - Defines Banker's algorithm data structures and the formal safety algorithm. 2

Fast Exam Heuristic

Always test processes with very small Need first, especially any row of (0,0,0,0)(0,0,0,0). Such a process can complete immediately and may unlock the rest of the sequence.

Initial Need by Resource Type and Process

Remaining demand for each process before running the safety algorithm.

Safe-Sequence Progression

Available Resources

Initial

Start with Work=(1,5,2,0)Work = (1,5,2,0)."

$P_0$ Completes

Step 1

P0P_0 needs nothing more, so it finishes and releases (0,0,1,2)(0,0,1,2)."

$P_2$ Completes

Step 2

With Work=(1,5,3,2)Work = (1,5,3,2), P2P_2 can finish and releases (1,3,5,4)(1,3,5,4)."

$P_1$ Completes

Step 3

The released resources make (0,7,5,0)(0,7,5,0) feasible for P1P_1."

$P_3$ Completes

Step 4

P3P_3 then satisfies its remaining need and returns its allocation."

$P_4$ Completes

Step 5

Finally, P4P_4 also completes, proving the state is safe."

Interpretation and Common Confusions

Final Conclusion

For this resource-allocation snapshot, the computed Need matrix is

P0:(0,0,0,0)P1:(0,7,5,0)P2:(1,0,0,2)P3:(0,0,2,0)P4:(0,6,4,2)\begin{aligned} P_0 &: (0,0,0,0) \\ P_1 &: (0,7,5,0) \\ P_2 &: (1,0,0,2) \\ P_3 &: (0,0,2,0) \\ P_4 &: (0,6,4,2) \end{aligned}

Starting from

Available=(1,5,2,0),Available = (1,5,2,0),

the safety algorithm can complete all processes in the order

P0, P2, P1, P3, P4.\langle P_0,\ P_2,\ P_1,\ P_3,\ P_4 \rangle.

Hence, the system is in a safe state.3

Footnotes

  1. Banker's Algorithm - GeeksforGeeks - Explains the Need matrix, safety algorithm, and safe sequence concept.

  2. Operating System - Deadlock Avoidance (Banker's Algorithm) - TutorialsPoint - Describes safe states, Work and Finish vectors, and the standard safety test.

  3. Operating Systems: Deadlocks - UIC Course Notes - Defines Banker's algorithm data structures and the formal safety algorithm.

Important Distinction

A safe state means at least one full completion ordering exists. It does not mean every possible next allocation choice will remain safe.

Knowledge Check

Question 1 of 4
Q1Single choice

What formula is used to compute the Need matrix in Banker's algorithm?

Explore Related Topics

1

Page Replacement Analysis: FIFO vs LRU for a 4-Frame Reference String

The page‑replacement analysis compares FIFO and LRU for the reference string 1,3,4,4,3,2,1,7,5,6,4,2,1,21,3,4,4,3,2,1,7,5,6,4,2,1,2 using four frames, showing that LRU incurs fewer faults.

  • FIFO simulation results in 1010 page faults.
  • LRU simulation results in 99 page faults.
  • LRU is superior here because it evicts the least recently used page, better exploiting temporal locality.
  • FIFO evicts the oldest loaded page, which can discard recently used pages and lead to higher fault rates.
  • This example illustrates why LRU often outperforms FIFO, though FIFO’s simplicity can cause anomalies such as Belady’s.
2

Justifying Why a Cycle in a Resource Allocation Graph Does Not Always Imply Deadlock

A cycle in a resource allocation graph (RAG) does not always imply deadlock; the conclusion hinges on whether each resource type involved has a single instance or multiple instances.

  • No cycle → the system cannot be deadlocked.
  • Cycle + every resource in the cycle has one instance ⇒ deadlock is guaranteed.
  • Cycle + any resource has multiple instances ⇒ deadlock is possible but not certain (Cycle\centernot    Deadlock\text{Cycle} \centernot\implies \text{Deadlock}).
  • Thus a cycle is a necessary but not sufficient condition for deadlock in multi‑instance systems.
  • Example: with two instances of R1R1 and R2R2, a cycle P1R2P2R1P1P1 \rightarrow R2 \rightarrow P2 \rightarrow R1 \rightarrow P1 can be broken when another process releases an instance.
3

Understanding Belady's Anomaly in Operating Systems

Belady's Anomaly shows that, for some page‑replacement policies, adding more physical frames can increase the number of page faults.

  • FIFO (a non‑stack algorithm) does not satisfy the inclusion property and can exhibit the anomaly.
  • On the reference string 1,2,3,4,1,2,5,1,2,3,4,51,2,3,4,1,2,5,1,2,3,4,5, FIFO yields 99 faults with 33 frames but 1010 faults with 44 frames.
  • Stack algorithms such as LRU or Optimal obey M(N,t)M(N+1,t)M(N,t)\subseteq M(N+1,t), guaranteeing that more frames never raise fault counts.
  • Designing a virtual‑memory system with stack‑based replacement eliminates Belady's Anomaly.