天天看點

java實作銀行家算法

南昌航空大學實驗報告

課程名稱:  作業系統       實驗名稱:  _避免死鎖(銀行家算法)

班級:110462          姓名:XXX    學号:  11046208

一 實驗目的

      1.    加深對死鎖概念的了解。

2. 能夠利用銀行家算法,有效避免死鎖的發生,或檢測死鎖的存在。

二 實驗内容

實驗要求用進階語言編寫和調試一個簡單的銀行家算法程式。加深了解有關資源申請、避免死鎖等概念,并體會和了解死鎖和避免死鎖的具體實施方法。

1.       設計程序對各類資源最大申請表示及初值确定。

2.       設定系統提供資源初始狀況。

3.       設定每次某個程序對各類資源的申請表示。

4.       編制程式,依據銀行家算法,決定其申請是否得到滿足。

三 實驗分析

1.     設計程式類Bank,在其中設計兩個類:Sources資源類和Processo類用于管理資源和程序的排程,

                      i.             Bank擁有一個系統字段和可配置設定資源字段allSources

                    ii.             擁有一個配置設定資源的方法:Assign(),将程序的資源請求進行處理

                 iii.             擁有一個檢查資源配置設定後,是否能安全運作的checkSafeQueue方法

2.    在類Sources中主要設計的是三個字段,用于标志A,B,C類,這三類資源

3.    在類Processor中 設計有三個字段

                       i.             字段name屬于String類,用與标記程序的名字

                     ii.             字段maxSources屬于Sources類,用于說明程式的最大資源占用

                   iii.             字段allSources屬于Sources類,用于說明目前程式已經配置設定有的内容

                    iv.             字段needSources屬于Sources類,用于說明目前程式最多還能申請的最大資源

4.      程式最後設計了一個test()方法,用與測試程式的狀态

測試函數對參數進行了初始化,并設計了從鍵盤輸入進行檢測資料的正确性,資料請看實驗結果

四 實驗程式設計

package com.rainplus.banker;

import java.util.Scanner;


public class Bank {
	// private Sources sysSources = new Sources(10, 5, 7);
	private Sources allSources = new Sources(3, 3, 2);// TODO

	// 資源類假設一共三類資源;
	class Sources {
		private int A;
		private int B;
		private int C;

		public Sources(int B, int A, int C) {
			super();
			this.B = B;
			this.A = A;
			this.C = C;
		}
		public Sources(Sources sources) {
			super();
			this.B = sources.getB();
			this.A = sources.getA();
			this.C = sources.getC();
		}
		public int getB() {
			return B;
		}

		public void setB(int B) {
			this.B = B;
		}

		public int getA() {
			return A;
		}

		public void setA(int A) {
			this.A = A;
		}

		public int getC() {
			return C;
		}

		public void setC(int C) {
			this.C = C;
		}
	}

	// 程序類,包程序使用最大記憶體,目前已配置設定記憶體,和需要配置設定記憶體
	class Processor {
		private String name;
		private Sources maxSources;
		private Sources allSources;
		private Sources needSources;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Sources getMaxSources() {
			return maxSources;
		}

		public void setMaxSources(Sources maxSources) {
			this.maxSources = maxSources;
		}

		public Sources getNeedSources() {
			return needSources;
		}

		public void setNeedSources(Sources needSources) {
			this.needSources = needSources;
		}

		public Sources getAllSources() {
			return allSources;
		}

		public void setAllSources(Sources allSources) {
			this.allSources = allSources;
		}

	}

