/*
* slj
* 2016-12-13
*/
import java.util.Scanner;
public class simpleBanker {
//进程数
private int number = 5;
//资源数
private int number2 = 3;
//可得到的资源
private int available[] = new int[]{3,3,2};
//进程名字
private String[] name = new String[]{"P1","P2","P3","P4","P5","P6"};
//每个进程最大资源数
private int max[][] = new int[][]{{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//每个进程目前拥有的资源数
private int allocation[][] = new int[][]{{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//每个进程需要的资源数
private int need[][] = new int[number][number2];
//初始输入
private void showBegin(){
System.out.println("初始数据:");
System.out.println("进程号\tMax\t Allocation\tAvailabel\t");
System.out.println("\tA B C\t A B C\tA B C");
for(int i = 0;i<number;i++) {
System.out.print(name[i] + "\t");
for(int m = 0;m<3;m++) System.out.print(max[i][m]+" ");
System.out.print(" ");
for(int m = 0;m<3;m++) System.out.print(allocation[i][m]+" ");
System.out.print("\t");
if(i==0){
for(int m = 0;m<3;m++) System.out.print(available[m]+" ");
System.out.print("\t");
}
System.out.println();
}
//需要资源情况
for(int i= 0;i<number;i++){
for(int j=0;j<number2;j++){
need[i][j] = max[i][j] - allocation[i][j];
}
}
System.out.println("每个进程请求资源:");
System.out.println("进程号\tNeed");
System.out.println("\tA B C");
for(int i = 0;i<number;i++) {
System.out.print(name[i] + "\t");
for(int m = 0;m<3;m++) System.out.print(need[i][m]+" ");
System.out.println();
}
}
//寻找所有安全序列
int sum = 0;
int tmp = 0;
String str = "";
int[] work = available;
boolean[] flag = new boolean[number];
private void find(){
if(tmp == number){
System.out.println(str);
sum++;
return ;
}
else{
for(int i=0;i<number;i++){
if((flag[i] == false)&&(need[i][0]<=work[0])&&(need[i][1]<=work[1])&&(need[i][2]<=work[2])){
work[0]+=allocation[i][0];
work[1]+=allocation[i][1];
work[2]+=allocation[i][2];
flag[i]=true;
tmp++;
str+=name[i]+" ";
find();
str=str.substring(0,str.length()-3);
tmp--;
flag[i]=false;
work[0]-=allocation[i][0];
work[1]-=allocation[i][1];
work[2]-=allocation[i][2];
}
}
}
}
//资源请求
boolean change(int inRequestNum,int inRequest[]){
int requestNum = inRequestNum;
int request[] = inRequest;
if(!(request[0]<=need[requestNum][0]&&request[1]<=need[requestNum][1]&&request[2]<=need[requestNum][2]))
{
System.out.println("请求的资源数超过了所需要的最大值,分配错误");
return false;
}
if((request[0]<=available[0]&&request[1]<=available[1]&&request[2]<=available[2])==false){
System.out.println("尚无足够资源分配,必须等待");
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];
}
sum = 0;
tmp = 0;
str = "";
work = available;
flag = new boolean[number];
find();//进行安全性检查并返回是否安全
// System.out.println("安全性检查后"+flag);
if(sum > 0) {
System.out.println("能够安全分配");
return true;
}
else//不能通过安全性检查 恢复到未分配前的数据
{
System.out.println("不能够安全分配");
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;
}
}
public void Test(){
showBegin();
//寻找所以安全序列
System.out.println("有如下安全序列:");
sum=0;
find();
System.out.println("一共有" + sum + "个安全序列。");
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("请输入要请求的进程号(0--4):");
requestNum = s.nextInt();
System.out.print("请输入请求的资源数目");
for(int i = 0;i<3;i++){
System.out.println(source[i]+"资源的数目:");
request[i] = s.nextInt();
}
change(requestNum, request);
System.out.println("是否再请求分配(y/n)");
choice = s.next();
if(choice.equals("n"))
break;
}
return ;
}
}
网友评论