import java.util.HashMap;
import java.util.Scanner;
class Banker{
private int amount;//可用资源种类的个数
private int m;//进程的个数
private boolean flag;
String name[]=new String[100];//资源名称
int Available[]=new int[100];//当前资源剩余向量
int atempt[]=new int[100];
int MAX[][]=new int[100][100];//最大需求矩阵
int Allocation[][]=new int[100][100];//已分配资源矩阵
int Need[][]=new int[100][100];//当前还需资源数量矩阵
int Request[]=new int[100];//请求资源向量
public void init(){
System.out.println("系统可用资源种类的个数:");
Scanner in=new Scanner(System.in);
amount=in.nextInt();
System.out.println("请输入可用资源名称和相应个数:(格式为:名称 个数)");
for(int i=0;i<amount;i++) {
String name1=in.next();
int n=in.nextInt();
name[i]=name1;
Available[i]=n;
}
//输入最大需求矩阵
System.out.println("请输入当前进程的数量:");
m=in.nextInt();
System.out.println("请输入各进程的最大需求矩阵:[MAX]");
do {
flag=false;
for(int i=0;i<m;i++) {
for(int j=0;j<amount;j++) {
MAX[i][j]=in.nextInt();
if(MAX[i][j]>Available[j]) {
flag=true;
}
}
}
if(flag) {
System.out.println("资源最大需求量大于系统资源最大数量,输入错误,请重新输入!");
}
}while(flag);
//输入已分配资源矩阵
System.out.println("请输入已分配资源矩阵:[Allocation]");
do {
flag=false;
for(int i=0;i<m;i++) {
for(int j=0;j<amount;j++) {
Allocation[i][j]=in.nextInt();
if(Allocation[i][j]>Available[j]) {
flag=true;
}
atempt[j]+=Allocation[i][j];
Need[i][j]=MAX[i][j]-Allocation[i][j];
}
}
if(flag) {
System.out.println("已分配资源数量大于系统资源最大数量,输入错误,请重新输入!");
}
}while(flag);
for(int i=0;i<amount;i++) {
Available[i]-=atempt[i];
}
}
public void show() {
showAvailable();
System.out.println();
System.out.println("当前系统资源分配情况如下:");
showMAX();
showAllocation();
showNeed();
}
public void showAvailable() {
System.out.println("当前资源剩余向量:Available[]");
for(int i=0;i<amount;i++) {
System.out.print(name[i]+"\t");
}
System.out.println();
for(int i=0;i<amount;i++) {
System.out.print(Available[i]+"\t");
}
}
public void showMAX() {
System.out.println("[MAX]");
System.out.print("进程名 \t");
for(int i=0;i<amount;i++) {
System.out.print(name[i]+" \t");
}
System.out.println();
for(int i=0;i<m;i++) {
System.out.print("P"+i);
for(int j=0;j<amount;j++) {
System.out.print(" \t"+MAX[i][j]);
}
System.out.println();
}
}
public void showAllocation() {
System.out.println("[Allocation]");
System.out.print("进程名 \t");
for(int i=0;i<amount;i++) {
System.out.print(name[i]+" \t");
}
System.out.println();
for(int i=0;i<m;i++) {
System.out.print("P"+i);
for(int j=0;j<amount;j++) {
System.out.print(" \t"+Allocation[i][j]);
}
System.out.println();
}
}
public void showNeed() {
System.out.println("[Need]");
System.out.print("进程名 \t");
for(int i=0;i<amount;i++) {
System.out.print(name[i]+" \t");
}
System.out.println();
for(int i=0;i<m;i++) {
System.out.print("P"+i);
for(int j=0;j<amount;j++) {
System.out.print(" \t"+Need[i][j]);
}
System.out.println();
}
}
public void tryTest() {
}
public void isSafe() {
int w=0;
int flag=0;
int t=0;
int safe[]=new int[m];
int t1[]=new int[amount];
int t0[]=new int[m];
for(int i=0;i<amount;i++) {
t1[i]=Available[i];
}
for(int i=0;i<m;i++) {
safe[i]=1;
}
while(w<m) {
MAX_LOOP:
for(int i=0;i<m;i++) {
if(safe[i]==1) {
for(int j=0;j<amount;j++) {
if(Need[i][j]<=t1[j]) {}
else {
continue MAX_LOOP;
}
}
safe[i]=0;
t0[t++]=i;
for(int k=0;k<amount;k++) {
t1[k]+=Allocation[i][k];
}
}
}
w++;
}
for(int i=0;i<m;i++) {
if(safe[i]==0) {flag++;}
else {
System.out.println("此时刻系统不安全!");
System.exit(0);
}
}
if(flag==m) {
System.out.println("此时刻系统安全!存在一个安全序列:");
for(int i=0;i<m;i++) {
System.out.print("p"+t0[i]+" ");
}
}
}
public void applyRequest() {
Scanner in=new Scanner(System.in);
System.out.println("发出资源请求的进程是哪一个:(输入格式:0,1,2....)");
int pro=in.nextInt();
System.out.println("请输入资源请求向量:(输入格式例如:1 2 3 4)");
for(int i=0;i<amount;i++) {
Request[i]=in.nextInt();
}
for(int i=0;i<amount;i++) {
if(Request[i]<=Available[i]) {}
else {
System.out.println("请求资源个数大于剩余资源个数,不能够实施资源分配!");
System.exit(0);
}
}
for(int i=0;i<amount;i++) {
if(Request[i]<=Need[pro][i]) {}
else {
System.out.println("请求资源个数大于需求资源个数,不能够实施资源分配!");
System.exit(0);
}
}
for(int i=0;i<amount;i++) {
Available[i]-=Request[i];
Need[pro][i]-=Request[i];
Allocation[pro][i]+=Request[i];
}
}
}
public class Main{
public static void main(String args[]) {
Scanner in=new Scanner(System.in);
System.out.println("*************************银行家算法*************************");
Banker bank=new Banker();
bank.init();
bank.show();
bank.isSafe();
System.out.println();
System.out.println("输入1:请求分配 输入0:退出程序");
int choice=in.nextInt();
switch(choice) {
case 0:
System.exit(0);
break;
case 1:
bank.applyRequest();
bank.show();
bank.isSafe();
break;
default:
System.out.println("请正确选择!");
}
}
}
运行结果:



本文介绍了一个基于Java实现的银行家算法示例程序。该程序通过用户输入资源和进程信息来模拟银行家算法的过程,包括资源分配、安全状态检查及资源请求处理等关键步骤。
576

被折叠的 条评论
为什么被折叠?



