According to the code provided as follows, change the Bankers.java program to be
ID: 3687775 • Letter: A
Question
According to the code provided as follows, change the Bankers.java program to be java class that is callable by another java class. The program currently is run from the command line and accepts user input for the resource parameters.
The revised program needs to:
1. accept the 3 input matrices (allocation, max, available) as parameters passed to the java object.
2. return a boolean result for true if safe (not deadlocked), or false if deadlocked
import java.util.Scanner;
public class Bankers
{
private int need[][],allocate[][],max[][],avail[][],np,nr;
private void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of processes and resources : ");
np=sc.nextInt(); //no. of process
nr=sc.nextInt(); //no. of resources
need=new int[np][nr]; //initializing arrays
max=new int[np][nr];
allocate=new int[np][nr];
avail=new int[1][nr];
System.out.println("Enter allocation matrix -->");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
allocate[i][j]=sc.nextInt(); //allocation matrix
System.out.println("Enter max matrix -->");
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++)
max[i][j]=sc.nextInt(); //max matrix
System.out.println("Enter available matrix -->");
for(int j=0;j<nr;j++)
avail[0][j]=sc.nextInt(); //available matrix
sc.close();
}
private int[][] calc_need()
{
for(int i=0;i<np;i++)
for(int j=0;j<nr;j++) //calculating need matrix
need[i][j]=max[i][j]-allocate[i][j];
return need;
}
private boolean check(int i)
{
//checking if all resources for ith process can be allocated
for(int j=0;j<nr;j++)
if(avail[0][j]<need[i][j])
return false;
return true;
}
public void isSafe()
{
input();
calc_need();
boolean done[]=new boolean[np];
int j=0;
while(j<np)
{ //until all process allocated
boolean allocated=false;
for(int i=0;i<np;i++)
if(!done[i] && check(i))
{ //trying to allocate
for(int k=0;k<nr;k++)
avail[0][k]=avail[0][k]-need[i][k]+max[i][k];
System.out.println("Allocated process : "+i);
allocated=done[i]=true;
j++;
}
if(!allocated) break; //if no allocation
}
if(j==np) //if all processes are allocated
System.out.println(" Safely allocated");
else
System.out.println("All proceess cant be allocated safely");
}
public static void main(String[] args)
{
new Bankers().isSafe();
}
}
Explanation / Answer
Answer for Question:
This below java code will implemented for bankers algorthim
import java.io.*;
class Bankers
{
public static void main(String args[])throws IOException
{
int all[][]=new int[10][10]; //to store allocation matrix
int max[][]=new int[10][10]; //to store maximum allocation
int need[][]=new int[10][10]; //need is stored in it
int avail[]=new int[10]; //availablity of process allocation
int ord[]=new int[10]; //stores order of process
int flags[]=new int[10]; //flag states that process executed
int i,j,pr,res,count=0,flag=0;
DataInputStream dis=new DataInputStream(System.in);
System.out.println(“Enter no of processes : “); //input for no of processes
pr=Integer.parseInt(dis.readLine());
System.out.println(“Enter no. of resources : “);
res=Integer.parseInt(dis.readLine()); //input for no. of resources
System.out.println(“Enter allocation matrix : “);
for(i=0;i<pr;i++)
{
for(j=0;j<res;j++)
{
all[i][j]=Integer.parseInt(dis.readLine()); //allocation matrix input
}
}
System.out.println(“Enter max matrix : “);
for(i=0;i<pr;i++)
{
for(j=0;j<res;j++)
{
max[i][j]=Integer.parseInt(dis.readLine()); //max matrix input
need[i][j]=max[i][j]-all[i][j]; //calculate need matrix
}
}
System.out.println(“Enter avail matrix : “);
for(j=0;j<res;j++)
{
avail[j]=Integer.parseInt(dis.readLine()); //availablity matrix
}
for(j=0;j<pr;j++)
{
flags[j]=-1;
System.out.println(“Busy”); //each process flag is made -1 i.e. not inserted
}
System.out.println(“Need matrix : “);
for(i=0;i<pr;i++)
{
for(j=0;j<res;j++)
{
System.out.print(need[i][j]+” “);
}
System.out.println();
}
int t=0;
while(count<pr)
{
for(i=0;i<pr;i++)
{
if(flags[i]==-1) //if flags[i]==-1 means process is not inserterd
{
System.out.print(“i =”+i+” “);
flag=0;
for(j=0;j<res;j++)
{
if(need[i][j]>avail[j])
{
flag=1; //check whether each resource is satisfying given condition
}
}
System.out.print(“flag =”+flag+” “);
if(flag==0)
{
for(j=0;j<res;j++)
{
avail[j]=avail[j]+all[i][j]; //after process inserted avail increamented
}
count++;
ord[t++]=i; //executed process’s order is stored
flags[i]=1; //flags[i]=1 marks process executed
}
System.out.print(“c =”+count+” “);
for(j=0;j<res;j++)
System.out.print(avail[j]);
System.out.println();
}
}
}
System.out.println(“Order”);
for(i=0;i<pr;i++)
{
System.out.print(ord[i]+” “); //process order is displayed
}
}
}
Output:
Enter no of processes :
5
Enter no. of resources :
4
Enter allocation matrix :
0
0
1
2
1
0
0
0
1
3
5
4
0
6
3
2
0
0
1
4
Enter max matrix :
0
0
1
2
1
7
5
0
2
3
5
6
0
6
5
2
0
6
5
6
Enter avail matrix :
1
5
2
0
Busy
Busy
Busy
Busy
Busy
Need matrix :
0 0 0 0
0 7 5 0
1 0 0 2
0 0 2 0
0 6 4 2
i =0 flag =0 c =1 1532
i =1 flag =1 c =1 1532
i =2 flag =0 c =2 2886
i =3 flag =0 c =3 214118
i =4 flag =0 c =4 2141212
i =1 flag =0 c =5 3141212
Order
0 2 3 4 1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.