	// 顯示目前系統和各個程序的資源使用情況
	public void showdata(Processor[] processors) {
		// 顯示目前可用資源
		System.out.print("目前系統可配置設定資源為:");
		showSources(allSources);
		System.out.println("-----------------程序狀态-------------");
		System.out.println("程序号      Max    Allocation      Need   ");
		System.out.println("     A B C     A B C       A B C");
		for (int i = 0; i < processors.length; i++) {
			System.out.print(processors[i].getName() + "   "
					+ processors[i].getMaxSources().getA() + " "
					+ processors[i].getMaxSources().getB() + " "
					+ processors[i].getMaxSources().getC() + "     ");
			System.out.print(processors[i].getAllSources().getA() + " "
					+ processors[i].getAllSources().getB() + " "
					+ processors[i].getAllSources().getC() + "       ");
			System.out.println(processors[i].getNeedSources().getA() + " "
					+ processors[i].getNeedSources().getB() + " "
					+ processors[i].getNeedSources().getC() + " ");
		}
		System.out.println("-----------------------------------");
	}

	// 顯示資源的資料
	public void showSources(Sources sources) {
		System.out.println("A:" + sources.getA() + " B:" + sources.getB()
				+ " C:" + sources.getC());
	}

	public boolean assign(Processor[] processors, int requestNum, Sources req) {
		Processor pro = processors[requestNum];
		if (!(req.getA() <= pro.getNeedSources().getA()
				&& req.getB() <= pro.getNeedSources().getB() && req.getC() <= pro
				.getNeedSources().getC())) {
			System.out.println("請求的資源數超過了所需要的最大值,配置設定錯誤");
			return false;
		}
		if (!(req.getA() <= allSources.getA()
				&& req.getB() <= allSources.getB() && req.getC() <= allSources
				.getC())) {
			System.out.println("尚無足夠資源配置設定,必須等待");
			return false;
		}
		// 配置設定資源
		allSources.setA(allSources.getA() - req.getA());
		allSources.setB(allSources.getB() - req.getB());
		allSources.setC(allSources.getC() - req.getC());
		pro.getAllSources().setA(pro.getAllSources().getA() + req.getA());
		pro.getAllSources().setB(pro.getAllSources().getB() + req.getB());
		pro.getAllSources().setC(pro.getAllSources().getC() + req.getC());
		pro.getNeedSources().setA(pro.getNeedSources().getA() - req.getA());
		pro.getNeedSources().setB(pro.getNeedSources().getB() - req.getB());
		pro.getNeedSources().setC(pro.getNeedSources().getC() - req.getC());
		boolean flag = checkSafeQueue(processors, allSources);// 進行安全性檢查并傳回是否安全
		if (flag == true) {
			System.out.println("能夠安全配置設定");
			return true;
		} else {
			System.out.println("不能夠安全配置設定");
			return false;
		}
	}

	public boolean checkSafeQueue(Processor[] pros, Sources all) {
		Sources temp = new Sources(all);
		
		boolean assigned[] = new boolean[5];
		int i = 0;
		while (i < 5) {
			if (!assigned[i] && temp.getA() >= pros[i].getNeedSources().getA()
					&& temp.getB() >= pros[i].getNeedSources().getB()
					&& temp.getC() >= pros[i].getNeedSources().getC()) {
				temp.setA(temp.getA() + pros[i].getAllSources().getA());
				temp.setB(temp.getB() + pros[i].getAllSources().getB());
				temp.setC(temp.getC() + pros[i].getAllSources().getC());
				System.out.println("配置設定成功的是:" + pros[i].getName());
				assigned[i] = true;
				i = 0;
			} else {
				i++;
			}
		}
		for (i = 0; i < 5; i++) {
			if (assigned[i] == false)
				return false;
		}
		return true;
	}

