import java.util.*;
class Bankers {
public static void main
(String[] args
) { int[][] curr = new int[5][5];
int[][] max_claim = new int[5][5];
int[] avl = new int[5];
int[] alloc = {0, 0, 0, 0, 0};
int[] max_res = new int[5];
int[] running = new int[5];
int i, j, exec, r, p;
int count = 0;
boolean safe = false;
Scanner sc
= new Scanner
(System.
in); System.
out.
println("\nEnter the number of resources: "); r = sc.nextInt();
System.
out.
println("\nEnter the number of processes: "); p = sc.nextInt();
for (i = 0; i < p; i++) {
running[i] = 1;
count++;
}
//Enter the total number of resources
System.
out.
println("\nEnter total # of resources: "); for (i = 0; i < r; i++){
max_res[i] = sc.nextInt();
}
//Enter the Resources already allocated to each process
System.
out.
println("\nEnter Allocated Resource Table: "); for (i = 0; i < p; i++) {
for (j = 0; j < r; j++){
curr[i][j] = sc.nextInt();
}
}
//Enter the total number of resources required by each process to complete
System.
out.
println("\nEnter Maximum Claim table: "); for (i = 0; i < p; i++) {
for (j = 0; j < r; j++)
max_claim[i][j] = sc.nextInt();
}
//Printing total resources
System.
out.
println("\nThe Claim Vector is: "); for (i = 0; i < r; i++){
System.
out.
println(max_res
[i
]); }
//Printing the Allocated Table
System.
out.
println("\nThe Allocated Resource Table:\n"); for (i = 0; i < p; i++) {
for (j = 0; j < r; j++){
System.
out.
println(curr
[i
][j
]); }
}
// Printing total resources required to complete for each process
System.
out.
println("\nThe Maximum Claim Table:\n"); for (i = 0; i < p; i++) {
for (j = 0; j < r; j++){
System.
out.
println(max_claim
[i
][j
]); }
}
for (i = 0; i < p; i++)
for (j = 0; j < r; j++)
alloc[j] += curr[i][j];
//Calculating the total resources allocated for all processes
System.
out.
println("\nAllocated resources: "); for (i = 0; i < r; i++)
for (i = 0; i < r; i++) //Calculating available resources for allocating
avl[i] = max_res[i] - alloc[i];
//Printing available resources for use
System.
out.
println("\nAvailable resources: "); for (i = 0; i < r; i++)
while (count != 0) {
safe = false;
for (i = 0; i < p; i++) {
if (running[i] != 0) {
exec = 1;
for (j = 0; j < r; j++) {
if (max_claim[i][j] - curr[i][j] > avl[j]) { // Checking if required resources of process are greater than available resources
exec = 0;
break;
}
}
//If required resources for a process is less than available then it is executed
if (exec !=0) {
System.
out.
println("\nProcess%d is executing.\n" + i
+ 1); running[i] = 0;
count--; //Decrease count to see how many processes are remaining
safe = true;
for (j = 0; j < r; j++)
avl[j] += curr[i][j]; //Update available resources by adding resources allocated to process in the beginning
break;
}
}
}
if (!safe) { //If no process is executed successfully
System.
out.
println("\nThe processes are in unsafe state."); break;
}
if (safe) // If process is executed successfully
System.
out.
println("\nThe process is in safe state.");
System.
out.
println("\nAvailable vector: "); for (i = 0; i < r; i++)
System.
out.
println(avl
[i
]); // Print total available resources after completion of process }
}
}