银行家算法Java代码
时间: 2023-11-17 09:04:44 浏览: 85
Java代码写的银行家算法
4星 · 用户满意度95%
以下是银行家算法的Java代码:
```
import java.util.Scanner;
public class Bank {
private int available[]; // 可用资源
private int max[][]; // 最大需求矩阵
private int allocation[][]; // 分配矩阵
private int need[][]; // 需求矩阵
private int request[]; // 请求资源向量
private int work[]; // 工作向量
private boolean 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];
request = new int[resourceNum];
work = new int[resourceNum];
finish = new boolean[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 = finish.length;
int resourceNum = available.length;
int count = 0; // 完成进程数
boolean isSafe = true; // 是否安全
// 初始化工作向量
for (int i = 0; i < resourceNum; i++) {
work[i] = available[i];
}
// 初始化进程完成标志
for (int i = 0; i < processNum; i++) {
finish[i] = false;
}
// 找到一个可以完成的进程
while (count < processNum) {
boolean canFinish = false;
for (int i = 0; i < processNum; i++) {
if (!finish[i]) {
boolean needLessThanWork = true;
for (int j = 0; j < resourceNum; j++) {
if (need[i][j] > work[j]) {
needLessThanWork = false;
break;
}
}
if (needLessThanWork) {
canFinish = true;
finish[i] = true;
count++;
for (int j = 0; j < resourceNum; j++) {
work[j] += allocation[i][j];
}
}
}
}
if (!canFinish) {
isSafe = false;
break;
}
}
if (isSafe) {
System.out.println("系统是安全的");
} else {
System.out.println("系统是不安全的");
}
}
// 银行家算法
public void request() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入请求资源的进程编号:");
int processId = scanner.nextInt();
System.out.println("请输入请求资源向量:");
for (int i = 0; i < request.length; i++) {
request[i] = scanner.nextInt();
}
int resourceNum = available.length;
boolean isSafe = true; // 是否安全
// 判断请求是否合法
for (int i = 0; i < resourceNum; i++) {
if (request[i] > need[processId][i] || request[i] > available[i]) {
System.out.println("请求不合法");
return;
}
}
// 模拟分配资源
for (int i = 0; i < resourceNum; i++) {
available[i] -= request[i];
allocation[processId][i] += request[i];
need[processId][i] -= request[i];
}
// 安全性检测
int processNum = finish.length;
int count = 0; // 完成进程数
// 初始化工作向量
for (int i = 0; i < resourceNum; i++) {
work[i] = available[i];
}
// 初始化进程完成标志
for (int i = 0; i < processNum; i++) {
finish[i] = false;
}
// 找到一个可以完成的进程
while (count < processNum) {
boolean canFinish = false;
for (int i = 0; i < processNum; i++) {
if (!finish[i]) {
boolean needLessThanWork = true;
for (int j = 0; j < resourceNum; j++) {
if (need[i][j] > work[j]) {
needLessThanWork = false;
break;
}
}
if (needLessThanWork) {
canFinish = true;
finish[i] = true;
count++;
for (int j = 0; j < resourceNum; j++) {
work[j] += allocation[i][j];
}
}
}
}
if (!canFinish) {
isSafe = false;
break;
}
}
// 回滚分配
if (!isSafe) {
for (int i = 0; i < resourceNum; i++) {
available[i] += request[i];
allocation[processId][i] -= request[i];
need[processId][i] += request[i];
}
System.out.println("分配失败,回滚分配");
} else {
System.out.println("分配成功");
}
}
// 打印进程状态
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();
}
}
}
```
阅读全文