银行家算法 Java实现

广东工业大学 操作系统实验

实验原理

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实现

import java.util.LinkedHashSet;
import java.util.Scanner;

public class Banker
{
	int m; // 资源种类数
	int n; // 进程数

	int[] available;
	int[][] max;
	int[][] allocation;
	int[][] need;
	int[] request;

	Scanner scan = new Scanner(System.in);

	/**
	 * 初始化
	 */
	void init()
	{
		System.out.print("请输入资源种类数:");
		m = scan.nextInt();

		System.out.print("请输入进程数:");
		n = scan.nextInt();

		available = new int[m];
		max = new int[n][m];
		allocation = new int[n][m];
		need = new int[n][m];
		request = new int[m];

		System.out.println();

		System.out.println("请输入资源情况:");

		for (int j = 0; j < m; j++)
		{
			System.out.print("资源" + (char) (j + 65) + "的可利用情况:");
			available[j] = scan.nextInt();
		}

		System.out.println();

		for (int i = 0; i < n; i++)
		{
			System.out.println("进程P" + i + "的资源情况:");

			for (int j = 0; j < m; j++)
			{
				System.out.print("资源" + (char) (j + 65) + "的最大需求情况:");
				max[i][j] = scan.nextInt();
			}

			for (int j = 0; j < m; j++)
			{
				System.out.print("资源" + (char) (j + 65) + "的分配情况:");
				allocation[i][j] = scan.nextInt();
			}

			for (int j = 0; j < m; j++)
			{
				need[i][j] = max[i][j] - allocation[i][j];
			}

			System.out.println();
		}
	}

	/**
	 * 银行家算法
	 * 
	 * @return 算法是否成功执行
	 */
	boolean banker()
	{
		System.out.print("请输入发出请求的进程:P");
		int i = scan.nextInt();

		System.out.println();

		int[] av = new int[m];
		int[] al = new int[m];
		int[] ne = new int[m];

		System.out.println("输入请求向量的值:");
		for (int j = 0; j < m; j++)
		{
			System.out.print("资源" + (char) (j + 65) + "的请求情况:");
			request[j] = scan.nextInt();

			if (request[j] <= need[i][j])
			{
				if (request[j] <= available[j])
				{
					av[j] = available[j];
					al[j] = allocation[i][j];
					ne[j] = need[i][j];

					available[j] -= request[j];
					allocation[i][j] += request[j];
					need[i][j] -= request[j];
				} else
				{
					System.out.println("\n尚无足够资源,P" + i + "需等待!\n");
					return false;
				}
			} else
			{
				System.out.println("\n输入错误!需要的资源数已超过宣布的最大值!\n");
				return false;
			}
		}

		if (safety() == false) // 安全性检测
		{
			for (int j = 0; j < m; j++)
			{
				available[j] = av[j];
				allocation[i][j] = al[j];
				need[i][j] = ne[j];
			}

			System.out.println("\n系统处于不安全状态,P" + i + "需等待!\n");

			return false;
		}

		System.out.println("\n完成分配!\n");

		System.out.println("资源分配表:");
		System.out.println("Max\tAllocation\tNeed\tAvailable");

		for (i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				System.out.print(max[i][j] + " ");
			}
			System.out.print("\t");

			for (int j = 0; j < m; j++)
			{
				System.out.print(allocation[i][j] + " ");
			}
			System.out.print("\t\t");

			for (int j = 0; j < m; j++)
			{
				System.out.print(need[i][j] + " ");
			}
			System.out.print("\t");

			if (i == 0)
			{
				for (int j = 0; j < m; j++)
				{
					System.out.print(available[j] + " ");
				}
			}

			System.out.println();
		}

		System.out.println();

