操作系统实验:实验六 银行家算法的模拟与实现
1、实验目的
(1) 进一步理解进程的并发执行。
(2) 加强对进程死锁的理解,理解安全状态与不安全状态的概念。
(3) 掌握使用银行家算法避免死锁问题。
2、实验基本知识及原理
(1)基本概念
死锁:多个进程在执行过程中,因为竞争资源会造成相互等待的局面。如果没有外力作用,这
些进程将永远无法向前推进。此时称系统处于死锁状态或者系统产生了死锁。
安全序列:系统按某种顺序并发进程,并使它们都能达到获得最大资源而顺序完成的序列为安
全序列。
安全状态:能找到安全序列的状态称为安全状态,安全状态不会导致死锁。
不安全状态:在当前状态下不存在安全序列,则系统处于不安全状态。
(2)银行家算法
银行家算法顾名思义是来源于银行的借贷业务,一定数量的本金要满足多个客户的借贷周转,
为了防止银行家资金无法周转而倒闭,对每一笔贷款,必须考察其是否能限期归还。
在操作系统中研究资源分配策略时也有类似问题,系统中有限的资源要供多个进程使用,必须
保证得到的资源的进程能在有限的时间内归还资源,以供其它进程使用资源。如果资源分配不当,
就会发生进程循环等待资源,则进程都无法继续执行下去的死锁现象。
当一进程提出资源申请时,银行家算法执行下列步骤以决定是否向其分配资源:
1)检查该进程所需要的资源是否已超过它所宣布的最大值。
2)检查系统当前是否有足够资源满足该进程的请求。
3)系统试探着将资源分配给该进程,得到一个新状态。
4)执行安全性算法,若该新状态是安全的,则分配完成;若新状态是不安全的,则恢复原状
态,阻塞该进程。
3**、实验内容**
本实验的内容是要通过编写和调试一个模拟系统动态分配资源的银行家算法程序,有效地避免
死锁发生。具体要求如下:
(1) 初始化时让系统拥有一定的资源;
(2) 用键盘输入的方式允许进程动态申请资源;
(3) 如果试探分配后系统处于安全状态,则修改系统的资源分配情况,正式分配资源;
(4) 如果试探分配后系统处于不安全状态,则提示不能满足请求,恢复原状态并阻塞该进程。
代码:算法类
public class bank {
int Available[] = new int[]{3, 4, 5};//可用资源
int Claim[][] = new int[][]{{4, 2, 7}, {2, 3, 4}, {1, 5, 6}, {1, 2, 2}};//进程需要的资源
int Allocation[][] = new int[][]{{1, 1, 0}, {0, 1, 0}, {2, 0, 3}, {0, 0, 1}};//已分配的资源
int max[][] = new int[][]{{5, 3, 7}, {2, 4, 4}, {3, 5, 9}, {1, 2, 3}};//进程需要的资源总数
public void showall() {
System.out.println("进程号 0 1 2 3 ");
System.out.println("资源种类 A B C A B C A B C A B C");
System.out.print("max ");
for (int[] ints : max) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.print(" ");
}
System.out.println("");
System.out.print("Allocation ");
for (int[] allocation : Allocation) {
for (int anAllo : allocation) {
System.out.print(anAllo + " ");
}
System.out.print(" ");
}
System.out.println("");
System.out.print("Claim ");
for (int[] ints : Claim) {
for (int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.print(" ");
}
System.out.println("");
}//展示数据
public boolean CheckResource(int processId, int resources[]) {
for (int i = 0; i < 3; i++) {
if (Claim[processId][i] < resources[i]) {
System.out.println("所需资源超过所宣布最大需求资源数,分配错误!");
return false;
}
}
for (int i = 0; i < 3; i++) {
if (resources[i] > Available[i]) {
System.out.println("尚无足够资源可分配,请等待");
return false;
}
}
return true;
}//判断需求是否符合所宣布的最大需求,检查资源是否足够
public void ChangeDate(int processId, int[] resources) {
for (int i = 0; i < 3; i++) {
Available[i] = Available[i] - resources[i];
Claim[processId][i] = Claim[processId][i] - resources[i];
Allocation[processId][i] = Allocation[processId][i] + resources[i];
}//资源分配
int p = 1;
for (int i = 0; i < 3; i++) {
if (Allocation[processId][i] != max[processId][i]) {
p = 0;
break;
}
}//资源全部分配完成
if (p != 0) {
for (int i = 0; i < 3; i++) {
Available[i] = Available[i] + Allocation[processId][i];
}//释放资源
}
boolean flag = SafeCheck(Available[0], Available[1], Available[2], processId);
if (flag) {
System.out.println("能够安全分配!");
} else {
System.out.println("不能安全分配!");
for (int i = 0; i < 3; i++) {
Available[i] = Available[i] - Allocation[processId][i];
Allocation[processId][i] = Allocation[processId][i] - resources[i];
Claim[processId][i] = Claim[processId][i] + resources[i];
Available[i] = Available[i] + resources[i];
}
}
}//分配资源
public boolean SafeCheck(int a, int b, int c, int processId) {//安全性检查
int[] able = new int[3];
able[0] = a;
able[1] = b;
able[2] = c;
int i = 0;
boolean[] finish = new boolean[4];
if (Claim[processId][0] == 0 && Claim[processId][1] == 0 && Claim[processId][2] == 0) {
finish[processId] = true;
System.out.println("进程" + processId + "分配成功!");
}
while (i < 4) {
if (!finish[i] && able[0] >= Claim[i][0] && able[1] >= Claim[i][1] && able[2] >= Claim[i][2]) {
finish[i] = true;
System.out.println("进程" + i + "分配成功!");
able[0] = able[0] + Allocation[i][0];
able[1] += Allocation[i][1];
able[2] += Allocation[i][2];
i = 0;
} else {
i++;
}
}
for (int j = 0; j < 4; j++) {
if (!finish[j]) {
return false;
}
}
return true;
}
}
代码:测试类
import java.util.Scanner;
public class main {
public static void main(String[] args) {
String[] resType = {"A", "B", "C"};
int[] resources = new int[3];
int processId = 0;
Scanner scanner = new Scanner(System.in);
String choice = "";
while (true) {
bank bk = new bank();
bk.showall();
System.out.println("输入你要分配的进程号(0-3)");
processId = scanner.nextInt();
for (int i = 0; i < 3; i++) {
System.out.println("请输入你要为" + resType[i] + "分配的资源数");
resources[i] = scanner.nextInt();
}
if (bk.CheckResource(processId, resources)) {
bk.ChangeDate(processId, resources);
}
System.out.println("是否继续分配(y/n)");
choice = scanner.next();
if (choice.equals("n")) {
break;
}
}
运行结果