《银行家算法的java编程实现.doc》由会员分享,可在线阅读,更多相关《银行家算法的java编程实现.doc(8页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、/*死锁避免与死锁检测模拟程序【银行家算法】 网络工程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
2、.out.println(如果您要继续分配资源请输入1,退出请输入0); System.out.print(您输入的值为:); gate = scanner.nextInt(); System.out.println(); System.out.println(使用愉快!期待您下次使用!);class TheBankerint m;int n;int max;int maxbak;/备份用int allocation;int allocationbak;/备份用int need;int needbak;/备份用int available;int availablebak;/备份用public
3、TheBanker() Scanner s = new Scanner(System.in); System.out.println(初始化=); System.out.print(请依次输入系统中的【进程数】和【资源类型数】:); m = s.nextInt(); n = s.nextInt(); max =new intmn; maxbak = new intmn; allocation = new intmn; allocationbak = new intmn; need = new intmn; needbak = new intmn; available = new intn; a
4、vailablebak = new intn; for(int i=0;imax.length;i+)/初始化向量MAX、ALLOCATION、NEED、AVAILABLE System.out.print(请依次输入第 + i + 进程的各资源数:); for(int j=0;jmaxi.length;j+) maxij = s.nextInt(); maxbakij = maxij; for(int i=0;iallocation.length;i+) System.out.print(请依次输入第 + i + 进程中已分配资源的数量:); for(int j=0;jallocationi
5、.length;j+) allocationij = s.nextInt(); allocationbakij = allocationij; for(int i=0;ineed.length;i+) for(int j=0;jneedi.length;j+) needij = maxij - allocationij; needbakij = needij; for(int i=0;iavailable.length;i+) System.out.print(请输入系统中第 + i + 种资源的剩余量:); availablei = s.nextInt(); availablebaki =
6、availablei; System.out.println(初始化结果=); System.out.println( MAX ALLOCATION NEED AVAILABLE); for(int i=0;im;i+) System.out.print(P + i + : ); for(int j=0;j9)/如果是两位数,控制格式,在数字前少输出一个 。 System.out.print(maxij + ); else System.out.print( + maxij + ); System.out.print( ); for(int j=0;j9) System.out.print(a
7、llocationij + ); else System.out.print( + allocationij + ); System.out.print( ); for(int j=0;j9) System.out.print(needij + ); else System.out.print( + needij + ); if(i=0) System.out.print( ); for(int j=0;j9) System.out.print(availablej + ); else System.out.print( + availablej + ); System.out.println
8、(); System.out.println(=完成初始化=); System.out.println();public void deadlockAvoidance() int security = new intm; boolean param = new booleanm; int tar = new intn; int count = 0; int num1 = m+1;/计数器,每循环一遍所有进程就自减1 int num2 = m;/计数器,每遇到一个被满足的进程就自减1 while(num10)/如果num1=0,则说明依次循环下来没有能够满足的进程,因此中止 for(int i=
9、0;im;i+) if(parami=false)/只有没有被满足的进程才可以进入内层循环 parami = true; for(int j=0;jn;j+) tarj = availablej - needij; if(tarj0) parami = false; if(parami=true) for(int k=0;k0) System.out.print(【安全序列】为:); for(int i=0;i); System.out.println(); System.out.println(=【死锁避免】结束=); System.out.println(); return; while(
10、num1=0)&(num20) System.out.println(抱歉!没有【安全序列】!); System.out.println(=【死锁避免】结束=); System.out.println(); return; public void deadlockDetection() Scanner sc = new Scanner(System.in); int key; int security = new intm; boolean param = new booleanm; int temp = new intn; int tar = new intn; int count = 0;
11、 int num1 = m+1;/计数器,每循环一遍所有进程就自减1 int num2 = m;/计数器,每遇到一个被满足的进程就自减1 for(int i=0;im;i+)/回复死锁检测前的状态 for(int j=0;jn;j+) maxij = maxbakij; allocationij = allocationbakij; needij = needbakij; availablej = availablebakj; System.out.println(); System.out.println(死锁检测=); System.out.println(如果您此时想申请系统资源请输入【
12、进程号】和+n+种【资源量】,系统将帮您判断是否可行。); System.out.print(您输入的进程号为:); key = sc.nextInt(); for(int i=0;in;i+) System.out.print(您要申请的第+i+种资源的数量为:); tempi = sc.nextInt(); for(int i=0;in;i+) allocationkeyi = allocationkeyi + tempi; needkeyi = needkeyi - tempi; if(needkeyi0) System.out.println(申请资源大于所需资源,系统【不能】分配资源
13、!); for(int k=0;km;k+)/回复死锁检测前的状态 for(int j=0;jn;j+) if(k=0) availablej = availablebakj; maxkj = maxbakkj; allocationkj = allocationbakkj; needkj = needbakkj; return; availablei = availablei - tempi; if(availablei0) System.out.println(申请资源大于系统所剩资源,系统【不能】分配资源!); for(int k=0;km;k+)/回复死锁检测前的状态 for(int
14、j=0;jn;j+) if(k=0) availablej = availablebakj; maxkj = maxbakkj; allocationkj = allocationbakkj; needkj = needbakkj; return; System.out.println(申请资源时各进程的状态=); System.out.println( MAX ALLOCATION NEED AVAILABLE); for(int i=0;im;i+) System.out.print(P + i + : ); for(int j=0;j9) System.out.print(maxij +
15、 ); else System.out.print( + maxij + ); System.out.print( ); for(int j=0;j9) System.out.print(allocationij + ); else System.out.print( + allocationij + ); System.out.print( ); for(int j=0;j9) System.out.print(needij + ); else System.out.print( + needij + ); if(i=0) System.out.print( ); for(int j=0;j
16、9) System.out.print(availablej + ); else System.out.print( + availablej + ); System.out.println(); System.out.println(=完成状态展示=); System.out.println(); while(num10)/如果num1=0,则说明依次循环下来没有能够满足的进程,因此中止 for(int i=0;im;i+) if(parami=false)/只有没有被满足的进程才可以进入内层循环 parami = true; for(int j=0;jn;j+) tarj = availa
17、blej - needij; if(tarj0) parami = false; if(parami=true) for(int k=0;k0) System.out.print(【安全序列】为:); for(int i=0;i); System.out.println(); System.out.println(可以产生新的安全序列!系统【能】将申请的资源分配给P+ key +!); System.out.println(=死锁检测结束=); System.out.println(); return; while(num1=0)&(num20) System.out.println(抱歉!【没有】安全序列!); System.out.println(系统【不能】将申请的资源分配给P+ key +!); System.out.println(=死锁检测结束=); System.out.println(); return;