銀行家演算法程式碼(純手輸入)
匆忙寫了這個演算法程式碼,沒有修正,有很多冗餘,主要是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);
}
}