銀行家算法 避免進程發生死鎖

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();
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章