操作系统之银行家算法的学习Java系列

在网上看到很多关于银行家算法的文章,感觉和真正的算法有点出入,安全检查是银行家算法中的一个环节,但是很多网上的代码只有检查代码,没有银行家算法的代码,一下贴出本人对银行家算法的理解,这段代码借鉴与网上,应该很多人都见过,但是我对其有所改正,如果有人为源代码正确的请和我讨论。

package first;


import java.util.Scanner;


public class Exe {


	/**
	 * @param args
	 */
	private static int process;// 进程数量
	private static int resource = 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) {
		// TODO Auto-generated method stub


		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入进程数>>");
		process = scanner.nextInt();
		System.out.print("请输入可利用资源(3个资源种类)>>");
		available = new int[resource];
		for (int i = 0; i < resource; i++) {
			available[i] = scanner.nextInt();
		}
		System.out.println("请输入已分配矩阵");
		allocation = new int[process][resource];
		for (int i = 0; i < process; i++) {
			System.out.print("请输入进程" + (i + 1) + "已分配的资源数>>");
			for (int j = 0; j < resource; j++) {
				allocation[i][j] = scanner.nextInt();
			}
		}
		System.out.println("请输入最大需求矩阵");
		max = new int[process][resource];
		for (int i = 0; i < process; i++) {
			System.out.print("请输入进程" + (i + 1) + "最大需求的资源数>>");
			for (int j = 0; j < resource; j++) {
				max[i][j] = scanner.nextInt();
			}
		}
		System.out.println("请输入请求矩阵");
		request = new int[process][resource];
		for (int i = 0; i < process; i++) {
			System.out.print("请输入进程" + (i + 1) + "请求资源数>>");
			for (int j = 0; j < resource; j++) {
				request[i][j] = scanner.nextInt();
			}
		}


		need = new int[process][resource];// 需求矩阵就是最大需求减去已分配
		for (int i = 0; i < process; i++) {
			for (int j = 0; j < resource; j++) {
				need[i][j] = max[i][j] - allocation[i][j];
			}
		}
		System.out.println();
		/*
		 * 打印资源分配表
		 */
		System.out.println("资源分配表");
		System.out.println(" 进程                max\t\tallocation\t   need\t        available\t request");
		System.out.print("P0  ");
		for (int i = 0; i < resource; i++) {
			System.out.print(max[0][i] + "   ");
		}
		System.out.print(" 	");
		for (int i = 0; i < resource; i++) {
			System.out.print(allocation[0][i] + "   ");
		}
		System.out.print(" 	");
		for (int i = 0; i < resource; i++) {
			System.out.print(need[0][i] + "   ");
		}
		System.out.print("    ");
		for (int i = 0; i < resource; i++) {
			System.out.print(available[i] + "   ");
		}
		System.out.print("    ");
		for (int i = 0; i < resource; i++) {
			System.out.print(request[0][i] + "   ");
		}
		System.out.println();
		for (int i = 1; i < process; i++) {
			System.out.print("P" + i + "  ");
			for (int j = 0; j < resource; j++) {
				System.out.print(max[i][j] + "   ");
			}
			System.out.print(" 	");
			for (int j = 0; j < resource; j++) {
				System.out.print(allocation[i][j] + "   ");
			}
			System.out.print(" 	");
			for (int j = 0; j < resource; j++) {
				System.out.print(need[i][j] + "   ");
			}
			System.out.print("    ");
			for (int j = 0; j < resource; j++) {
				System.out.print(request[i][j] + "   ");
			}
			System.out.println();
		}
		/*
		 * 银行家算法
		 */
		for (int i = 0; i < process; i++) {
			for (int j = 0; j < resource; j++) {
				if (request[i][j] <= need[i][j]) {
					if (request[i][j] <= available[j]) {
						available[j] = available[j] - request[i][j];
						allocation[i][j] = allocation[i][j] + request[i][j];
						need[i][j] = need[i][j] - request[i][j];
						safe();
					} else
						System.out.println("尚无足够资源");
				} else
					System.out.println("所需资源数目超过它所宣布的最大值");
			}
		}
	}


	public static void safe() {
		/*
		 * 安全性算法
		 */


		int[] work = new int[3];// 表示系统可提供给进程运行所需的各类资源数目
		for (int i = 0; i < work.length; i++) {
			work[i] = available[i];// work:=Available
		}
		boolean[] finish = new boolean[process];// 是否有足够资源分配给进程使之运行完成
		for (int i = 0; i < process; i++) {
			finish[i] = false;// 初始化数组finish:=false
		}
		int[] array = new int[process];// 定义一个数组保存安全序列
		int num = 1;// 当前的进程
		int count1 = 1; // 当前放入array的进程编号
		while (num < process) {
			for (int i = 0; i < process; i++) {
				if (finish[i] == false) {
					if (need[i][0] <= work[0] && need[i][1] <= work[1]
							&& need[i][2] <= work[2]) {
						for (int j = 0; j < resource; j++) {
							work[j] = work[j] + allocation[i][j];// 如果需要的每个资源都少于可用资源则释放
						}
						finish[i] = true;// 当前进程标记为true,并把进程编号放入数组中
						array[count1 - 1] = i;
						count1++;
					}
				}
			}
			num++;
		}
		int count = 0;
		for (int i = 0; i < array.length; i++) {
			if (finish[i] == true) {
				count++;
			}
		}
		if (count == process) {
			System.out.println("存在一个安全序列:");
			for (int i = 0; i < array.length; i++) {
				System.out.print("P" + array[i] + " ");
			}
		} else
			System.out.println("系统处于不安全状态!");
	}


}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值