银行家算法的实现java
时间: 2023-11-17 07:08:05 浏览: 75
银行家算法的java实现
银行家算法的实现需要使用Java的数据结构,包括一维数组和HashMap。其中,一维数组用于存储Available、Request和Max等数据,而HashMap则用于存储Allocation和Need等数据。具体实现可以参考以下代码:
```
import java.util.HashMap;
import java.util.Scanner;
public class Bank {
private int[] Available; // 当前系统可以使用的资源数
private int[][] Max; // 当前进程最大需求的资源
private int[][] Allocation; // 当前进程占有的资源数
private int[][] Need; // 当前资源还需的资源数
private int[] Work; // 当前可用的资源数
private int[] Finish; // 当前进程是否能完成
public Bank() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入资源种类数:");
int resourceNum = scanner.nextInt();
System.out.println("请输入进程数:");
int processNum = scanner.nextInt();
Available = new int[resourceNum];
Max = new int[processNum][resourceNum];
Allocation = new int[processNum][resourceNum];
Need = new int[processNum][resourceNum];
Work = new int[resourceNum];
Finish = new int[processNum];
System.out.println("请输入当前系统可以使用的资源数:");
for (int i = 0; i < resourceNum; i++) {
Available[i] = scanner.nextInt();
}
System.out.println("请输入每个进程最大需求的资源数:");
for (int i = 0; i < processNum; i++) {
for (int j = 0; j < resourceNum; j++) {
Max[i][j] = scanner.nextInt();
}
}
System.out.println("请输入每个进程已占有的资源数:");
for (int i = 0; i < processNum; i++) {
for (int j = 0; j < resourceNum; j++) {
Allocation[i][j] = scanner.nextInt();
Need[i][j] = Max[i][j] - Allocation[i][j];
}
}
}
// 安全性检测
public void securityExamine() {
int processNum = Allocation.length;
int resourceNum = Available.length;
int count = 0;
// 初始化Work和Finish数组
for (int i = 0; i < resourceNum; i++) {
Work[i] = Available[i];
}
for (int i = 0; i < processNum; i++) {
Finish[i] = 0;
}
// 找到一个能够完成的进程
while (count < processNum) {
boolean flag = false;
for (int i = 0; i < processNum; i++) {
if (Finish[i] == 0) {
int j;
for (j = 0; j < resourceNum; j++) {
if (Need[i][j] > Work[j]) {
break;
}
}
if (j == resourceNum) {
for (int k = 0; k < resourceNum; k++) {
Work[k] += Allocation[i][k];
}
Finish[i] = 1;
flag = true;
count++;
}
}
}
if (!flag) {
System.out.println("系统不安全!");
return;
}
}
System.out.println("系统安全!");
}
// 银行家算法
public void request() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入请求资源的进程编号:");
int processId = scanner.nextInt();
System.out.println("请输入请求的资源数:");
int[] request = new int[Available.length];
for (int i = 0; i < Available.length; i++) {
request[i] = scanner.nextInt();
}
// 判断请求是否合法
for (int i = 0; i < Available.length; i++) {
if (request[i] > Need[processId][i]) {
System.out.println("请求失败,请求资源数超过了该进程还需的资源数!");
return;
}
if (request[i] > Available[i]) {
System.out.println("请求失败,请求资源数超过了系统当前可用的资源数!");
return;
}
}
// 模拟分配资源
for (int i = 0; i < Available.length; i++) {
Available[i] -= request[i];
Allocation[processId][i] += request[i];
Need[processId][i] -= request[i];
}
// 安全性检测
securityExamine();
}
// 打印进程状态
public void printState() {
System.out.println("当前系统可以使用的资源数:");
for (int i = 0; i < Available.length; i++) {
System.out.print(Available[i] + " ");
}
System.out.println();
System.out.println("每个进程最大需求的资源数:");
for (int i = 0; i < Max.length; i++) {
for (int j = 0; j < Max[i].length; j++) {
System.out.print(Max[i][j] + " ");
}
System.out.println();
}
System.out.println("每个进程已占有的资源数:");
for (int i = 0; i < Allocation.length; i++) {
for (int j = 0; j < Allocation[i].length; j++) {
System.out.print(Allocation[i][j] + " ");
}
System.out.println();
}
System.out.println("每个进程还需的资源数:");
for (int i = 0; i < Need.length; i++) {
for (int j = 0; j < Need[i].length; j++) {
System.out.print(Need[i][j] + " ");
}
System.out.println();
}
}
}
```
阅读全文