操作系统实验-银行家算法

操作系统实验报告(银行家算法)

1.实验目的

掌握银行家算法,加深对避免死锁的理解

2.实验内容及要求

实现下列要求,并写出实验报告:(题目、目的、内容和要求、实验原理、程序清单、运行情况(输入输出)、总结。)
系统中有进程P0、P1、P2、P3、P4,三种资源数量分别为A=10、B=5、C=7,T0时刻资源情况如下:
操作系统实验-银行家算法_第1张图片

(1)、分析T0时刻安全性,输出分析结果,若安全输出安全序列。
(1)、输入P1提出请求:Requestp1(1,0,2)用银行家算法分析这个请求是否可满足,输出分析结果。若可满足,输出系统安全序列。

实验源代码如下

package com.xiaole.os123.chap01;

import java.util.Scanner;

class Banker {
	private String processer[] = { "p0", "p1", "p2", "p3", "p4" };
	private int available[] = new int[3];
	private int allocation[][] = new int[5][3];
	private int need[][] = new int[5][3];
	private int work[] = new int[3];
	private int request[] = new int[3];
	private boolean finish[] = new boolean[5];
	private int safeSeries[] = new int[5];

	// private int request[][][];
	public void Input() {
		Scanner scanner = new Scanner(System.in);
		for (int i = 0; i < 5; i++) {
			System.out.println("请输入进程" + processer[i] + "已获得的三类资源个数:");
			for (int j = 0; j < 3; j++) {
				allocation[i][j] = scanner.nextInt();
			}
		}

		for (int i = 0; i < 5; i++) {
			System.out.println("请输入进程" + processer[i] + "还需要的三类资源个数:");
			for (int j = 0; j < 3; j++) {
				need[i][j] = scanner.nextInt();
			}
		}
		for (int i = 0; i < 3; i++) {
			System.out.println("请输入资源" + processer[i] + "的可用资源量:");
			available[i] = scanner.nextInt();
		}
	}

	public int[][] printAllocation() {
		System.out.println("Allocation:");
		for (int i = 0; i < 5; i++) {
			System.out.println();
			System.out.print("P"+i+": ");
			for (int j = 0; j < 3; j++) {
				System.out.print(allocation[i][j] + " ");
			}
		}
		return allocation;
	}

	public int[][] printNeed() {
		System.out.println();
		System.out.println("Need:");
		for (int i = 0; i < 5; i++) {
			System.out.println();
			System.out.print("P"+i+": ");
			for (int j = 0; j < 3; j++) {
				System.out.print(need[i][j] + " ");

			}
		}
		return need;
	}

	public int[] printAvailable() {
		System.out.println();
		System.out.println("Available:");
		for (int i = 0; i < 3; i++) {
			System.out.println("P" + i + ": " + available[i]);
		}
		return available;
	}

	public boolean isAllZero(int Kang) { // 判断一个进程资源是否全为零
		int num = 0;
		for (int i = 0; i < 3; i++) {
			if (need[Kang][i] == 0) {
				num++;
			}
		}
		if (num == 3) {
			return true;
		} else {
			return false;
		}
	}
	
	//打印安全检查信息
	public void SafeInfo(int work[], int i)
	{
	    int j;
	    
	    System.out.print("P"+i+"    ");
	    for(j = 0; j < 3; j++)
		{
	        System.out.print(work[j]+" ");
	    }   
	    System.out.print("  ");
	    for(j = 0; j < 3; j++)
		{
	        System.out.print(allocation[i][j]+" ");
	    }
		System.out.print("  ");
	    for(j = 0; j < 3; j++)
		{
	        System.out.print(need[i][j]+" ");
	    }
	    System.out.print("  ");
	    for(j = 0; j < 3; j++)
		{
	        System.out.print(allocation[i][j]+work[j]+" ");
	    }
	    System.out.println();
	}

