D7A-Talha-Mulani-40
Banker's Algorithm — Deadlock Avoidance
Aim
To implement Deadlock Avoidance using the Banker's Algorithm, determine the Need matrix
from given Allocation and Max matrices, apply the Safety Algorithm to identify a safe sequence
of process execution, and verify whether the system remains in a safe state after resource
allocation.
Theory
Definition
Banker's Algorithm is a resource allocation and deadlock avoidance algorithm developed by
Edsger Dijkstra. It simulates resource allocation for each process and evaluates whether the
resulting state is safe before committing to any actual allocation. The algorithm gets its name from
the analogy of a banker who decides whether to grant loans while ensuring the bank never runs
out of funds.
Purpose — Deadlock Avoidance
Unlike deadlock prevention, which restricts resource usage, deadlock avoidance allows flexible
resource allocation but requires the system to make informed decisions at each allocation
request. The Banker's Algorithm ensures that the system never enters an unsafe state, thereby
avoiding deadlock entirely by refusing requests that could lead to an unresolvable circular wait.
Key Concepts
Available: A vector of length m (number of resource types) representing the number of available
instances of each resource type at any given time. Available[j] = k means k instances of resource
type j are currently free.
Max: An n × m matrix defining the maximum number of instances of each resource type that each
process may ever request. Max[i][j] = k means process i may request at most k instances of
resource type j.
Allocation: An n × m matrix defining the number of instances of each resource type currently
allocated to each process. Allocation[i][j] = k means process i is currently holding k instances of
resource type j.
Need: An n × m matrix representing the remaining resource need of each process. It is computed
as:
Need[i][j] = Max[i][j] − Allocation[i][j]
This indicates how many more resources process i may still request before completing.
Operating Systems | Banker's Algorithm — Deadlock Avoidance | Page 1
D7A-Talha-Mulani-40
Safe State
Safe State: A state is considered safe if the system can allocate resources to each process in
some order and still avoid deadlock. A safe state guarantees that every process can eventually
obtain all the resources it needs and complete execution.
Safe Sequence: A sequence of all processes ⟨ P1, P2, ..., Pn⟩ is a safe sequence if, for each
process Pi, the resources Pi still needs can be satisfied by the currently available resources plus
the resources held by all processes Pj where j < i (i.e., processes that have already completed
and released their resources).
If no safe sequence exists, the system is in an unsafe state, meaning deadlock is possible though
not guaranteed.
Working Principle
Before granting any resource request, the system tentatively allocates the requested resources
and then runs the Safety Algorithm. The Safety Algorithm simulates process completion by
repeatedly finding a process whose remaining needs can be satisfied by the current available
resources. When such a process is found, its allocated resources are released back to the pool.
If all processes can complete in this simulation, the resulting state is safe and the allocation is
granted. Otherwise, the request is deferred until a safe allocation is possible.
Algorithm: Banker's Algorithm
1. Read the number of processes n, number of resource types r, Allocation matrix, Max
matrix, and Available vector.
2. Compute the Need matrix: Need[i][j] = Max[i][j] − Allocation[i][j] for all i, j.
3. Initialize Work = Available and Finish[i] = false for all i.
4. Find an index i such that: Finish[i] = false AND Need[i] ≤ Work (element-wise).
5. If such a process i is found:
a. Simulate resource release: Work = Work + Allocation[i].
b. Set Finish[i] = true and add i to the safe sequence.
c. Return to step 4.
6. If no such process is found and not all processes are finished, go to step 7.
7. If Finish[i] = true for all i: the system is in a Safe State — output the safe sequence. Else:
the system is in an Unsafe State — deadlock may occur.
Operating Systems | Banker's Algorithm — Deadlock Avoidance | Page 2
D7A-Talha-Mulani-40
Input Data
Processes: P1, P2, P3, P4, P5
Resources: A, B, C
Available: A = 3, B = 3, C = 2
Allocation Matrix
Process
A
B
C
P1
0
1
0
P2
2
0
0
P3
3
0
2
P4
2
1
1
P5
0
0
2
Process
A
B
C
P1
7
5
3
P2
3
2
2
P3
9
0
2
P4
4
2
2
P5
5
3
3
Max Matrix
Program — Java Implementation
public class BankersAlgorithm {
public static void main(String[] args) {
int n = 5; // number of processes
int r = 3; // number of resource types (A, B, C)
int[][] allocation = {
{0, 1, 0}, // P1
{2, 0, 0}, // P2
{3, 0, 2}, // P3
{2, 1, 1}, // P4
{0, 0, 2}
// P5
};
int[][] max = {
{7, 5, 3}, // P1
Operating Systems | Banker's Algorithm — Deadlock Avoidance | Page 3
D7A-Talha-Mulani-40
{3, 2, 2},
{9, 0, 2},
{4, 2, 2},
{5, 3, 3}
// P2
// P3
// P4
// P5
};
int[] available = {3, 3, 2};
// Step 1: Compute Need matrix
int[][] need = new int[n][r];
System.out.println("Need Matrix:");
System.out.printf("%-6s %-6s %-6s %-6s%n", "Proc", "A", "B", "C");
for (int i = 0; i < n; i++) {
for (int j = 0; j < r; j++)
need[i][j] = max[i][j] - allocation[i][j];
System.out.printf("%-6s %-6d %-6d %-6d%n",
"P" + (i + 1), need[i][0], need[i][1], need[i][2]);
}
// Step 2: Safety Algorithm
boolean[] finished = new boolean[n];
int[]
safeSeq
= new int[n];
int[]
work
= available.clone();
int
count
= 0;
while (count < n) {
boolean found = false;
for (int i = 0; i < n; i++) {
if (!finished[i]) {
// Check if Need[i] <= Work
boolean canRun = true;
for (int j = 0; j < r; j++) {
if (need[i][j] > work[j]) { canRun = false; break; }
}
if (canRun) {
// Simulate allocation: release resources
for (int j = 0; j < r; j++)
work[j] += allocation[i][j];
safeSeq[count++] = i + 1;
finished[i] = true;
found = true;
}
}
}
if (!found) break; // No progress possible
}
// Step 3: Output result
System.out.println();
if (count == n) {
System.out.print("Safe Sequence: ");
for (int i = 0; i < n; i++)
System.out.print("P" + safeSeq[i] + (i < n - 1 ? " -> " : ""));
System.out.println("\nSystem is in Safe State");
} else {
System.out.println("System is in Unsafe State — Deadlock may occur");
}
}
}
Operating Systems | Banker's Algorithm — Deadlock Avoidance | Page 4
D7A-Talha-Mulani-40
Output
Need Matrix (Need = Max − Allocation)
Process
A
B
C
P1
7
4
3
P2
1
2
2
P3
6
0
0
P4
2
1
1
P5
5
3
1
Safety Algorithm — Step-by-Step Execution
Step
Process
Need (A B C)
Available Before
Available After
1
P2
122
332
532
2
P4
211
532
743
3
P5
531
743
745
4
P1
743
745
755
5
P3
600
755
10 5 7
Safe Sequence: P2 → P4 → P5 → P1 → P3
Message: System is in Safe State.
Result
The Banker's Algorithm was successfully implemented in Java for a system with 5 processes and
3 resource types. The Need matrix was correctly computed from the given Allocation and Max
matrices. The Safety Algorithm identified a valid safe sequence: P2 → P4 → P5 → P1 → P3.
Since all processes can complete execution in this order without exceeding available resources
at any point, the system is confirmed to be in a Safe State. The experiment demonstrates that
the Banker's Algorithm effectively prevents deadlock by ensuring resource allocation never leads
the system into an unsafe condition.
Operating Systems | Banker's Algorithm — Deadlock Avoidance | Page 5