2020-11-14:银行家算法(Java)——操作系统

操作系统——银行家算法

  • 1 实验目的:
  • 2 实验内容:
  • 3 源代码:
  • 4 测试数据:(部分的主要数据)
  • 5 运行结果


1 实验目的:

银行家算法是避免死锁的一种重要方法,本实验要求用高级语言编写和调试一个简单的银行家算法程序。加深了解有关资源申请、避免死锁等概念,并体会和了解死锁和避免死锁的具体实施方法。

2 实验内容:

1)设计进程对各类资源最大申请表示及初值确定。
2)设定系统提供资源初始状况。
3)设定每次某个进程对各类资源的申请表示。
4)编制程序,依据银行家算法,决定其申请是否得到满足。

3 源代码:

银行家算法(Java)完整代码
下面是实现框架

import java.util.Scanner;

//银行家算法的数据结构
class Banker{
	
	//Map P_name = new HashMap();	//进程名
	//Map S_name = new HashMap();	//资源名
	int ID[],			//进程代号
		M,				//m个进程
		N,				//n类资源
		All[],			//系统各资源数量
		Max[][],		//m个进程对n类资源的最大需求量
		Allocation[][],	//m个进程已经得到n类资源的资源量
		Need[][],		//m个进程还需要n类资源的资源量
		Available[][];	//系统可用资源数
		
	boolean Finish[];	//标记进程是否完成
	int a=0;//Available的第一个下标

	public Banker() {	//无参构造函数
	
		//代码空缺

	}
	
	public Banker(int[] iD, int m, int n, int[] all, int[][] max, int[][] allocation, int[][] need, int[][] available,
			boolean[] finish, int a) {
		super();
		ID = iD;
		M = m;
		N = n;
		All = all;
		Max = max;
		Allocation = allocation;
		Need = need;
		Available = available;
		Finish = finish;
		this.a = a;
	}

	private void Need_Resources() {
	
		//代码空缺

	}

	private void Available_Resources() {
	
		//代码空缺

	}

	private void Print_Banker() {
	
		//代码空缺

	}

	public void Security_examine(){
	
		//代码空缺

	}
	
	public void Reallocation(){//申请资源
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		int[] Request = new int[N];
		boolean flag = true;
		
		System.out.print("\n输入进程代号:");
		int n = input.nextInt();
		System.out.print("输入请求资源数:");
		for(int j=0; j<N; j++)
			Request[j] = input.nextInt();
		
		for(int i=0; i<N; i++)//合理性检查,可用性检查
		{
			if(Request[i] > Need[n][i] || Request[i] > Available[0][i])
				flag = false;
		}
		if(flag)
		{
			for(int i=0; i<N; i++)
			{
				Allocation[n][i] += Request[i];
			}
			
			Init();
			Print_Banker();
			
		}
		else
		{
			System.out.println("分配不安全\n");
		}
		
	}

	private void Init() {
	
		//代码空缺


	}

	private void Print_Banker_Se() {
		System.out.print("\n进程\t Work\tNeed\tAllocation\tWork+Allocation\tFinish\n");
	}
	
	
}

public class OS_banker5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Banker banker = new Banker();

		int n;
		boolean flag=true;
		Scanner input = new Scanner(System.in);
		//input.nextLine();
		System.out.println("\n*************菜单****************");
		System.out.println("进行安全性检查:1\n请求再分配资源:2\n退出:其他");
		System.out.println("*********************************");
		while(flag)
		{
			System.out.print("输入操作代号:");
			n = input.nextInt();
			switch(n)
			{
				case 1:	banker.Security_examine();break;
				case 2:	banker.Reallocation();break;
				default:flag = false;System.out.print("\n操作结束!!!!");
			}
		}

		input.close();

	}

}

4 测试数据:(部分的主要数据)

例1:(主要数据)

初始化:
5
3
10 5 7

7 5 3
3 2 2
9 0 2
2 2 2
4 3 3

0 1 0
2 0 0
3 0 2
2 1 1
0 0 2

再分配:
1
1 0 2

4
3 3 0

0
0 2 0

例2:(主要数据)

初始化:
3
3
10 8 7
8 7 5
5 2 5
6 6 2

3 2 0
2 0 2
1 3 2

再分配:
0
1 0 0

1
0 1 1

5 运行结果

运行结果1:(书上的例子)

T0时刻资源分配图:
资源	资源数量
S0	10
S1	5
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	7 5 3 	0 1 0 		7 4 3 	3 3 2 		false
P1	3 2 2 	2 0 0 		1 2 2 			false
P2	9 0 2 	3 0 2 		6 0 0 			false
P3	2 2 2 	2 1 1 		0 1 1 			false
P4	4 3 3 	0 0 2 		4 3 1 			false