		return true;
	}

	/**
	 * 安全性算法
	 * 
	 * @return 系统是否处于安全状态
	 */
	boolean safety()
	{
		int[] work = new int[m];
		boolean[] finish = new boolean[n];

		for (int j = 0; j < m; j++)
			work[j] = available[j];

		for (int i = 0; i < n; i++)
			finish[i] = false;

		LinkedHashSet<Integer> set = new LinkedHashSet<Integer>();

		while (true)
		{
			boolean result = true;

			for (boolean f : finish)
			{
				result &= f;
			}

			if (result == false)
			{
				int index = findProgress(work, finish);

				if (index != -1)
				{
					set.add(index);
				} else
				{
					return false;
				}
			} else
				break;
		}

		System.out.print("\n安全序列为:");
		for (int i : set)
		{
			System.out.print("P" + i + " ");
		}
		System.out.println();

		return true;
	}

	/**
	 * 寻找符合条件的进程
	 * 
	 * @param work   工作向量
	 * @param finish 系统是否有足够的资源分配给进程
	 * @return 进程号
	 */
	int findProgress(int[] work, boolean[] finish)
	{
		int index = -1;

		pro: for (int i = 0; i < n; i++)
		{
			if (finish[i] == false)
			{
				for (int j = 0; j < m; j++)
				{
					if (need[i][j] > work[j])
						continue pro;
					if (j == m - 1 && need[i][j] <= work[j])
					{
						index = i;

						finish[i] = true;

						j = 0;

						for (; j < m; j++)
						{
							work[j] = work[j] + allocation[i][j];
						}

						break pro;
					}
				}
			}
		}

		return index;
	}

	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		Banker b = new Banker();

		int i = 1;

		b.init();
		while (true)
		{
			System.out.print("请选择您要执行的操作:1-请求资源 其他-退出 ");
			i = scan.nextInt();
			if (i != 1)
			{
				System.out.println("\n程序已退出!");
				break;
			}
			b.banker();
		}

		scan.close();
	}
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行家算法是一种避免死锁的算法,它通过动态地分配资源来避免进程因争夺资源而产生死锁。下面是银行家算法Java实现: ```java import java.util.Scanner; public class BankerAlgorithm { private int[][] need; // 需求矩阵 private int[][] allocation; // 分配矩阵 private int[][] max; // 最大需求矩阵 private int[] available; // 可用资源向量 private int processNum; // 进程数 private int resourceNum; // 资源数 public BankerAlgorithm(int[][] need, int[][] allocation, int[][] max, int[] available) { this.need = need; this.allocation = allocation; this.max = max; this.available = available; this.processNum = allocation.length; this.resourceNum = available.length; } // 判断是否满足需求 private boolean check(int[] work, boolean[] finish, int[] need, int[] allocation) { for (int i = 0; i < resourceNum; i++) { if (need[i] > work[i]) { return false; } } for (int i = 0; i < resourceNum; i++) { work[i] += allocation[i]; } finish[processNum] = true; return true; } // 执行银行家算法 public boolean execute() { boolean[] finish = new boolean[processNum]; int[] work = new int[resourceNum]; for (int i = 0; i < resourceNum; i++) { work[i] = available[i]; } int count = 0; while (count < processNum) { boolean flag = false; for (int i = 0; i < processNum; i++) { if (!finish[i] && check(work, finish, need[i], allocation[i])) { flag = true; count++; } } if (!flag) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入进程数:"); int processNum = scanner.nextInt(); System.out.print("请输入资源数:"); int resourceNum = scanner.nextInt(); int[][] max = new int[processNum][resourceNum]; int[][] allocation = new int[processNum][resourceNum]; int[][] need = new int[processNum][resourceNum]; int[] available = new int[resourceNum]; System.out.println("请输入最大需求矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { max[i][j] = scanner.nextInt(); } } System.out.println("请输入分配矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { allocation[i][j] = scanner.nextInt(); need[i][j] = max[i][j] - allocation[i][j]; } } System.out.println("请输入可用资源向量:"); for (int i = 0; i < resourceNum; i++) { available[i] = scanner.nextInt(); } BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(need, allocation, max, available); if (bankerAlgorithm.execute()) { System.out.println("系统是安全的!"); } else { System.out.println("系统是不安全的!"); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值