银行家算法的java编程实现.doc





《银行家算法的java编程实现.doc》由会员分享,可在线阅读,更多相关《银行家算法的java编程实现.doc(8页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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=
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 银行家 算法 java 编程 实现

限制150内