银行家算法

用java实现银行家算法:

package 银行家算法;

import java.util.Scanner;
public class Banker {
private  static int Available[] = {3,3,2};
private  static  int Max[][] = {{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}} ;
private int Allocation[][] = {{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
private  int Need[][] = {{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
private  String Jincheng[] ={"P0","P1","P2","P3","P4"};
private static int Request[][] = new int[5][3];
private static  int m = Max.length;

private static  int n = Available.length;


public  void  motify(int i)     //请求后修改相应的数值
{
for(int j = 0; j < n; j++)
{

Available[j] -= Request[i][j];
Allocation[i][j] += Request[i][j];
Need[i][j] -= Request[i][j];
}

}
public  void recover(int i)   //请求失败后恢复修改的值
{
for(int j = 0; j < n; j++)
{
Need[i][j] += Request[i][j];
Allocation[i][j] -= Request[i][j];
Available[j] += Request[i][j];
}
}

public  boolean resouce(int i ,int Work[])  //判断可分配的资源是否大于需要的资源
{
int j;
for( j = 0; j < n; j++)
{
if(Need[i][j] > Work[j])
{
break;
}
}
if(j == n)
{
return true;
}
else
{
return false;
}

}
public  boolean Secure( )        //安全性算法
{
int[] Work = new int [5];
int a=1,q;
boolean[] Finish = new boolean [m];
   System.out.println("进程名\tWork\t\tNeed\t\tAllocation\t\tWork+Allocation\t\tFinish");
for (int d = 0; d < n; d++)
{
Work[d] = Available[d];

}
for(int i = 0;i < m; i++)
{
Finish[i] = false;
}

while(a == 1)
{
a=0;
       for(int i = 0; i < m; i++)
       {
        if(Finish[i] == false )
        {
        if( resouce(i,Work) == true)
        {
        System.out.print(Jincheng[i]);
        System.out.print("\t");
        for(int j = 0; j < n; j++)
        {
        System.out.print(Work[j] );
        System.out.print("  " );
        }
        System.out.print("\t");
        for(int j = 0; j < n; j++)
        {
        System.out.print(Need[i][j] );
        System.out.print("  " );
        Work[j] = Work[j] + Allocation[i][j];
        }
        System.out.print("\t");
        for(int j = 0; j < n; j++)
        {
        System.out.print(Allocation[i][j] );
        System.out.print("  " );
       
        }
        System.out.print("\t\t");
        for(int j = 0; j < n; j++)
        {
        System.out.print(Work[j] );
        System.out.print("  " );
       
        }
        System.out.print("\t\t");
        Finish[i] = true;
        System.out.println(Finish[i]);
        a = 1;
        }
       
       
        }
       
       }
}

for( q = 0; q < m; q++)
{
if(Finish[q] == false)
{
break;
}
}
if ( q == m )
{
System.out.println("系统属于安全状态!");

return true;
}
else
{
System.out.println("系统属于不安全状态!");
return false;
}

}

public  void Bank(int Requst[][],int i)  //银行家算法

int j;
this.Request = Requst;
for( j = 0; j < n; j++)
{
if(Requst[i][j] > Need[i][j])
{
System.out.println("所需要的资源已超过他所宣布的最大值!");
break;
}
if(Requst[i][j] > Available[j])
{
System.out.println("尚无足够资源" + Jincheng[i] + "需等待");
break;

}

}

if(j == n)
{
motify(i);
if(Secure() == true)
{

System.out.println("请求成功!");


}
else
{
recover(i);
System.out.println("请求失败!");
}
}
else
{
System.out.println("请求的资源有问题,请重新输入!");
}




}
public static void main(String[] args)
{
System.out.println("请选择是否发出请求:(1为是,0为否)"); 
Scanner se = new Scanner(System.in);
int s = se.nextInt();
Banker b = new Banker();
while(s == 1)
{
System.out.println("请输入需要请求的资源号(1代表P1,2代表P2,3代表P3,4代表P4,0代表P0)");
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();
System.out.println("请依次输入所需资源:");
Scanner sa = new Scanner(System.in);
Scanner sb = new Scanner(System.in);
Scanner sd = new Scanner(System.in);
int i = sa.nextInt();
int j = sb.nextInt();
int c = sd.nextInt();
Request[y][0] = i;
Request[y][1] = j;
Request[y][2] = c;
b.Bank(Request,y);
System.out.println("请选择是否发出请求:(1为是,0为否)"); 
Scanner sf = new Scanner(System.in);
s = sf.nextInt();
}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值