银行家算法java实现
时间: 2023-10-16 21:27:02 浏览: 33
银行家算法是一种资源分配算法,用于管理多个进程之间的资源分配。以下是Java实现示例:
```java
import java.util.Scanner;
public class BankerAlgorithm {
private int[][] need; // 需求矩阵
private int[][] allocation; // 分配矩阵
private int[][] max; // 最大需求矩阵
private int[] available; // 可用资源向量
private int numberOfProcesses; // 进程数
private int numberOfResources; // 资源数
// 初始化数据
public void init() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入进程数:");
numberOfProcesses = sc.nextInt();
System.out.print("请输入资源数:");
numberOfResources = sc.nextInt();
need = new int[numberOfProcesses][numberOfResources];
allocation = new int[numberOfProcesses][numberOfResources];
max = new int[numberOfProcesses][numberOfResources];
available = new int[numberOfResources];
// 输入最大需求矩阵
System.out.println("请输入最大需求矩阵:");
for (int i = 0; i < numberOfProcesses; i++) {
for (int j = 0; j < numberOfResources; j++) {
max[i][j] = sc.nextInt();
}
}
// 输入分配矩阵
System.out.println("请输入分配矩阵:");
for (int i = 0; i < numberOfProcesses; i++) {
for (int j = 0; j < numberOfResources; j++) {
allocation[i][j] = sc.nextInt();
}
}
// 计算需求矩阵
for (int i = 0; i < numberOfProcesses; i++) {
for (int j = 0; j < numberOfResources; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
// 输入可用资源向量
System.out.println("请输入可用资源向量:");
for (int i = 0; i < numberOfResources; i++) {
available[i] = sc.nextInt();
}
}
// 判断是否满足需求
public boolean isSafe() {
boolean[] finish = new boolean[numberOfProcesses];
int[] work = new int[numberOfResources];
// 初始化work向量
for (int i = 0; i < numberOfResources; i++) {
work[i] = available[i];
}
// 初始化finish向量
for (int i = 0; i < numberOfProcesses; i++) {
finish[i] = false;
}
int count = 0; // 已分配的进程数
while (count < numberOfProcesses) {
boolean flag = false;
// 查找未分配且满足需求的进程
for (int i = 0; i < numberOfProcesses; i++) {
if (!finish[i]) {
int j;
for (j = 0; j < numberOfResources; j++) {
if (need[i][j] > work[j]) {
break;
}
}
if (j == numberOfResources) {
// 找到未分配且满足需求的进程
for (j = 0; j < numberOfResources; j++) {
work[j] += allocation[i][j];
}
// 标记进程已分配
finish[i] = true;
flag = true;
count++;
}
}
}
if (!flag) {
// 没有找到未分配且满足需求的进程
break;
}
}
return count == numberOfProcesses;
}
// 分配资源
public void allocate() {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要申请资源的进程编号:");
int pid = sc.nextInt();
System.out.println("请输入要申请的各个资源数量:");
int[] request = new int[numberOfResources];
for (int i = 0; i < numberOfResources; i++) {
request[i] = sc.nextInt();
}
// 判断是否超过最大需求
for (int i = 0; i < numberOfResources; i++) {
if (request[i] > need[pid][i]) {
System.out.println("申请资源超过最大需求!");
return;
}
}
// 判断是否超过可用资源
for (int i = 0; i < numberOfResources; i++) {
if (request[i] > available[i]) {
System.out.println("没有足够的资源分配!");
return;
}
}
// 分配资源
for (int i = 0; i < numberOfResources; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (isSafe()) {
System.out.println("分配成功!");
} else {
// 分配失败,回滚操作
System.out.println("无法分配,回滚操作!");
for (int i = 0; i < numberOfResources; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
}
public static void main(String[] args) {
BankerAlgorithm ba = new BankerAlgorithm();
ba.init();
ba.allocate();
}
}
```
其中,init()方法用于初始化数据,isSafe()方法用于判断是否满足需求,allocate()方法用于分配资源。在main()方法中,首先调用init()方法进行初始化,然后调用allocate()方法进行资源分配。