Assignment6
Assignment6
*Assignment no: 06
Title : Write a program to implement Banker’s Algorithm
Objective :To implement the Banker’s Algorithm to determine the safe state of a
system during resource allocation.
**/
import java.util.*;
while (!sc.hasNextInt()) {
System.out.print("Invalid input. Please enter a positive integer for
the number of resources: ");
sc.next(); // Clear the invalid input
}
int res = sc.nextInt();
while (res <= 0) {
System.out.print("Invalid input. Please enter a positive integer for
the number of resources: ");
res = sc.nextInt();
}
int[][] max = new int[n][res]; //max[i][j] = maximum resources of process i
of resource j
int[][] allot = new int[n][res]; //allot[i][j] = allocated resources of
process i of resource j
int[][] need = new int[n][res]; //need[i][j] = need resources of process i
of resource j
int[] avail = new int[res]; //avail[j] = available resources of resource j
//Validation 2: Check if the maximum and allocated resources are non-
negative integers
System.out.println("----- Enter the maximum resources ----- ");
for (int i = 0; i < n; i++) {
System.out.println("-------------------------Process "+
(i+1)+"-------------------------");
for (int j = 0; j < res; j++) {
System.out.print("Enter the maximum resources for process " + (i+1)
+ " of resource " + (j+1) + ": ");
while (!sc.hasNextInt()) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
sc.next(); // Clear the invalid input
}
max[i][j] = sc.nextInt();
while (max[i][j] < 0) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
max[i][j] = sc.nextInt();
}
}
}
System.out.println("----- Enter the allocated resources ----- ");
for (int i = 0; i < n; i++) {
System.out.println("-------------------------Process "+
(i+1)+"-------------------------");
for (int j = 0; j < res; j++) {
System.out.print("Enter the allocated resources for process " +
(i+1) + " of resource " + (j+1) + ": ");
while (!sc.hasNextInt()) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
sc.next(); // Clear the invalid input
}
allot[i][j] = sc.nextInt();
while (allot[i][j] < 0) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
allot[i][j] = sc.nextInt();
}
}
}
System.out.println("----- Enter the available resources ----- ");
for (int i = 0; i < res; i++) {
System.out.print("Enter the available resources of resource " + (i+1) +
": ");
while (!sc.hasNextInt()) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
sc.next(); // Clear the invalid input
}
avail[i] = sc.nextInt();
while (avail[i] < 0) {
System.out.print("Invalid input. Please enter a non-negative
integer: ");
avail[i] = sc.nextInt();
}
}
//Validation 3: Check if the allocated resources are less than or equal to
the maximum resources
for (int i = 0; i < n; i++) {
for (int j = 0; j < res; j++) {
if (allot[i][j] > max[i][j]) {
System.out.println("Error: Allocated resources cannot exceed
maximum resources.");
return;
}
}
}
//Validation 4: Check if the need matrix is valid (non-negative and less
than or equal to max matrix)
}
//If the system is in a safe state, print the safe sequence
System.out.println("System is in a safe state.\nSafe sequence is: ");
for (int i = 0; i < n; i++) {
//Print the safe sequence
System.out.print("P" + safeSeq[i] + " ");
}
}
}
/*
Output:
Banker's Algorithm
Enter the number of processes (positive integer): 5
Enter the number of resources (positive integer): 3
----- Enter the maximum resources -----
-------------------------Process 1-------------------------
Enter the maximum resources for process 1 of resource 1: 7
Enter the maximum resources for process 1 of resource 2: 5
Enter the maximum resources for process 1 of resource 3: 3
-------------------------Process 2-------------------------
Enter the maximum resources for process 2 of resource 1: 3
Enter the maximum resources for process 2 of resource 2: 2
Enter the maximum resources for process 2 of resource 3: 2
-------------------------Process 3-------------------------
Enter the maximum resources for process 3 of resource 1: 9
Enter the maximum resources for process 3 of resource 2: 0
Enter the maximum resources for process 3 of resource 3: 2
-------------------------Process 4-------------------------
Enter the maximum resources for process 4 of resource 1: 2
Enter the maximum resources for process 4 of resource 2: 2
Enter the maximum resources for process 4 of resource 3: 2
-------------------------Process 5-------------------------
Enter the maximum resources for process 5 of resource 1: 4
Enter the maximum resources for process 5 of resource 2: 3
Enter the maximum resources for process 5 of resource 3: 3
----- Enter the allocated resources -----
-------------------------Process 1-------------------------
Enter the allocated resources for process 1 of resource 1: 0
Enter the allocated resources for process 1 of resource 2: 1
Enter the allocated resources for process 1 of resource 3: 0
-------------------------Process 2-------------------------
Enter the allocated resources for process 2 of resource 1: 2
Enter the allocated resources for process 2 of resource 2: 0
Enter the allocated resources for process 2 of resource 3: 0
-------------------------Process 3-------------------------
Enter the allocated resources for process 3 of resource 1: 3
Enter the allocated resources for process 3 of resource 2: 0
Enter the allocated resources for process 3 of resource 3: 2
-------------------------Process 4-------------------------
Enter the allocated resources for process 4 of resource 1: 2
Enter the allocated resources for process 4 of resource 2: 1
Enter the allocated resources for process 4 of resource 3: 1
-------------------------Process 5-------------------------
Enter the allocated resources for process 5 of resource 1: 0
Enter the allocated resources for process 5 of resource 2: 0
Enter the allocated resources for process 5 of resource 3: 2
----- Enter the available resources -----
Enter the available resources of resource 1: 3
Enter the available resources of resource 2: 3
Enter the available resources of resource 3: 2
----- Calculating Need Matrix -----