*************菜单****************
进行安全性检查:1
请求再分配资源:2
退出:其他
*********************************
输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	3 3 2 	1 2 2 	2 0 0 		5 3 2 		true
P3	5 3 2 	0 1 1 	2 1 1 		7 4 3 		true
P4	7 4 3 	4 3 1 	0 0 2 		7 4 5 		true
P0	7 4 5 	7 4 3 	0 1 0 		7 5 5 		true
P2	7 5 5 	6 0 0 	3 0 2 		10 5 7 		true

安全序列:
->P1->P3->P4->P0->P2

输入操作代号:2

输入进程代号:1
输入请求资源数:1 0 2

T0时刻资源分配图:
资源	资源数量
S0	10
S1	5
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	7 5 3 	0 1 0 		7 4 3 	2 3 0 		false
P1	3 2 2 	3 0 2 		0 2 0 			false
P2	9 0 2 	3 0 2 		6 0 0 			false
P3	2 2 2 	2 1 1 		0 1 1 			false
P4	4 3 3 	0 0 2 		4 3 1 			false
输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	2 3 0 	0 2 0 	3 0 2 		5 3 2 		true
P3	5 3 2 	0 1 1 	2 1 1 		7 4 3 		true
P4	7 4 3 	4 3 1 	0 0 2 		7 4 5 		true
P0	7 4 5 	7 4 3 	0 1 0 		7 5 5 		true
P2	7 5 5 	6 0 0 	3 0 2 		10 5 7 		true

安全序列:
->P1->P3->P4->P0->P2

输入操作代号:2

输入进程代号:4
输入请求资源数:3 3 0
分配不安全

输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	2 3 0 	0 2 0 	3 0 2 		5 3 2 		true
P3	5 3 2 	0 1 1 	2 1 1 		7 4 3 		true
P4	7 4 3 	4 3 1 	0 0 2 		7 4 5 		true
P0	7 4 5 	7 4 3 	0 1 0 		7 5 5 		true
P2	7 5 5 	6 0 0 	3 0 2 		10 5 7 		true

安全序列:
->P1->P3->P4->P0->P2

输入操作代号:2

输入进程代号:0
输入请求资源数:0 2 0

T0时刻资源分配图:
资源	资源数量
S0	10
S1	5
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	7 5 3 	0 3 0 		7 2 3 	2 1 0 		false
P1	3 2 2 	3 0 2 		0 2 0 			false
P2	9 0 2 	3 0 2 		6 0 0 			false
P3	2 2 2 	2 1 1 		0 1 1 			false
P4	4 3 3 	0 0 2 		4 3 1 			false
输入操作代号:1

T0时刻的安全性:
系统不安全
输入操作代号:4

操作结束!!!!

运行结果2:(实验报告上的例子)

T0时刻资源分配图:
资源	资源数量
S0	10
S1	8
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	8 7 5 	3 2 0 		5 5 5 	4 3 3 		false
P1	5 2 5 	2 0 2 		3 2 3 			false
P2	6 6 2 	1 3 2 		5 3 0 			false

*************菜单****************
进行安全性检查:1
请求再分配资源:2
退出:其他
*********************************
输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	4 3 3 	3 2 3 	2 0 2 		6 3 5 		true
P2	6 3 5 	5 3 0 	1 3 2 		7 6 7 		true
P0	7 6 7 	5 5 5 	3 2 0 		10 8 7 		true

安全序列:
->P1->P2->P0

输入操作代号:2

输入进程代号:0
输入请求资源数:1 0 0

T0时刻资源分配图:
资源	资源数量
S0	10
S1	8
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	8 7 5 	4 2 0 		4 5 5 	3 3 3 		false
P1	5 2 5 	2 0 2 		3 2 3 			false
P2	6 6 2 	1 3 2 		5 3 0 			false
输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	3 3 3 	3 2 3 	2 0 2 		5 3 5 		true
P2	5 3 5 	5 3 0 	1 3 2 		6 6 7 		true
P0	6 6 7 	4 5 5 	4 2 0 		10 8 7 		true

安全序列:
->P1->P2->P0

输入操作代号:2

输入进程代号:1
输入请求资源数:0 1 1

T0时刻资源分配图:
资源	资源数量
S0	10
S1	8
S2	7

进程	 Max	Allocation	Need	Available	Finish
P0	8 7 5 	4 2 0 		4 5 5 	3 2 2 		false
P1	5 2 5 	2 1 3 		3 1 2 			false
P2	6 6 2 	1 3 2 		5 3 0 			false
输入操作代号:1

T0时刻的安全性:

进程	 Work	Need	Allocation	Work+Allocation	Finish
P1	3 2 2 	3 1 2 	2 1 3 		5 3 5 		true
P2	5 3 5 	5 3 0 	1 3 2 		6 6 7 		true
P0	6 6 7 	4 5 5 	4 2 0 		10 8 7 		true

安全序列:
->P1->P2->P0

输入操作代号:5

操作结束!!!!

你可能感兴趣的:(实验报告,java,操作系统,算法)