银行家算法的java编程实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*死锁避免和死锁检测模拟程序【银行家算

法】网络工程06-3班学号:310609040308*/

import java.util.*;

public class TestTheBanker {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

TheBanker tb = new TheBanker();

tb.deadlockAvoidance();//死锁避免

int gate = 1;

while(gate!=0){

tb.deadlockDetection();//死锁检测

System.out.println("如果您要继续分配资源请输入\"1\",退出请输入\"0\"");

System.out.print("您输入的值为:");

gate = scanner.nextInt();

System.out.println();

}

System.out.println("使用愉快!期待您下次使用!");

}

}

class TheBanker{

int m;

int n;

int[][] max;

int[][] maxbak;//备份用

int[][] allocation;

int[][] allocationbak;//备份用

int[][] need;

int[][] needbak;//备份用

int[] available;

int[] availablebak;//备份用

public TheBanker(){

Scanner s = new Scanner(System.in);

System.out.println("初始化==============");

System.out.print("请依次输入系统中的【进程数】和【资源类型数】:");

m = s.nextInt();

n = s.nextInt();

max =new int[m][n];

maxbak = new int[m][n];

allocation = new int[m][n];

allocationbak = new int[m][n];

need = new int[m][n];

needbak = new int[m][n];

available = new int[n];

availablebak = new int[n];

for(int i=0;i

System.out.print("请依次输入第" + i + "进程的各资源数:");

for(int j=0;j

max[i][j] = s.nextInt();

maxbak[i][j] = max[i][j];

}

}

for(int i=0;i

System.out.print("请依次输入第" + i + "进程中已分配资源的数量:");

for(int j=0;j

allocation[i][j] = s.nextInt();

allocationbak[i][j] = allocation[i][j];

}

}

for(int i=0;i

for(int j=0;j

need[i][j] = max[i][j] - allocation[i][j];

needbak[i][j] = need[i][j];

}

}

for(int i=0;i

System.out.print("请输入系统中第" + i + "种资源的剩余量:");

available[i] = s.nextInt();

availablebak[i] = available[i];

}

System.out.println("初始化结果=============");

System.out.println(" MAX

ALLOCATION NEED AVAILABLE");

for(int i=0;i

System.out.print("P" + i + ": ");

for(int j=0;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

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

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

if(available[j]>9){

System.out.print(available[j] + " ");

}else{

System.out.print(" " + available[j] + " ");

}

}

}

System.out.println();

}

System.out.println("=====完成初始化=====");

System.out.println();

}

public void deadlockAvoidance(){

int[] security = new int[m];

boolean[] param = new boolean[m];

int[] tar = new int[n];

int count = 0;

int num1 = m+1;//计数器,每循环一遍所有进程就自减1

int num2 = m;//计数器,每遇到一个被满足的进程就自减1

相关文档
最新文档