Program No 11 Linux 80
Program No 11 Linux 80
PROGRAM NO:11
AIM : To study Bankers Algorithm.
anker’s Algorithm
The Banker’s Algorithm is a deadlock avoidance algorithm used in operating systems to allocate resources
safely to processes. It was developed by Edsger Dijkstra and is named because it simulates a banking system
where a banker ensures that customers (processes) never exceed available resources.
The system has fixed instances of resources (like CPU, memory, printers, etc.). Processes request resources
dynamically, and the system decides whether to grant them safely. A safe sequence of process execution
ensures that all processes complete without deadlock.
Key Concepts
The algorithm uses four primary matrices:
Algorithm
Safety Algorithm
MMEC,MULLANA Page | 50
KAJAL SAH 4C3 11232680
this.numResources = numResources;
available = new int[numResources];
max = new int[numProcesses][numResources];
allocation = new int[numProcesses][numResources];
need = new int[numProcesses][numResources];
}
public void inputMatrices(Scanner scanner) {
System.out.println("Enter Available Resources: ");
for (int i = 0; i < numResources; i++) {
available[i] = scanner.nextInt();
}
System.out.println("Enter Maximum Resource Matrix:");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numResources; j++) {
max[i][j] = scanner.nextInt();
}
}
System.out.println("Enter Allocation Matrix:");
for (int i = 0; i < numProcesses; i++) {
for (int j = 0; j < numResources; j++) {
allocation[i][j] = scanner.nextInt();
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
public boolean isSafeState() {
boolean[] finish = new boolean[numProcesses];
int[] work = available.clone();
int[] safeSequence = new int[numProcesses];
int count = 0;
while (count < numProcesses) {
boolean found = false;
for (int i = 0; i < numProcesses; i++) {
if (!finish[i]) {
boolean canAllocate = true;
for (int j = 0; j < numResources; j++) {
if (need[i][j] > work[j]) {
canAllocate = false;
break;
}
}
if (canAllocate) {
for (int j = 0; j < numResources; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finish[i] = true;
found = true;
}
}
}
if (!found) {
System.out.println("System is in an unsafe state!");
MMEC,MULLANA Page | 51
KAJAL SAH 4C3 11232680
return false;
}
}
System.out.print("Safe Sequence: ");
for (int i : safeSequence) {
System.out.print("P" + i + " ");
}
System.out.println();
return true;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of processes and resources: ");
int numProcesses = scanner.nextInt();
int numResources = scanner.nextInt();
BankersAlgorithm ba = new BankersAlgorithm(numProcesses, numResources);
ba.inputMatrices(scanner);
ba.isSafeState();
scanner.close();
}
}
Example Execution
Input:
Enter number of processes and resources: 5 3
332
753
322
902
222
433
010
2 00
3 02
211
002
Calculation:
MMEC,MULLANA Page | 52
KAJAL SAH 4C3 11232680
Need Matrix (Max - Allocation):
743
122
600
011
431
Available Resources:
332
Safe Sequence Output:
Safe Sequence: P1 P3 P4 P0 P2
MMEC,MULLANA Page | 53