PROGRAM TO IMPLEMENT BANKERS ALGORITHM

import java.io.*;
class banker
{
public static void main(String args[])throws IOException
{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
int m,n,i,j,count=0;
System.out.println("Enter the number of process maximum(4)...");
m=Integer.parseInt(b.readLine());
System.out.println("Enter the number of Resources maximum(3)...");
n=Integer.parseInt(b.readLine());
int claim[][]=new int[m][n];
int allocation[][]=new int[m][n];
int need[][]=new int[m][n];
int resource[]=new int[n];
int available[]=new int[n];

System.out.println("Enter the resource matrix");
for(j=0;j resource[j]=Integer.parseInt(b.readLine());

System.out.println("Enter the available matrix");
for(j=0;j available[j]=Integer.parseInt(b.readLine());

for(i=0;i for(j=0;j { System.out.println("Enter the claim of process "+i+" for resource "+j);
claim[i][j]=Integer.parseInt(b.readLine());
}

for(i=0;i for(j=0;j { System.out.println("Enter the allocation of process "+i+" for resource "+j);
allocation[i][j]=Integer.parseInt(b.readLine());
}

for(i=0;i for(j=0;j { need[i][j]=claim[i][j]-allocation[i][j]; }

do
{
for(i=0;i {
if(need[i][0]==0&&need[i][1]==0&&need[i][2]==0)
continue;

if(need[i][0]<=available[0]&&need[i][1]<=available[1]&&need[i][2]<=available[2])
{ count++;
need[i][0]=need[i][1]=need[i][2]=0;
available[0]=available[0]+allocation[i][0];
available[1]=available[1]+allocation[i][1];
available[2]=available[2]+allocation[i][2];

System.out.println("..Need matrix...");
for(i=0;i { for(j=0;j {System.out.print(need[i][j]+" ");}
System.out.println();
}
System.out.println("..available matrix...");

for(j=0;j System.out.println(available[j]+" ");
}

}
}while(count<4 br="">

}
}

0 comments :