操作系统算法 之 银行家算法
来源:互联网 发布:矩阵满秩是什么意思 编辑:程序博客网 时间:2024/05/20 01:45
MyBanker.java:银行家算法类
Main.java:程序入口
如有疑问,请请在评论留言。
下面附上代码
MyBanker.java
————————————————————
import java.util.LinkedHashMap;import java.util.Map;import java.util.Scanner;public class MyBanker {private int resourceCount;private int processCount;private int[] available;private int[][] max;private int[][] allocation;private int[][] need;private int[] request;Scanner scanner = new Scanner(System.in);public MyBanker(){System.out.println("请输入资源数量:");this.resourceCount = scanner.nextInt();System.out.println("请输入进程数量:");this.processCount = scanner.nextInt();available = new int[resourceCount];max = new int[processCount][resourceCount];allocation = new int[processCount][resourceCount];need = new int[processCount][resourceCount];}public void initData(){System.out.println("请输入" + resourceCount + "类资源的数量:"); for(int i=0;i<resourceCount;i++){ available[i]=scanner.nextInt(); } for(int i=0;i<processCount;i++){ System.out.println("请输入进程" + i + "对"+ resourceCount +"类资源的最大需求:"); for(int j=0;j<resourceCount;j++){ max[i][j]=scanner.nextInt(); } } for(int i=0;i<processCount;i++){ System.out.println("请输入进程" + i + "已分配的" + resourceCount + "类资源数量:"); for(int j=0;j<resourceCount;j++){ allocation[i][j]=scanner.nextInt(); } } for(int i=0;i<processCount;i++){ for(int j=0;j<resourceCount;j++){ need[i][j]=max[i][j]-allocation[i][j]; } } for(int i=0;i<resourceCount;i++){ for(int j=0;j<processCount;j++){ available[i]-=allocation[j][i]; } } checkSafety();}public void requestResource(){System.out.println("请输入申请资源的线程:");int requestId = scanner.nextInt();if(requestId < 0 || requestId >= processCount){throw new RuntimeException("该线程不存在!");}System.out.println("请输入申请的资源(共" + resourceCount + "类,若某类资源不申请则填0)。");request = new int[resourceCount]; for(int i=0;i<resourceCount;i++){ request[i]=scanner.nextInt(); } for(int i=0;i<resourceCount;i++){ if(request[i] > need[requestId][i]){ throw new RuntimeException(requestId+"线程申请的资源超出其需要的资源!"); } } for(int i=0;i<resourceCount;i++){ if(request[i] > available[i]){ throw new RuntimeException(requestId+"线程申请的资源大于系统资源!"); } } for(int i=0;i<resourceCount;i++){ available[i] -= request[i]; allocation[requestId][i] += request[i]; need[requestId][i] -= request[i]; } if(checkSafety()){ System.out.println(requestId+"线程申请资源成功!"); }else{ for(int i=0;i<resourceCount;i++){ available[i] += request[i]; allocation[requestId][i] -= request[i]; need[requestId][i] += request[i]; } throw new RuntimeException(requestId+"线程申请资源失败!"); }}private boolean checkSafety(){ int work[]=new int[resourceCount]; boolean finish[]=new boolean[processCount]; int queue[]=new int[processCount]; Map<Integer, Integer> rest = new LinkedHashMap<Integer, Integer>(); for(int i=0;i<resourceCount;i++){ work[i] = available[i]; } for(int i=0;i<processCount;i++){ finish[i] = false; rest.put(i, i); } int processId = 0; int count = 0; int noChange = 0; while(rest.size() != 0){ if(finish[processId]){ processId = (processId + 1) % processCount; continue; } boolean p = true; for(int i=0;i<resourceCount;i++){ if(need[processId][i] > work[i]){ p = false; break; } } if(p){ rest.remove(processId); for(int i=0;i<resourceCount;i++){ work[i] += allocation[processId][i]; } queue[count] = processId; finish[processId] = true; count++; noChange = 0; }else{ noChange++; if(noChange == rest.size()){ System.out.println("不存在安全序列!"); return false; } } processId = (processId + 1) % processCount; } System.out.println("存在安全序列:"); for(int i=0;i<processCount;i++){ if(i != 0){ System.out.print(","); } System.out.print(queue[i]); } System.out.println(); return true;}public void showData(){System.out.println("need:");for(int i=0;i<processCount;i++){for(int j=0;j<resourceCount;j++){if(j != 0){System.out.print(" ");}System.out.print(need[i][j]);}System.out.println();}System.out.println("available:");for(int i=0;i<resourceCount;i++){if(i != 0){System.out.print(" ");}System.out.print(available[i]);}System.out.println();}}
————————————————————
Main.java
————————————————————
public class Main {public static void main(String[] args) {MyBanker mb = new MyBanker();Scanner s = new Scanner(System.in);mb.initData();boolean p = true;while(p){try {System.out.println("请输入你要进行的操作:(1:申请资源。2:显示资源。0:退出。)");int cmd = s.nextInt();switch(cmd){case 1:{mb.requestResource();};break;case 2:{mb.showData();};break;case 0:{p = false;};break;}} catch (Exception e) {System.out.println(e.getMessage());}}}}————————————————————
0 0
- 操作系统之银行家算法
- 操作系统之银行家算法
- 操作系统之银行家算法
- 操作系统 之 银行家算法
- 操作系统之银行家算法
- 操作系统之银行家算法
- 操作系统算法 之 银行家算法
- 操作系统之--银行家算法1
- 操作系统之--银行家算法2
- 操作系统实验之银行家算法
- 避免死锁之银行家算法 银行家算法 操作系统
- 操作系统实验之银行家算法模拟
- 操作系统之死锁概述与银行家算法
- 操作系统--银行家算法设计
- 操作系统课程设计银行家算法
- 操作系统课程设计银行家算法
- 操作系统::银行家算法
- 操作系统银行家算法
- 适配器模式(设计模式_19)
- (4.2.7.3)Android PullToRefresh 分析之三、手势响应
- XRecyclerView与轮播图的使用
- 更多赋值运算符和逗号运算符
- 12-30 Wannafly每日一题 Pretty Song
- 操作系统算法 之 银行家算法
- (4.2.7.4) Android PullToRefresh 分析之四、扩展RecyclerView
- 设计心理学的原则和定律之冯·雷斯托夫效应
- SpringBoot使用笔记
- Sunday算法流程与代码
- (4.2.7.5)Android PullToRefresh 分析之五、扩展刷新加载样式
- 后端的基础知识点
- hdu 2639 Bone Collector II
- CodeForces - 749C Voting (队列)