用java写一个银行家算法
时间: 2024-01-01 21:04:46 浏览: 23
银行家算法是一种死锁避免算法,用于管理资源的分配。以下是一个Java语言实现的银行家算法:
```java
import java.util.Scanner;
public class BankerAlgorithm {
private int[][] max; // 最大需求矩阵
private int[][] allocation; // 分配矩阵
private int[][] need; // 需求矩阵
private int[] available; // 可用资源向量
private int[] work; // 工作向量
private int[] safeSequence; // 安全序列
private boolean[] finish; // 是否完成标志
private int n; // 进程数
private int m; // 资源种类数
private Scanner scanner;
public BankerAlgorithm() {
scanner = new Scanner(System.in);
System.out.print("请输入进程数n:");
n = scanner.nextInt();
System.out.print("请输入资源种类数m:");
m = scanner.nextInt();
max = new int[n][m];
allocation = new int[n][m];
need = new int[n][m];
available = new int[m];
work = new int[m];
safeSequence = new int[n];
finish = new boolean[n];
// 初始化各矩阵和向量
System.out.println("请输入最大需求矩阵Max:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
max[i][j] = scanner.nextInt();
}
}
System.out.println("请输入分配矩阵Allocation:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
allocation[i][j] = scanner.nextInt();
need[i][j] = max[i][j] - allocation[i][j];
}
}
System.out.println("请输入可用资源向量Available:");
for (int i = 0; i < m; i++) {
available[i] = scanner.nextInt();
}
}
// 检查进程是否能够获得资源
public boolean check(int i) {
for (int j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
return false;
}
}
return true;
}
// 安全性检查
public boolean safetyCheck() {
int count = 0;
for (int i = 0; i < m; i++) {
work[i] = available[i];
}
for (int i = 0; i < n; i++) {
finish[i] = false;
}
while (count < n) {
boolean flag = false;
for (int i = 0; i < n; i++) {
if (!finish[i] && check(i)) {
for (int j = 0; j < m; j++) {
work[j] += allocation[i][j];
}
safeSequence[count++] = i;
finish[i] = true;
flag = true;
}
}
if (!flag) {
return false;
}
}
return true;
}
// 资源分配
public void resourceAllocation() {
System.out.println("请输入请求资源的进程编号和请求向量:");
int pid = scanner.nextInt();
int[] request = new int[m];
for (int i = 0; i < m; i++) {
request[i] = scanner.nextInt();
}
for (int i = 0; i < m; i++) {
if (request[i] > need[pid][i] || request[i] > available[i]) {
System.out.println("请求无法满足!");
return;
}
}
for (int i = 0; i < m; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (safetyCheck()) {
System.out.println("分配成功!");
} else {
System.out.println("分配失败!");
for (int i = 0; i < m; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
}
}
// 打印输出矩阵和向量
public void print() {
System.out.println("Max矩阵:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(max[i][j] + "\t");
}
System.out.println();
}
System.out.println("Allocation矩阵:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(allocation[i][j] + "\t");
}
System.out.println();
}
System.out.println("Need矩阵:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(need[i][j] + "\t");
}
System.out.println();
}
System.out.println("Available向量:");
for (int i = 0; i < m; i++) {
System.out.print(available[i] + "\t");
}
System.out.println();
System.out.println("安全序列:");
for (int i = 0; i < n; i++) {
System.out.print(safeSequence[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
BankerAlgorithm banker = new BankerAlgorithm();
banker.print();
banker.resourceAllocation();
banker.print();
}
}
```
这个程序实现了银行家算法的基本功能,包括输入数据、安全性检查、资源分配等。用户可以根据自己的需要进行修改和扩展。