Deadlock Avoidance and Banker's Algorithm: Worked Safety and Resource-Request Checks

Understand why availability alone cannot justify a resource grant. Build the Need matrix, trace a safe sequence, and test two requests from the same original state.

KnowledgeGate Team

Exam prep & CS education

Updated 27 Oct 20265 min read

A process may request fewer resources than Available and still be denied. The request can pass the immediate availability check but leave no safe order in which every process can finish. Deadlock avoidance differs from prevention and detection: it uses Need, the safety test, and tentative request checks to determine whether a request can be granted safely.

Deadlock avoidance: what Banker's algorithm guarantees

The approaches differ in when they act:

  • Prevention makes deadlock structurally impossible by breaking at least one Coffman condition.

  • Avoidance examines each request before granting it and keeps the system in a safe state.

  • Detection and recovery permits deadlock, detects it later, and then takes corrective action.

The earlier Deadlock in Operating Systems: Four Conditions, Banker's Algorithm, and Worked Examples supplies the Coffman-condition, resource-graph, and recovery context. Request authorization starts from a narrower question: does one tentative allocation preserve at least one completion order?

A state is safe if at least one order lets every process obtain its remaining maximum claim and finish. Every safe state is deadlock-free. An unsafe state signals risk, not proof of an existing deadlock.

The algorithm assumes fixed resource types, known total instances, and a maximum claim declared in advance by every process.

Banker's algorithm data structures and component-wise tests

For n processes and m resource types, the algorithm maintains four structures:

  • Available[m]: instances of each resource type currently free.

  • Max[n][m]: maximum claim of every process.

  • Allocation[n][m]: resources currently held by every process.

  • Need[n][m]: resources each process may still request, where Need[i][j] = Max[i][j] - Allocation[i][j].

Vector comparisons are component-wise. (1,2,2) <= (3,3,2) is true, while (4,1,0) <= (3,3,2) is false because its first component fails. Row totals would hide that failure.

The safety algorithm follows this loop:

  1. Set Work = Available and every Finish[i] = false.

  2. Find an unfinished process whose Need[i] <= Work.

  3. Simulate its completion by setting Work = Work + Allocation[i], then mark it finished.

  4. Repeat until every process finishes or no eligible process remains.

Available records the current state, while Work is the simulation's scratch vector. A finished process releases its current Allocation, not its Max or Need.

Banker's algorithm worked example: calculate Need and find a safe sequence

Consider processes P0 to P4, resource types A, B, and C, and initial Available = (3,3,2).

Process

Allocation A B C

Max A B C

Need A B C

P0

0 1 0

7 5 3

7 4 3

P1

2 0 0

3 2 2

1 2 2

P2

3 0 2

9 0 2

6 0 0

P3

2 1 1

2 2 2

0 1 1

P4

0 0 2

4 3 3

4 3 1

Calculate Need by subtracting Allocation from Max:

  • P0: (7,5,3) - (0,1,0) = (7,4,3)

  • P3: (2,2,2) - (2,1,1) = (0,1,1)

It also gives P1 = (1,2,2), P2 = (6,0,0), and P4 = (4,3,1).

Now trace one safety test:

Step

Process selected

Why it can finish

Work after releasing Allocation

Start

None

Initial state

(3,3,2)

1

P1

(1,2,2) <= (3,3,2)

(3,3,2) + (2,0,0) = (5,3,2)

2

P3

(0,1,1) <= (5,3,2)

(5,3,2) + (2,1,1) = (7,4,3)

3

P4

(4,3,1) <= (7,4,3)

(7,4,3) + (0,0,2) = (7,4,5)

4

P0

(7,4,3) <= (7,4,5)

(7,4,5) + (0,1,0) = (7,5,5)

5

P2

(6,0,0) <= (7,5,5)

(7,5,5) + (3,0,2) = (10,5,7)