	public void test() {
		int processNum = 5;
		// 初始化程序資料
		Sources[] all = new Sources[processNum];
		Sources[] max = new Sources[processNum];
		Sources[] need = new Sources[processNum];
		int[][] allData = { { 0, 1, 0 }, { 2, 0, 0 }, { 3, 0, 2 }, { 2, 1, 1 },
				{ 0, 0, 2 } };
		int[][] maxData = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 }, { 2, 2, 2 },
				{ 4, 3, 3 } };
		int[][] needData = { { 7, 4, 3 }, { 1, 2, 2 }, { 6, 0, 0 },
				{ 0, 1, 1 }, { 4, 3, 1 } };
		for (int i = 0; i < processNum; i++) {
			all[i] = new Sources(allData[i][0], allData[i][1], allData[i][2]);
			max[i] = new Sources(maxData[i][0], maxData[i][1], maxData[i][2]);
			need[i] = new Sources(needData[i][0], needData[i][1],
					needData[i][2]);
		}

		Processor[] processors = new Processor[processNum];
		for (int i = 0; i < processors.length; i++) {
			processors[i] = new Processor();
			processors[i].setName("P" + i);
			processors[i].setMaxSources(max[i]);
			processors[i].setAllSources(all[i]);
			processors[i].setNeedSources(need[i]);
		}
		showdata(processors);

		Scanner s = new Scanner(System.in);
		int requestNum = 0;
		int[] input = { 0, 0, 0 };
		Sources requestSources = new Sources(0, 0, 0);
		while (true)// 循環進行配置設定
		{
			System.out.println("請輸入要請求的程序号(0--4):");
			requestNum = s.nextInt();
			System.out.println("請輸入請求的資源數目:");
			for (int i = 0; i < 3; i++) {
				input[i] = s.nextInt();
			}
			requestSources.setA(input[0]);
			requestSources.setB(input[1]);
			requestSources.setC(input[2]);
			assign(processors, requestNum, requestSources);
			showdata(processors);
		}
	}

	public static void main(String[] args) {
		new Bank().test();
	}
}
           

五 實驗結果

1.  正确安全配置設定:

目前系統可配置設定資源為:A:3B:3 C:2

-----------------程序狀态-------------

程序号      Max   Allocation      Need  

     AB C     A B C       A B C

P0   57 3     1 0 0       4 7 3

P1   23 2     0 2 0       2 1 2

P2   09 2     0 3 2       0 6 0

P3   22 2     1 2 1       1 0 1

P4   34 3     0 0 2       3 4 1

-----------------------------------

請輸入要請求的程序号(0--4):1

請輸入請求的資源數目:102

配置設定成功的是:P1

配置設定成功的是:P3

配置設定成功的是:P0

配置設定成功的是:P2

配置設定成功的是:P4

能夠安全配置設定

目前系統可配置設定資源為:A:2B:3 C:0

-----------------程序狀态-------------

程序号      Max   Allocation      Need  

     AB C     A B C       A B C

P0   57 3     1 0 0       4 7 3

P1   23 2     1 2 2       1 1 0

P2   09 2     0 3 2       0 6 0

P3   22 2     1 2 1       1 0 1

P4   34 3     0 0 2       3 4 1

-----------------------------------

請輸入要請求的程序号(0--4):

 2.不能安全運作:

目前系統可配置設定資源為:A:3B:3 C:2

-----------------程序狀态-------------

程序号      Max   Allocation      Need  

     AB C     A B C       A B C

P0   57 3     1 0 0       4 7 3

P1   23 2     0 2 0       2 1 2

P2   09 2     0 3 2       0 6 0

P3   22 2     1 2 1       1 0 1

P4   34 3     0 0 2       3 4 1

-----------------------------------

請輸入要請求的程序号(0--4):4

請輸入請求的資源數目:3 3 1

不能夠安全配置設定

目前系統可配置設定資源為:A:0B:0 C:1

-----------------程序狀态-------------

程序号      Max   Allocation      Need  

     AB C     A B C       A B C

P0   57 3     1 0 0       4 7 3

P1   23 2     0 2 0       2 1 2

P2   09 2     0 3 2       0 6 0

P3   22 2     1 2 1       1 0 1

P4   34 3     3 3 3       0 1 0

-----------------------------------

請輸入要請求的程序号(0--4):

六 實驗體會

加深了對死鎖概念的了解,能夠利用銀行家算法,有效避免死鎖的發生,或檢測死鎖的存在。對程序之間的運作情況有了更加深層次的了解

繼續閱讀