package operatingSystem;
import java.util.Scanner;
/**
* 避免进程发生死锁
* 银行家算法
* @author TanPeng
*/
public class Banker {
private static int n; // 进程个数
private static int m = 3; // 资源类型数
private static int[] available; // 可利用的资源向量
private static int[][] max; // 资源最大申请量矩阵
private static int[][] allocation; // 资源分配矩阵
private static int[][] need; // 以后还需要的资源矩阵
private static int[][] request; // 进程申请的资源矩阵
public static void main(String[] args) {
getData();
banker();
}
/**
* 银行家算法核心部分
*/
private static void banker() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (request[i][j] <= need[i][j]) {
if (request[i][j] <= available[i]) {
available[i] = available[i] - request[i][j];
allocation[i][j] = allocation[i][j] + request[i][j];
need[i][j] = need[i][j] - request[i][j];
checkSafe();
} else {
System.out.println("系统资源不足!!");
System.exit(0);
}
} else {
System.out.println("申请的资源超出其需要的资源,错误!");
System.exit(0);
}
}
}
}
/**
* 安全状态检查算法
*/
private static void checkSafe() {
int[] work = new int[m];
for (int i = 0; i < work.length; i++) {
work[i] = available[i]; // 初始化work=available
}
boolean[] finishFlag = new boolean[n]; // 用于记录是否有足够资源分配给进程使它能正常完成
for (int i = 0; i < finishFlag.length; i++) {
finishFlag[i] = false;
}
int[] result = new int[n]; // 安全序列
for (int i = 1; i < n; i++) {
// 第i个进程
for (int j = 0; j < n; j++) {
if (finishFlag[i] == false) {
if (need[i][0] <= work[0] && need[i][1] <= work[1]
&& need[i][2] <= work[2]) {
for (int k = 0; k < m; k++) {
work[k] = work[k] + allocation[j][k];
}
finishFlag[j] = true;
result[i - 1] = j;
}
}
}
}
int count = 0;
for (int i = 0; i < finishFlag.length; i++) {
if (finishFlag[i] == true)
count++;
}
if (count == n) {
System.out.println("存在一个安全序列:");
for (int i = 0; i < result.length; i++) {
System.out.print("P" + result[i] + " ");
}
System.out.println();
} else {
System.out.println("系统属于不安全状态!");
System.exit(0);
}
}
/**
* 输入数据
*/
private static void getData() {
Scanner in = new Scanner(System.in);
System.out.println("请输入进程数:");
n = in.nextInt();
available = new int[m];
System.out.println("请输入可利用的资源向量(3种资源):");
for (int i = 0; i < 3; i++) {
available[i] = in.nextInt();
}
max = new int[n][m];
System.out.println("请输入资源最大申请量矩阵:");
for (int i = 0; i < n; i++) {
System.out.println("请输入进程 " + (i + 1) + " 所需的最大资源数(3种资源):");
for (int j = 0; j < m; j++) {
max[i][j] = in.nextInt();
}
}
allocation = new int[n][m];
System.out.println("请输入已分配资源的矩阵:");
for (int i = 0; i < n; i++) {
System.out.println("请输入进程 " + (i + 1) + " 已经分配的资源数(3种资源):");
for (int j = 0; j < m; j++) {
allocation[i][j] = in.nextInt();
}
}
need = new int[n][m];
System.out.println("请输入还需要分配的资源矩阵:");
for (int i = 0; i < n; i++) {
System.out.println("请输入进程 " + (i + 1) + " 还需要的资源数(3种资源):");
for (int j = 0; j < m; j++) {
need[i][j] = in.nextInt();
}
}
request = new int[n][m];
System.out.println("请输入申请资源矩阵:");
for (int i = 0; i < n; i++) {
System.out.println("请输入进程 " + (i + 1) + " 申请的资源数(3种资源):");
for (int j = 0; j < m; j++) {
request[i][j] = in.nextInt();
}
}
in.close();
}
}
银行家算法 避免进程发生死锁
最新推荐文章于 2024-08-07 16:28:55 发布