//BANKER’S ALGORITHM
import java.util.*;
class Bankersalgorithm
{
void disp(int m,int n,int alloc[][],int max[][],int avble[],int need[][])
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
int i,j;
System.out.println("\n\tALLOCATION\tMAX\tNEED\tAVAILABLE");
System.out.println("\n\t\t");
for(i=0;i<4;i++)
{
for(j=0;j<m;j++)
System.out.print(" "+res[j]);
System.out.print("\t\t");
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
System.out.print(" "+alloc[i][j]);
System.out.print("\t\t");
for(j=0;j<m;j++)
System.out.print(" "+max[i][j]);
System.out.print("\t\t");
for(j=0;j<m;j++)
System.out.print(" "+need[i][j]);
System.out.print("\t\t");
if(i==0)
{
for(j=0;j<m;j++)
System.out.print(" "+avble[j]);
}
}
}
void safesq(int m,int n,int alloc[][],int avble[],int need[][])
{
int i,j,k=0,l,flag=0,flag1=0;
int work[]=new int[5];
int work1[]=new int[5];
int fin[]=new int[5];
int safesq[]=new int[6];
for(i=0;i<m;i++)
work[i]=avble[i];
for(i=0;i<n;i++)
fin[i]=0;
for(l=0;l<n;l++)
{
for(i=0;i<n;i++)
{
flag1=0;
if(fin[i]==0)
{
for(j=0; j<m; j++)
{
if(need[i][j] > work[j])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(j=0;j<m;j++)
work[j]=work[j]+alloc[i][j];
fin[i]=1;
safesq[k]=i;
k++;
}
}
}
}
for(i=0;i<n;i++)
{
if(fin[i]==0)
{
System.out.println("\n\tFOR THE GIVEN REQUIREMENT THE SYSTEM IS");
System.out.println(" NOT IN A SAFE STATE.\n");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("\n\tTHE SAFE SEQUENCE IS:\t");
for(i=0;i<n;i++)
System.out.println(" "+safesq[i]);
}
}
void resreq(int m,int n,int alloc[][],int avble[],int need[][])
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
Scanner sc1=new Scanner(System.in);
int i,j,num,flag=0,flag1=0;
int alloc1[][]=new int[5][5];
int avble1[]=new int[5];
int req[]=new int[5];
for(i=0;i<n;i++)
for(j=0;j<m;j++)
alloc1[i][j] = alloc[i][j];
for(j=0;j<m;j++)
avble1[j]=avble[j];
System.out.println("\n\tENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES:\t");
num=sc1.nextInt();
System.out.println("\n\tENTER THE INSTANCE REQUIREMENT OF PROCESS \n"+num);
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE :\t"+res[i]);
req[i]=sc1.nextInt();
}
for(i=0;i<m;i++)
{
if(req[i]>need[num][i])
{
flag=1;
break;
}
}
if(flag==0)
{
for(i=0;i<m;i++)
{
if(req[i] > avble1[i])
{
flag1=1;
break;
}
}
if(flag1==0)
{
for(i=0;i<m;i++)
{
avble1[i]=avble1[i]-req[i];
alloc1[num][i]=alloc1[num][i]+req[i];
need[num][i]=need[num][i]-req[i];
}
safesq(m,n,alloc1,avble1,need);
}
else
{
System.out.println("\n\tTHE PROCESS P%d HAS TO WAIT AS RESOURCES ");
System.out.println("ARE NOT AVAILABLE.\n");
}
}
else
System.out.println("\n\t REQUIREMENT EXCEEDS MAXIMUM CLAIM.\n");
}
}
class Result
{
public static void main(String args[])
{
char res[]={'A','B','C','D','E','F','G','H','I','J'};
int i,j,ch=0,n,m;
int work1[]=new int[5];
int avble[]=new int[5];
int max[][]=new int[5][5];
int alloc[][]=new int[5][5];
int need[][]=new int[5][5];
Bankersalgorithm b=new Bankersalgorithm();
Scanner sc = (new Scanner(System.in));
System.out.println("\nENTER THE NUMBER OF PROCESSES : \t");
n=sc.nextInt();
System.out.println("\nENTER THE NUMBER OF RESOURCES : \t");
m=sc.nextInt();
System.out.println("\nENTER THE MAXIMUM INSTANCES OF EACH RESOURCE\n");
for(i=0;i<m;i++)
{
System.out.println("\n\tRESOURCE :\t"+res[i]);
avble[i]=sc.nextInt();
}
System.out.println("\nENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
max[i][j]=sc.nextInt();
}
}
System.out.println("\nENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO");
System.out.println(" A PROCESS.\n");
for(i=0;i<n;i++)
{
System.out.println("\n\tFOR PROCESS \n"+i);
for(j=0;j<m;j++)
{
System.out.println("\n\tRESOURCE : \t"+res[j]);
alloc[i][j]=sc.nextInt();
}
}
for(i=0;i<m;i++)
{
work1[i]=0;
for(j=0;j<n;j++)
work1[i]+=alloc[j][i];
avble[i]=avble[i] - work1[i];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
need[i][j]=max[i][j]-alloc[i][j];
}
}
while(ch<5)
{
System.out.println("\n\n\tMENU:\n\t1]DISPLAY DATA\n\t2]GENERATE SAFE SEQUENCE");
System.out.println("\n\t3]RESOURCE REQUEST\n\t4]EXIT\n\tENTER YOUR CHOICE:\t");
ch=sc.nextInt();
switch(ch)
{
case 1:
b.disp(m,n,alloc,max,avble,need);
break;
case 2:
b.safesq(m,n,alloc,avble,need);
break;
case 3:
b.resreq(m,n,alloc,avble,need);
break;
case 4:
break;
default:
System.out.println("\n\tINVALID CHOICE ENTERED.\n");
}
}
System.out.println("\n\t\tThank You");
}
}
SAMPLE OUTPUT:
ENTER THE NUMBER OF PROCESSES : 3
ENTER THE NUMBER OF RESOURCES : 3
ENTER THE MAXIMUM INSTANCES OF EACH RESOURCE
RESOURCE : A 10
RESOURCE : B 5
RESOURCE : C 7
ENTER THE MAXIMUM DEMAND OF EACH PROCESS FOR A RESOURCE
FOR PROCESS 0
RESOURCE : A 7
RESOURCE : B 5
RESOURCE : C 3
FOR PROCESS 1
RESOURCE : A 3
RESOURCE : B 2
RESOURCE : C 2
FOR PROCESS 2
RESOURCE : A 9
RESOURCE : B 0
RESOURCE : C 2
ENTER THE MAX NO. OF INSTANCES OF A RESOURCE ALLOCATED TO A PROCESS.
FOR PROCESS 0
RESOURCE : A 0
RESOURCE : B 1
RESOURCE : C 0
FOR PROCESS 1
RESOURCE : A 2
RESOURCE : B 0
RESOURCE : C 0
FOR PROCESS 2
RESOURCE : A 3
RESOURCE : B 0
RESOURCE : C 2
MENU:
1]DISPLAY DATA
2]GENERATE SAFE SEQUENCE
3]RESOURCE REQUEST
4]EXIT
ENTER YOUR CHOICE: 1
ALLOCATION MAX NEED AVAILABLE
A B C A B C A B C A B C
0 1 0 7 5 3 7 4 3 5 4 5
2 0 0 3 2 2 1 2 2
3 0 2 9 0 2 6 0 0
MENU:
1]DISPLAY DATA
2]GENERATE SAFE SEQUENCE
3]RESOURCE REQUEST
4]EXIT
ENTER YOUR CHOICE: 2
THE SAFE SEQUENCE IS:
1
2
0
MENU:
1]DISPLAY DATA
2]GENERATE SAFE SEQUENCE
3]RESOURCE REQUEST
4]EXIT
ENTER YOUR CHOICE: 3
ENTER THE PROCESS NO. THAT REQUIRES EXTRA RESOURCES: 2
ENTER THE INSTANCE REQUIREMENT OF PROCESS 2
RESOURCE : A 3
RESOURCE : B 0
RESOURCE : C 1
REQUIREMENT EXCEEDS MAXIMUM CLAIM.