Bankers Algorithm in Java
Bankers Algorithm in Java
import java.util.*;
class GFG
{
// Number of processes
static int P = 5;
// Number of resources
static int R = 4;
// Finding the need of each process
static void calculateNeed(int need[][], int maxm[][],
int allot[][])
{
// Calculating Need of each P
for (int i = 0 ; i < P ; i++)
for (int j = 0 ; j < R ; j++)
// Need= maxm -
// allocated
need[i][j] = maxm[i][j] - allot[i][j];
}
// Finding safe state or not
static boolean isSafe(int processes[], int avail[], int maxm[][],
int allot[][])
{
int [][]need = new int[P][R];
// Need matrix
calculateNeed(need, maxm, allot);
// Mark all processes as infinish
boolean []finish = new boolean[P];
// Safe sequence
int []safeSeq = new int[P];
// Make a copy of available resources
int []work = new int[R];
for (int i = 0; i < R ; i++)
work[i] = avail[i];
// While all processes are not finished
// or system is not in safe state.
int count = 0;
while (count < P)
{