银行家算法的核心机制:
针对3类资源5个进程的情况,设计相应的数据结构,分别表示每个进程占用各类资源的情况;编程实现安全性算法函数,
编制主函数,动态输入资源的占用情况,进程的资源申请,调用安全性函数,实现银行家算法;测试:输入可分配和不可分配的请求,测试系统的正确性。
import java.util.*;
/*此类主要功能如下
* 1.初始化资源
* 2.进行死锁避免
* 3.检索进程请求资源是否可行
* */
class TestBanker{
int m;
int n;
int[][] max;
int[][] max1;
int[][] allocation;
int[][] allocation1;
int[][] need;
int[][]need1;
int[] available;
int[] availablebak;
public TestBanker(){
Scanner s = new Scanner(System.in);
System.out.print("请依次输入系统中的进程数");
m = s.nextInt();
System.out.print("请依次输入系统中的资源类型数");
n = s.nextInt();
max =new int[m][n];
max1 = new int[m][n];
allocation = new int[m][n];
allocation1 = new int[m][n];
need = new int[m][n];
need1 = new int[m][n];
available = new int[n];
availablebak = new int[n];
for(int i=0;i<max.length;i++){//初始化向量MAX、ALLOCATION、NEED、AVAILABLE
System.out.print("请依次输入第" + i + "进程所需的最大max的各资源数");
for(int j=0;j<max[i].length;j++){
max[i][j] = s.nextInt();
max1[i][j] = max[i][j]; }
}
for(int i=0;i<allocation.length;i++){
System.out.print("请依次输入第" + i + "进程中已分配(allocation)资源的数量");
for(int j=0;j<allocation[i].length;j++){
allocation[i][j] = s.nextInt();
allocation1[i][j] = allocation[i][j];
}
}
System.out.println("自动计算得出各进程需要的资源数量");
for(int i=0;i<need.length;i++){
//System.out.println("请一次输入第"+i+"进程尚需的(need)资源数量");
for(int j=0;j<need[i].length;j++){
need[i][j] = max[i][j] - allocation[i][j];
need1[i][j] = need[i][j];
}
}
for(int i=0;i<available.length;i++){
System.out.print("请输入系统中第" + i + "种可利用的资源数量");
available[i] = s.nextInt();
availablebak[i] = available[i];
}
System.out.println("初始化结果=============");
init();
}
public void init(){//输出分配资源的状态
System.out.println(" MAX ALLOCATION NEED AVAILABLE");
for(int i=0;i<m;i++){
System.out.print("P" + i + ": ");
for(int j=0;j<n;j++){
if(max[i][j]>9){//如果是两位数控制格式在数字前少输出一个" "。
System.out.print(max[i][j] + " ");
}else{
System.out.print(" " + max[i][j] + " ");
}
}
System.out.print(" | ");
for(int j=0;j<n;j++){
if(allocation[i][j]>9){
System.out.print(allocation[i][j] + " ");
}else{
System.out.print(" " + allocation[i][j] + " "); }
}
System.out.print(" | ");
for(int j=0;j<n;j++){
if(need[i][j]>9){
System.out.print(need[i][j] + " ");
}else{
System.out.print(" " + need[i][j] + " ");
}
}
if(i==0){
System.out.print(" | ");
for(int j=0;j<n;j++){
if(available[j]>9){
System.out.print(available[j] + " ");
}else{
System.out.print(" " + available[j] + " ");
}
}
}
System.out.println();
}
System.out.println();
}
public void securityMethods(){//死锁检测
int[] security = new int[m];
boolean[] finish = new boolean[m];
int[] tar = new int[n];
int count = 0;
int num1 = m+1;//计数器,每循环一遍所有进程就自减1
int num2 = m;//计数器每遇到一个被满足的进程就自减1
while(num1>0){//如果num1==0则说明依次循环下来没有能够满足的进程因此终止
for(int i=0;i<m;i++){
if(finish[i]==false){//只有没有被满足的进程才可以进入内层循环
finish[i] = true;
for(int j=0;j<n;j++){
tar[j] = available[j] - need[i][j];
if(tar[j]<0){
finish[i] = false;
}
}
if(finish[i]==true){
for(int k=0;k<n;k++){
available[k] = available[k] + allocation[i][k];
}
security[count] = i;//记录以满足的进程号
count++;
num2--; }
}
}
num1--;
while((num2==0)&&(num1>0)){
System.out.println("可以产生新的安全序列");
System.out.print("安全序列为");
for(int i=0;i<m;i++){
if(i==(m-1)){
System.out.print("P" + security[i]);
}else{
System.out.print("P" + security[i] + "-->");
}
}
System.out.println();
System.out.println("=====死锁检测结束=====");
System.out.println();
return;
}
while((num1==0)&&(num2>0)){
System.out.println("没有安全序列");
System.out.println("系统不能将申请的资源分配给给该进程");
System.out.println("=====死锁检测结束=====");
System.out.println();
return;
}
}
}
}
//主运行类
public class Banker2 {
public static void main(String[] args) {
System.out.println("欢迎使用");
Scanner scanner = new Scanner(System.in);
TestBanker tb = new TestBanker();
tb.securityMethods();
}
}