写个银行家算法代码 JAVA语言

时间: 2023-08-31 21:39:18 浏览: 66
### 回答1: public class BankerAlgorithm { static int m, n; static int allocation[][] = new int[10][10]; static int max[][] = new int[10][10]; static int need[][] = new int[10][10]; static int available[] = new int[10]; static int sequence[] = new int[10]; static int counter = 0; public static void main(String args[]) { System.out.println("Enter the number of processes and resources : "); Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); System.out.println("Enter the Allocation Matrix : "); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) allocation[i][j] = sc.nextInt(); System.out.println("Enter the Max Matrix : "); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) max[i][j] = sc.nextInt(); System.out.println("Enter the Available Vector : "); for (int j = 0; j < n; j++) available[j] = sc.nextInt(); //calculate the need matrix for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) need[i][j] = max[i][j] - allocation[i][j]; //run the algorithm boolean done[] = new boolean[m]; int j = 0; while (j < m) { boolean allocated = false; for (int i = 0; i < m; i++) { if (!done[i] && check(i)) { for (int k = 0; k < n; k++) available[k] = available[k] - need[i][k] + max[i][k]; System.out.println("Allocated process : " + i); allocated = done[i] = true; sequence[counter++] = i; j++; } } if (!allocated) break; } if (j == m) { System.out.println("\nSafely allocated"); System.out.println("Safe Sequence is : "); for (int i = 0; i < m - 1; i++) System.out.print(sequence[i] + " -> "); System.out.print(sequence[m - 1]); } else System.out.println("All proceess cant be allocated safely"); } static boolean check(int i) { for (int j = 0; j < n; j++) if (need[i][j] > available[j]) return false; return true; } } ### 回答2: 银行家算法是一种确保系统资源分配安全的算法,用于避免死锁的发生。下面给出一个简单的银行家算法的示例代码(使用JAVA语言): ```java import java.util.Arrays; public class BankerAlgorithm { private int[][] max; // 最大需求矩阵 private int[][] allocation; // 已分配资源矩阵 private int[][] need; // 剩余需求矩阵 private int[] available; // 可用资源向量 private boolean[] finished; // 标记进程是否执行完毕 public BankerAlgorithm(int[][] max, int[][] allocation, int[] available) { this.max = max; this.allocation = allocation; this.available = available; this.finished = new boolean[max.length]; this.need = new int[max.length][max[0].length]; calculateNeed(); } // 计算剩余需求矩阵 private void calculateNeed() { for (int i = 0; i < max.length; i++) { for (int j = 0; j < max[i].length; j++) { need[i][j] = max[i][j] - allocation[i][j]; } } } // 判断进程是否能够安全执行 public boolean isSafe() { int processCount = max.length; // 进程数量 int resourceCount = max[0].length; // 资源数量 int[] work = Arrays.copyOf(available, available.length); // 创建一个可用资源副本 boolean[] finish = Arrays.copyOf(finished, finished.length); // 创建一个finish副本 int[] safeSequence = new int[processCount]; int index = 0; // 安全序列下标 while (index < processCount) { boolean isSafe = false; for (int i = 0; i < processCount; i++) { if (!finish[i]) { boolean isProcessAvailable = true; for (int j = 0; j < resourceCount; j++) { if (need[i][j] > work[j]) { isProcessAvailable = false; break; } } if (isProcessAvailable) { for (int k = 0; k < resourceCount; k++) { work[k] += allocation[i][k]; } safeSequence[index++] = i; // 添加到安全序列中 finish[i] = true; // 标记进程已执行完毕 isSafe = true; } } } if (!isSafe) { break; } } // 判断是否所有进程都能安全执行 boolean isAllFinished = true; for (boolean f : finish) { if (!f) { isAllFinished = false; break; } } // 输出安全序列 if (isAllFinished) { System.out.println("安全序列:"); for (int i = 0; i < processCount - 1; i++) { System.out.print("P" + safeSequence[i] + " -> "); } System.out.print("P" + safeSequence[processCount - 1]); System.out.println(); } else { System.out.println("无安全序列,系统处于不安全状态!"); } return isAllFinished; } public static void main(String[] args) { // 定义三个进程和三个资源 int[][] max = {{7, 5, 3}, {3, 2, 2}, {9, 0, 2}}; int[][] allocation = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}}; int[] available = {3, 3, 2}; BankerAlgorithm banker = new BankerAlgorithm(max, allocation, available); banker.isSafe(); } } ``` 以上代码通过银行家算法判断系统是否处于安全状态,如果是,则输出安全序列;如果不是,则输出无安全序列,并提示系统处于不安全状态。

相关推荐

最新推荐

recommend-type

java实现简单银行家算法

主要为大家详细介绍了java实现简单银行家算法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

java实现银行家算法(Swing界面)

主要为大家详细介绍了银行家算法的java代码实现,Swing写的界面,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

银行家算法Java实现带图形界面

图形界面银行家算法,设计有 n 个进程共享 m 个系统资源的系统,进程可动态的申请和释放资源,系统按各进程的申请动态的分配资源。系统能显示各个进程申请和释放资源,以及系统动态分配资源的过程,便于用户观察和...
recommend-type

操作系统实验二——银行家算法

银行家算法是在实现资源分配时避免死锁 的一个著名算法,该算法是在能确保系统处于安全状态时才把资源分配给申请者。 本设计的目的是通过编写和调试一个系统动态分配资源的简单模拟程序,观察死锁产生的条件,并采用...
recommend-type

银行家算法java实现带UI界面(修正版)

银行家算法java实现带UI界面(修正版:修改了由yu114提出的BankerMainUI.java与AddProcessDialog.java文件相同的问题) 以后大家发现类似问题的话,请大家知会一声,希望我们共同成长。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

解释这行代码 c = ((double)rand() / RAND_MAX) * (a + b - fabs(a - b)) + fabs(a - b);

这行代码是用于生成 a 和 b 之间的随机数。首先,它使用 rand() 函数生成一个 [0,1) 之间的随机小数,然后将这个小数乘以 a、b 范围内的差值,再加上 a 和 b 中的较小值。这可以确保生成的随机数大于等于 a,小于等于 b,而且不会因为 a 和 b 之间的差距过大而导致难以生成足够多的随机数。最后,使用 fabs() 函数来确保计算结果是正数。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。