package ch_1bank;
import java.util.Scanner;
public class bankAlgorithm {
int pro_Num;//进程数量
int re_Num;//资源种类数量
String[] Pname = new String[100]; //进程名称
char[] Rname = new char[100]; //资源种类名称
int[][] Max = new int[100][100];//需要的最大资源数量,用二维数组表示进程所对应的资源数量
int[][] Allocation = new int[100][100];//进程已分配的资源
int[][] Need = new int[100][100];//进程在分配后的所需要的资源量,need = max - allocation
int[] Available = new int[100];//系统中未分配的资源量
int[] Request = new int[100]; //请求资源数量
//假设数据变化
int[] AvaCopy = new int[100];//假设请求后数据剩余量
int[][] AlloCopy = new int[100][100];//假设请求后已分配资源量
int[][] NeedCopy = new int[100][100];//假设请求后需要资源量
//安全算法所需要
int[] Work = new int[100];//虚拟分配
boolean[] Finish = new boolean[100];//判端是否正确
String[] SecPname = new String[100]; //存放安全序列
//对数据进行初始化
public void init() {
boolean flag;
Scanner sc = new Scanner(System.in);
System.out.println("请输入进程的数量:");
pro_Num = sc.nextInt(); //输入进程数量
System.out.println("请输入资源种类的数量:");
re_Num = sc.nextInt(); //输入资源种类的数量
//对进程命名
for(int i = 0; i < pro_Num; i++) {
Pname[i] = "进程" + i;
}
System.out.println("请输入资源的数量:");
//对种类命名并给各类资源进行资源赋值
for(int i = 0; i < re_Num; i++) {
Rname[i] = (char)('A' + i);
System.out.print(Rname[i]+":");
Available[i] = sc.nextInt();
AvaCopy[i] = Available[i];
}
System.out.println("请输入所需最大资源数量:");
//分配需要的最大资源数量
for(int i = 0; i < pro_Num; i++) {
System.out.println(Pname[i]+" ");
for(int j = 0; j < re_Num; j++ ) {
Max[i][j] = sc.nextInt();
}
}
//设置已分配的资源数量以及所需要的资源量
do {
flag = false;
System.out.println("请输入已分配的资源数量:");
for(int i = 0; i < pro_Num; i ++) {
System.out.println(Pname[i]+" ");
for(int j = 0; j < re_Num; j ++) {
Allocation[i][j] = sc.nextInt();
AlloCopy[i][j] = Allocation[i][j];
Need[i][j] = Max[i][j] - Allocation[i][j];
NeedCopy[i][j] = Need[i][j];
if(Allocation[i][j] > Max[i][j] || Allocation[i][j] < 0) {
flag = true;
}
}
}
if(flag) {
System.out.println("设置已分配的资源量存在不符合规范,请重新输入:");
}
}while(flag);
sc.close();
}
//显示初始化后的程序
public void show() {
//显示系统剩余资源
int i,j;
System.out.print("目前系统资源剩余Available:");
for(i = 0; i < re_Num; i++) {
System.out.print(Rname[i]+" " + Available[i]+";");
}
System.out.println();
//显示进程所需要的最大资源Max
System.out.println("进程所需要的最大资源Max");
for(i = 0; i < re_Num; i++) {
System.out.print(" "+Rname[i]);
}
System.out.println();
for(i = 0; i < pro_Num; i ++) {
System.out.print(Pname[i]+": ");
for(j = 0; j < re_Num; j++) {
System.out.print(Max[i][j]+" ");
}
System.out.println();
}
//显示进程已分配的资源Allocation
System.out.println("进程已分配的资源Allocation");
for(i = 0; i < re_Num; i++) {
System.out.print(" "+Rname[i]);
}
System.out.println();
for(i = 0; i < pro_Num; i ++) {
System.out.print(Pname[i]+": ");
for(j = 0; j < re_Num; j++) {
System.out.print(Allocation[i][j]+" ");
}
System.out.println();
}
//显示进程还需要的资源Need
System.out.println("进程还需要的资源Need");
for(i = 0; i < re_Num; i++) {
System.out.print(" "+Rname[i]);
}
System.out.println();
for(i = 0; i < pro_Num; i ++) {
System.out.print(Pname[i]+": ");
for(j = 0; j < re_Num; j++) {
System.out.print(Need[i][j]+" ");
}
System.out.println();
}
}
//安全性算法
public boolean sec() {
int i, j, n, count;
int num = 0;//用于安全序列的存放
//将数组Work 和 Finish 进行初始化
for(i = 0; i < re_Num; i ++) {
Work[i] = AvaCopy[i];
}
for(i = 0; i < pro_Num; i++) {
Finish[i] = false;
}
//查找安全序列
for(i = 0; i < pro_Num; i++) {
count = 0;
for(j = 0; j < re_Num; j++) {
if(Finish[i] == false && NeedCopy[i][j]<=Work[j]) {
count++;
if(count == re_Num) {
for(n = 0; n < re_Num; n++) {
Work[n] = Work[n]+AlloCopy[i][n];
}
Finish[i] = true;
SecPname[num++] = "进程" + i;
i = -1;
}
}
}
}
for(i = 0; i < pro_Num; i++) {
if(Finish[i] == false)
return false;
}
return true;
}
//银行家算法
public void bank() {
int i, j, pNum;
//boolean flag = true;
Scanner sc = new Scanner(System.in);
System.out.println("请输入请求资源的进程编号:");
pNum = sc.nextInt();
System.out.println("进程 "+pNum +"正在请求资源:");
System.out.println("请输入请求资源量:");
for(i = 0; i < re_Num; i++) {//为请求的资源赋值
Request[i] = sc.nextInt();
}
for(i = 0; i < re_Num; i++) {//判断Request[i] 是否大于Need or Available
if(Request[i] > Need[pNum][i] && Request[i] > Available[i]) {
return;
}
}
for(i = 0; i < re_Num; i++) {
AvaCopy[i] -= Request[i];
AlloCopy[pNum][i] += Request[i];
NeedCopy[pNum][i] -= Request[i];
}
if(sec()) {
for(j = 0; j < re_Num; j++) {
Available[j] = AvaCopy[j];
Allocation[pNum][j] = AlloCopy[pNum][j];
Need[pNum][j] = NeedCopy[pNum][j];
}
System.out.println("存在安全序列!已分配");
return;
}
System.out.println("请求资源错误,请重新请求资源!");
sc.close();
return ;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
bankAlgorithm ban = new bankAlgorithm();
char flag;
Scanner sc = new Scanner(System.in);
ban.init();
ban.show();
System.out.println("*************请输入要请求的资源**************");
ban.bank();
System.out.println("是否选择打印安全序列,“是”点击“Y” “否”点击“N”");
flag = sc.next().charAt(0);
switch(flag){
case'Y':
for(int i = 0; i < ban.pro_Num; i++) {
System.out.print(ban.SecPname[i]+" ");
}
case'N':
break;
default:
System.out.println("不要乱按哦!!!");
}
sc.close();
}
}