匆忙写了这个算法代码,没有修正,有很多冗余,主要是check()算法有点价值。
流程图不再赘述,网上很多已经很完善了。
package 银行家算法;
public class ShowData {
void Show(int Max[][],int Allocation[][],int Need[][],int Available[]){
System.out.println(“此时系统资源情况为:”);
System.out.println(“最大需求资源Max[][]:”);
for(int i=0;i<Max.length;i++){
System.out.print(“P”+i+”:”);
System.out.print(“[“);
for(int j=0;j<Max[i].length;j++){
System.out.print(Max[i][j]+”,”);
}
System.out.print(“]”);
System.out.print(“\n”);
}
System.out.println(“最大已分配资源Allocation[][]:”);
for(int i=0;i<Max.length;i++){
System.out.print(“P”+i+”:”);
System.out.print(“[“);
for(int j=0;j<Max[i].length;j++){
System.out.print(Allocation[i][j]+”,”);
}
System.out.print(“]”);
System.out.print(“\n”);
}
System.out.println(“还需求资源Need[][]:”);
for(int i=0;i<Max.length;i++){
System.out.print(“P”+i+”:”);
System.out.print(“[“);
for(int j=0;j<Max[i].length;j++){
System.out.print(Need[i][j]+”,”);
}
System.out.print(“]”);
System.out.print(“\n”);
}
System.out.println(“可以用资源Available[]:”);
System.out.print(“[“);
for(int i=0;i<Available.length;i++){
System.out.print(Available[i]+”,”);
}
System.out.print(“]”);
System.out.print(“\n”);
}
}
package 银行家算法;
public class Check {
int check(int Allocation[][],int Need[][],int Available[]){
int flags;
int flag=0;
Boolean Finish[]=new Boolean[Allocation.length];
for(int i=0;i<Allocation.length;i++){
Finish[i]=false;
}
int Work[]=new int[Allocation[0].length];
for(int i=0;i<Allocation[0].length;i++){
Work[i]=Available[i];
}
int queue[]=new int[Allocation.length]; //用于存放安全队列
int l=0;
int j=0;
int i;
while(j<Allocation.length){
for(i=0;i<Allocation.length;i++){
if(Finish[i]){
i++;
continue;
}
else if(Compare(Need, Work, i)){
System.out.println(“i=”+i);
System.out.println(Compare(Need, Work, i));
Finish[i]=true;
for(int k=0;k<Allocation[i].length;k++)
Work[k]+=Allocation[i][k];
for(int k=0;k<Allocation[i].length;k++)
System.out.println(Work[k]);
queue[l]=i;
l++;
i=0;
break;
}
}
j++;
}
for(int m=0;m<Allocation.length;m++){
if(Finish[m]==true){
flag++;
}
}
if (flag==Allocation.length)
{
System.out.println(“资源申请成功,安全队列为:”);
for(int q=0;q<Allocation.length;q++){
System.out.println(queue[q]);
}
flags=1;
}
else{
System.out.println(“系统不安全,资源申请失败”);
flags=0;
}
return flags;
}
Boolean Compare(int Need[][],int Work[],int thread){
int flags=0;
for(int i=0;i<Need[0].length;i++ ){
if(Need[thread][i]<=Work[i]){
flags++;
}
}
if(flags==Need[0].length){
return true;
}
else
return false;
}
}
package 银行家算法;
import java.util.Scanner;
public class Bank {
Scanner in=new Scanner(System.in);
void bank(int Max[][],int Allocation[][],int Need[][],int Available[],int Num){
while(true){
//int flags=0;
int flags1=0;
int flags2=0;
System.out.println(“请输入需申请资源的进程号:”);
int n=in.nextInt();
int Request[]=new int[Allocation[0].length];
if(n>=Allocation.length){
System.out.println(“该进程不存在,请重新输入!”);
n=in.nextInt();
}
System.out.println(“请输入要申请的资源量Requset[]”);
for(int i=0;i<Allocation[0].length;i++){
Request[i]=in.nextInt();
}
for(int i=0;i<Allocation[0].length;i++){
if(Request[i]<=Need[n-1][i]){
flags1++;
}
}
if(flags1!=Allocation[0].length){
System.out.println(“申请的资源数大于系统所需求的,申请不合理,请重新输:!”);
System.out.println(“请输入要申请的资源量Requset[]”);
for(int i=0;i<Allocation.length;i++){
Request[i]=in.nextInt();
}
}
for(int i=0;i<Allocation[0].length;i++){
if(Request[i]<=Available[i]){
flags2++;
}
}
if(flags2!=Allocation[0].length){
System.out.println(“申请的资源数大于系统可用,申请不合理,请重新输:!”);
System.out.println(“请输入要申请的资源量Requset[]”);
for(int i=0;i<Allocation.length;i++){
Request[i]=in.nextInt();
}
}
//预分配
for(int i=0;i<Allocation[0].length;i++){
Available[i]=Available[i]-Request[i];
Allocation[n-1][i]+=Request[i];
Need[n-1][i]-=Request[i];
}
Check ck=new Check();
//System.out.println(“n=”+n);
int flags=ck.check(Allocation, Need, Available);
//System.out.println(flags);
if(flags==1){
ShowData data=new ShowData();
data.Show(Max, Allocation, Need, Available);
}
else{
System.out.println(“资源重置:”);
RstorData(n-1, Available, Allocation, Need, Request,Max);
}
System.out.println(“是否继续申请资源?继续按1,退出按0!”);
int next=in.nextInt();
if(next==0)
return;
}
}
void RstorData(int thread,int[] available,int Allocation[][],int Need[][],int Request[],int Max[][]){ //恢复现场
for (int j=0;j<Need[0].length;j++){
available[j]+=Request[j];
Need[thread][j]+=Request[j];
Allocation[thread][j]-=Request[j];
}
ShowData datas=new ShowData();
datas.Show(Max, Allocation, Need, available);
}
}
package 银行家算法;
import java.util.Scanner;
public class Main {
public static void main(String []args){
//欲构造的函数:ShowData();Bank();Check();
//初始化数据
Scanner in=new Scanner(System.in);
int Num;
int category;
System.out.println(“请输入进程数:”);
Num=in.nextInt();
System.out.println(“请输入资源种类数:”);
category=in.nextInt();
int Max[][]=new int[Num][category];
int Allocation[][]=new int[Num][category];
int Available[]=new int[category];
int Need[][]=new int[Num][category];
//手动输入各类资源状况
System.out.println(“请依次输入Max[][]资源:”);
for(int i=0;i<Num;i++){
for(int j=0;j<category;j++){
Max[i][j]=in.nextInt();
}
}
System.out.println(“请依次输入Allocation[][]资源:”);
for(int i=0;i<Num;i++){
for(int j=0;j<category;j++){
Allocation[i][j]=in.nextInt();
}
}
System.out.println(“请依次输入Available[]的资源:”);
for(int i=0;i<category;i++){
Available[i]=in.nextInt();
}
for(int i=0;i<Num;i++){
for(int j=0;j<category;j++){
Need[i][j]=Max[i][j]-Allocation[i][j];
}
}
ShowData data=new ShowData();
data.Show(Max, Allocation, Need, Available);
Bank bk=new Bank();
bk.bank(Max, Allocation, Need, Available, Num);
}
}