	public boolean SecurityAlgorithm() { // 安全性算法
		// int resourceNumFinish = 0;
		int safeIndex = 0;
		int allFinish = 0;
		int r = 0;
		int temp = 0;
		int pNum = 0;
		int num = 0;
		// 预分配为了保护available[]
		for (int i = 0; i < 3; i++) {
			work[i] = available[i];
		}
		// 把未完成进程置为false
		for (int i = 0; i < 5; i++) {
			boolean result = isAllZero(i);
			if (result == true) {
				finish[i] = true;
				allFinish++;
			} else {
				finish[i] = false;
			}

		}
		// 预分配开始
		System.out.println("PID   "+"Work  "+"Allocation  "+"Need  "+"Work+Allocation  ");
		while (allFinish != 5) {
			num = 0;
			for (int i = 0; i < 3; i++) {
				if (need[r][i] <= work[i] && finish[r] == false) {
					num++;
				}
			}
			if (num == 3) {
				for (int i = 0; i < 3; i++) {
					work[i] = work[i] + allocation[r][i];
				}
				allFinish++;
				SafeInfo(work,r);
				safeSeries[safeIndex] = r;
				safeIndex++;
				finish[r] = true;
			}
			r++;
			if (r >= 5) {
				r = r % 5;
				if (temp == allFinish) {
					break;
				}
				temp = allFinish;
			}
			pNum = allFinish;
		}
		// 判断系统是否安全
		for (int i = 0; i < 5; i++) {
			if (finish[i] == false) {
				System.out.print("\n当前系统不安全!故不分配资源");
				return false;
			}
		}
		// 打印安全序列
		System.out.println("当前系统安全!\n安全序列为:");
		for (int i = 0; i < 5; i++) {
			boolean result = isAllZero(i);
			if (result == true) {
				pNum--;
			}
		}
		for (int i = 0; i < pNum; i++) {
			System.out.print("P" + safeSeries[i] + "->");
		}
		return true;
	}

	public void setRequest() {
			Scanner scanner = new Scanner(System.in);
			System.out.println();
			System.out.println("请输入要分配资源的进程(0-5):");
			int curProcess = scanner.nextInt();
			System.out.println("请输入请求的三种资源的个数");
			for (int i = 0; i < 3; i++) {
				request[i] = scanner.nextInt();
			}
			int num = 0;
			for (int i = 0; i < 3; i++) {
				if (request[i] <= need[curProcess][i] && request[i] <= available[i]) {
					num++;
				} else {
					System.out.println("发生错误!可能原因如下:(1)您请求分配的资源可能打于该进程的某些资源的最大需求(2)系统所剩的资源已不足");
					break;
				}
			}
			if (num == 3) {
				num = 0;
				for (int i = 0; i < 3; i++) {
					// 分配资源
					available[i] = available[i] - request[i];
					allocation[curProcess][i] = allocation[curProcess][i] + request[i];
					need[curProcess][i] = need[curProcess][i] - request[i];
					// 记录分配以后,是否该进程需要值为0了
					if (need[curProcess][i] == 0) {
						num++;
					}
				}
				// 预分配完成以后,判断该系统是否安全,若安全,则可继续进行分配,若不安全,将已经分配的资源换回来
				if (!SecurityAlgorithm()) {
					for (int j = 0; j < 3; j++) {
						available[j] = available[j] + request[j];
						allocation[curProcess][j] = allocation[curProcess][j] - request[j];
						need[curProcess][j] = need[curProcess][j] + request[j];
					}
					System.out.println("资源不足,等待中...\n分配失败!");
				}
			}
			scanner.close();
		}
	}

public class Demo1 {

	public static void main(String[] args) {
		Banker bank = new Banker();
		bank.Input();
		bank.printAllocation();
		bank.printNeed();
		bank.printAvailable();
		bank.SecurityAlgorithm();
		bank.setRequest();
	}
}

实验截图
操作系统实验-银行家算法_第2张图片
有欠妥当的地方还望大家多多斧正

你可能感兴趣的:(操作系统实验)