java实现银行家算法
时间: 2023-08-14 08:38:39 浏览: 41
银行家算法可以用来避免死锁情况的发生,主要是通过资源分配来避免进程因为互相等待而陷入死锁。
下面是一个简单的Java实现:
```java
import java.util.Scanner;
public class BankerAlgorithm {
private int available[]; // 可用资源数
private int max[][]; // 最大需求矩阵
private int allocation[][]; // 分配矩阵
private int need[][]; // 需求矩阵
private int n; // 进程数量
private int m; // 资源类型数量
public BankerAlgorithm(int available[], int max[][], int allocation[][]) {
this.available = available;
this.max = max;
this.allocation = allocation;
n = max.length;
m = available.length;
need = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
need[i][j] = max[i][j] - allocation[i][j];
}
}
}
public boolean isSafe() {
boolean finish[] = new boolean[n];
int work[] = new int[m];
for (int i = 0; i < m; i++) {
work[i] = available[i];
}
int count = 0;
while (count < n) {
boolean found = false;
for (int i = 0; i < n; i++) {
if (!finish[i]) {
boolean canFinish = true;
for (int j = 0; j < m; j++) {
if (need[i][j] > work[j]) {
canFinish = false;
break;
}
}
if (canFinish) {
for (int j = 0; j < m; j++) {
work[j] += allocation[i][j];
}
finish[i] = true;
found = true;
count++;
}
}
}
if (!found) {
return false;
}
}
return true;
}
public boolean requestResources(int pid, int request[]) {
for (int i = 0; i < m; i++) {
if (request[i] > need[pid][i]) {
return false;
}
if (request[i] > available[i]) {
return false;
}
}
for (int i = 0; i < m; i++) {
available[i] -= request[i];
allocation[pid][i] += request[i];
need[pid][i] -= request[i];
}
if (isSafe()) {
return true;
} else {
for (int i = 0; i < m; i++) {
available[i] += request[i];
allocation[pid][i] -= request[i];
need[pid][i] += request[i];
}
return false;
}
}
public void display() {
System.out.println("Available:");
for (int i = 0; i < m; i++) {
System.out.print(available[i] + " ");
}
System.out.println("\nMax:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(max[i][j] + " ");
}
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] + " ");
}
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] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
int available[] = { 3, 3, 2 };
int max[][] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 }, { 4, 3, 3 } };
int allocation[][] = { { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 2 }, { 2, 1, 1 }, { 0, 0, 2 } };
BankerAlgorithm banker = new BankerAlgorithm(available, max, allocation);
banker.display();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter pid and request:");
int pid = scanner.nextInt();
int request[] = new int[banker.m];
for (int i = 0; i < banker.m; i++) {
request[i] = scanner.nextInt();
}
if (banker.requestResources(pid, request)) {
System.out.println("Request accepted");
banker.display();
} else {
System.out.println("Request denied");
}
}
}
```
其中,`available`数组表示每种资源当前可用的数量,`max`表示每个进程最大需要的资源数量,`allocation`表示每个进程已经分配的资源数量。`need`矩阵表示每个进程还需要的资源数量。
`isSafe`方法用来判断当前状态是否安全,如果安全则返回true,否则返回false。
`requestResources`方法用来处理进程的资源请求,如果请求成功则返回true,否则返回false。
`display`方法用来显示当前状态。
在`main`方法中,我们可以看到一个示例,其中包含了5个进程和3种资源,可以根据需要修改。在运行程序后,可以输入进程号和请求的资源数量,程序会判断是否可以满足请求,并进行相应的处理。