All processes finish, so <P1, P3, P4, P0, P2> is one safe sequence. The same baseline has 16 valid safe sequences, enumerated in Banker's Algorithm for GATE: Safe Sequence Numericals, together with a separate minimum-extra-resource problem. A request decision needs only one valid sequence, but it must preserve the original state until the tentative scan succeeds.

Safety test table tracing Work from (3,3,2) to (10,5,7) as P1, P3, P4, P0 and P2 finish in that safe sequence.

Resource-request algorithm: audit all three changed fields

For a request by Pi, first require Request_i <= Need_i, then require Request_i <= Available. Failure means the process exceeded its declared maximum or must wait for resources. Passing both checks proves only that the request is admissible, not that it is safe.

If both checks pass, subtract the request from Available, add it to Allocation_i, and subtract it from Need_i. Run the full safety algorithm on those tentative values. Commit all three changes only when the tentative state is safe; otherwise restore all three.

Trial A starts from the original state and lets P2 request (2,0,0). Both preliminary comparisons pass:

  • (2,0,0) <= Need2 (6,0,0)

  • (2,0,0) <= Available (3,3,2)

Field

Original value

Tentative value

Available

(3,3,2)

(1,3,2)

Allocation2

(3,0,2)

(5,0,2)

Need2

(6,0,0)

(4,0,0)

The tentative Work begins at (1,3,2). P1 finishes first and releases (2,0,0), giving (3,3,2). P3 then releases (2,1,1), giving (5,4,3). P2 now satisfies Need2 (4,0,0) and releases its updated Allocation2 (5,0,2), giving (10,4,5). P0 and P4 finish next, taking Work to (10,5,5) and then (10,5,7). The safe sequence <P1, P3, P2, P0, P4> proves that the request may be granted.

Unsafe request example: exhausting C blocks every process

Trial B is independent: restore the original state before testing P0's request (0,0,2). It also passes both preliminary comparisons:

  • (0,0,2) <= Need0 (7,4,3)

  • (0,0,2) <= Available (3,3,2)

Field

Original value

Tentative value

Value after denial

Available

(3,3,2)

(3,3,0)

(3,3,2)

Allocation0

(0,1,0)

(0,1,2)

(0,1,0)

Need0

(7,4,3)

(7,4,1)

(7,4,3)

At Work (3,3,0), no process can start. P0 needs A, B, and C; P1 needs two C; P2 needs six A; P3 needs one C; and P4 needs four A. Every Finish value therefore remains false and the safety scan stops before its first release.

The tentative state is unsafe, so P0 must wait. Rolling back Available alone would corrupt the accounting: Allocation0 and Need0 must return to their original values as the table shows. The original state remains safe and is not deadlocked.

Banker's algorithm exam patterns and high-cost traps

Common questions ask for a missing Need or Available vector, one or all safe sequences, an independent request decision, or the distinction between avoidance, prevention, and detection.

Watch for these expensive mistakes:

  • Adding Need to Work instead of adding Allocation.

  • Comparing row totals instead of checking every resource component.

  • Mutating the original state when separate requests are independent.

  • Declaring a state unsafe when the first chosen process cannot run, without checking the other unfinished processes.

  • Assuming that a safe sequence must be unique.

  • Calling every unsafe state deadlocked.

In timed practice, copy the table, compute Need, circle processes satisfying Need <= Work, and update Work after each completion. Audit every component. The GATE Test Series is a place to practise Operating Systems in test conditions.

Banker's algorithm short version and next step

Keep this four-line memory aid:

  1. Need = Max - Allocation.

  2. Availability alone is not enough.

  3. Pretend to allocate, then run the safety test.

  4. Grant only if every process can finish in some order.

For the wider subject sequence, GATE Guidance by Sanchit Sir includes Operating System and Deadlock in its live curriculum. The GATE CS Exam Preparation Courses & Test Series category is the broader catalogue route. Practise by rebuilding the Need table and writing every Work update so mistakes become visible.