2019327100017朱奕名homework8 Version 0 |
|
👤 Author: by 1114774057qqcom 2021-12-30 04:58:47 |
Banker’s algorithm
Multiple instances.
Each process must a priori claim maximum use.
When a process requests a resource it may have to wait.
When a process gets all its resources it must return them in a finite amount of time.
import java.util.Scanner;
public class Banker {
int available[] = new int[]{3,3,2};
int max[][] = new int[][]{{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int allocation[][] = new int[][]{{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int need[][] = new int[][]{{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
void showData() {
System.out.println("Process number Max All Need ");
System.out.println(" A B C A B C A B C");
for(int i = 0;i<5;i++){
System.out.print(i+" ");
for(int m = 0;m<3;m++) System.out.print(max[i][m]+" ");
for(int m = 0;m<3;m++) System.out.print(allocation[i][m]+" ");
for(int m = 0;m<3;m++) System.out.print(need[i][m]+" ");
System.out.println();
}
}
boolean change(int inRequestNum,int inRequest[])
{
int requestNum = inRequestNum;
int request[] = inRequest;
// for(int i=0;i<3;i++)System.out.println("Before modification available"+available[i]);
if(!(request[0]<=need[requestNum][0]&&request[1]<=need[requestNum][1]&&request[2]<=need[requestNum][2]))
{
//request[0]<=need[requestNum][0]
//request[1]<=need[requestNum][1]
//request[2]<=need[requestNum][2]
System.out.println("The number of resources requested exceeds the maximum required, allocation error");
return false;
}
if((request[0]<=available[0]&&request[1]<=available[1]&&request[2]<=available[2])==false)
{
System.out.println("Not enough resources have been allocated and must wait");
return false;
}
for(int i = 0;i<3;i++)
{
available[i] = available[i]-request[i];
allocation[requestNum][i] = allocation[requestNum][i] + request[i];
need[requestNum][i] = need[requestNum][i] - request[i];
}
// for(int i=0;i<3;i++)System.out.println("After modificationavailable"+available[i]);
boolean flag = checkSafe(available[0],available[1],available[2]);
// System.out.println("After safety check"+flag);
if(flag==true)
{
System.out.println("Able to safely distribute");
return true;
}
else
{
System.out.println("Cannot be safely assigned");
for(int i = 0;i<3;i++)
{
available[i] = available[i]+request[i];
allocation[requestNum][i] = allocation[requestNum][i] - request[i];
need[requestNum][i] = need[requestNum][i] + request[i];
}
return false;
}
}
boolean checkSafe(int a,int b,int c)
{
int work[] = new int[3];
work[0] = a;
work[1] = b;
work[2] = c;
int i=0;
boolean finish[] = new boolean[5];
while(i<5)
{
if(finish[i]==false&&need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[2])
{
System.out.println("The key to successful allocation is"+i);
for(int m = 0;m<3;m++)
work[m] =work[m] + allocation[i][m];
finish[i] = true;
i=0;
}
else
i++;
}
for(i=0;i<5;i++)
{
if(finish[i]==false)
return false;
}
return true;
}
public static void main(String[] args)
{
Banker bank = new Banker();
bank.showData();
int request[] =new int[3];
int requestNum;
String source[] = new String[]{"A","B","C"};
Scanner s = new Scanner(System.in);
String choice = new String();
while(true)
{
System.out.println("Please enter the process number to request (0 -- 4):");
requestNum = s.nextInt();
System.out.print("Please enter the number of resources requested");
for(int i = 0;i<3;i++)
{
System.out.println(source[i]+"Number of resources:");
request[i] = s.nextInt();
}
bank.change(requestNum, request);
System.out.println("Re request allocation (Y / N)");
choice = s.next();
if(choice.equals("n"))
break;
}
}
}