Java~操作系统实验银行家算法的实现(附带源码)

mainOperation(pcbs[i]);

System.out.println(pcbs[i]);

System.out.println();

displayWorks();

}

}

System.out.println();

if (ifSafe()) {

System.out.println(“系统处于安全状态”);

} else {

System.out.println(“系统处于不安全状态”);

}

}

3.3、安全性算法


1)设置两个向量:

工作向量Work: 它表示系统可提供给进程继续运行所需的各类资源数目,它含有m个元素,在执行安全算法开始时,Work=Available;

工作向量Finish: 它表示系统是否有足够的资源分配给进程,使之运行完成。开始时先做Finish[i]=false; 当有足够资源分配给进程时, 再令Finish[i]=true。

2)从进程集合中找到一个能满足下述条件的进程: 

Finish[i]=false;

Need[i,j]≤Work[j];若找到,执行 (3),否则,执行 (4)

3)当进程Pi获得资源后,可顺利执行,直至完成,并释放出分配给它的资源,故应执行:

Work[j]=Work[i]+Allocation[i,j];

Finish[i]=true;

go to step 2;

private void mainOperation(PCB pcb) {

//运行到这说明该进程可以得到足够的资源,那么直接将该进程已分配的资源放回到系统中

//并将finish改为true

for (int i = 0; i < this.work.length; i++) {

this.work[i].num += pcb.allocation[i].num;

pcb.finish = true;

}

}

4)如果所有进程的Finish[i]=true都满足, 则表示系统处于安全状态;否则,系统处于不安全状态

四、实现代码

=====================================================================

/**

  • 优化银行家算法,实现资源再次申请

*/

//系统资源的数据结构

class Resources {

public String name;

public int num;

public Resources(String name, int num) {

this.name = name;

this.num = num;

}

@Override

public String toString() {

return “资源”+ name + “有” + num;

}

}

//进程的数据结构

class PCB {

public String name;

public Resources[] max;//最大需求资源Max

public Resources[] allocation;//已分配资源Allocation

public Resources[] need;//需求资源Need

public boolean finish = false;//表示进程是否获得足够资源

public PCB(String name, Resources[] max, Resources[] allocation) {

this.name = name;

this.max = max;

this.allocation = allocation;

this.need = new Resources[max.length];

for (int i = 0; i < need.length; i++) {

this.need[i] = new Resources(max[i].name, max[i].num - allocation[i].num);

}

}

@Override

public String toString() {

return this.name +(this.finish ? “进程已得到足够资源” : “需要等待”);

}

}

public class BetterBankerAlgorithm {

private Resources[] work;//当前可以资源

private PCB[] pcbs;//所有进程

public BetterBankerAlgorithm(Resources[] Available, PCB[] pcbs) {

this.work = Available;//一开始可用资源与系统总资源是相同的

this.pcbs = pcbs;

//计算剩余可用资源

actualWork();

}

private void actualWork() {

for (int i = 0; i < this.work.length; i++) {

//初始系统总资源减去已分配的资源就是当前可以利用的资源

this.work[i].num = this.work[i].num - pcbsAllRes(i);

}

}

//计算所有进程已分配的第i个资源总数

private int pcbsAllRes(int index) {

int sum = 0;

for ( PCB p : pcbs

) {

sum += p.allocation[index].num;

}

return sum;

}

//判断是否为安全状态

private boolean ifSafe() {

for (PCB p : pcbs

) {

if (! p.finish) {

return false;

}

}

return true;

}

//进行资源分配

public void resAllocation() {

//对进程进行循环资源分配,当所有进程都需要等待或者所有进程都为安全状态退出循环

for (int i = 0; !ifSafe() && !ifAllNeedWait(); i++) {

//实现循环

if (i == this.pcbs.length) {

i = 0;

}

//判断当前这个进程是否已经获得过足够资源

if (pcbs[i].finish) {

continue;

}

//判断当前这个进程是否需要等待

if (! needWait(pcbs[i])) {

//进行资源分配

mainOperation(pcbs[i]);

System.out.println(pcbs[i]);

System.out.println();

displayWorks();

}

}

System.out.println();

if (ifSafe()) {

System.out.println(“系统处于安全状态”);

} else {

System.out.println(“系统处于不安全状态”);

}

}

private void displayWorks() {

System.out.println(“此时系统可用资源为:”);

System.out.println(“====================”);

for (Resources r : this.work

) {

System.out.println®;

}

System.out.println(“====================”);

}

private boolean ifAllNeedWait() {

for (PCB p : this.pcbs

) {

//如果该进程已经得到过足够资源就不进行判断

if (!p.finish) {

//如果该进程不需要等待就直接返回false

if (!needWait§) {

return false;

}

}

}

return true;

}

private void mainOperation(PCB pcb) {

//运行到这说明该进程可以得到足够的资源,那么直接将该进程已分配的资源放回到系统中

//并将finish改为true

for (int i = 0; i < this.work.length; i++) {

this.work[i].num += pcb.allocation[i].num;

pcb.finish = true;

}

}

private boolean needWait(PCB pcb) {

//挨个判断此时pcb这个进程所需要的每个资源,如果need大于系统当前可分配资源,就说明需要等待

for (int i = 0; i < this.work.length; i++) {

if (this.work[i].num < pcb.need[i].num) {

return true;

}

}

return false;

}

}

测试数据及结果

======================================================================

数据


在这里插入图片描述

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print(“输入资源有几种:”);

int resKindNum = scan.nextInt();

Resources[] Available = new Resources[resKindNum];//系统总资源

System.out.println();

System.out.println(“输入每个系统资源名称、系统资源数量”);

for (int i = 0; i < Available.length; i++) {

System.out.println(“初始化第” + (i +1) + “个资源”);

System.out.println();

System.out.print(“资源名称:”);

String name = scan.next();

System.out.println();

System.out.print(“该资源数量:”);

int num = scan.nextInt();

Available[i] = new Resources(name, num);

System.out.println();

System.out.println(“第” + (i +1) + “个资源初始化完毕”);

System.out.println(“====================”);

}

System.out.println();

System.out.println(“系统资源初始化完毕,开始初始化进程”);

System.out.println();

System.out.print(“输入进程个数:”);

int pcbNums = scan.nextInt();

PCB[] pcbs = new PCB[pcbNums];

for (int i = 0; i < pcbNums; i++) {

System.out.println(“初始化第” + (i +1) + “个进程”);

System.out.println();

System.out.print(“输入进程名:”);

String name = scan.next();

System.out.println();

System.out.print(“输入该进程最大需求资源:”);

Resources[] max = new Resources[resKindNum];

int[] maxResNum = new int[resKindNum];

for (int j = 0; j < resKindNum; j++) {

maxResNum[j] = scan.nextInt();

}

for (int j = 0; j < max.length; j++) {

max[j] = new Resources(Available[j].name, maxResNum[j]);

}

System.out.println();

System.out.print(“输入该进程已分配资源数目:”);

Resources[] allocation = new Resources[resKindNum];

int[] allocReesNum = new int[resKindNum];

for (int j = 0; j < resKindNum; j++) {

allocReesNum[j] = scan.nextInt();

}

for (int j = 0; j < allocation.length; j++) {

allocation[j] = new Resources(Available[j].name, allocReesNum[j]);

}

System.out.println();

//此时一个进程的所有需要的东西都已输入完毕

pcbs[i] = new PCB(name, max, allocation);

System.out.println(“第” + (i +1) + “个进程初始化完毕”);

System.out.println(“====================”);

}

System.out.println();

System.out.println(“所有进程初始化完毕,开始资源分配”);

System.out.println();

boolean key = true;

while (key) {

BetterBankerAlgorithm bankerAlgorithm = new BetterBankerAlgorithm(Available, pcbs);

bankerAlgorithm.resAllocation();

System.out.println();

System.out.println("是否需要再次申请资源输入: y or n ");

char ch = scan.next().charAt(0);

if (ch == ‘n’ || ch == ‘N’) {

key = false;

} else {

System.out.print(“输入要申请资源的进程名:”);

String name = scan.next();

System.out.println();

System.out.print(“输入要申请资源的数量:”);

int[] res = new int[resKindNum];

for (int i = 0; i < resKindNum; i++) {

res[i] = scan.nextInt();

}

System.out.println();

for (PCB p : pcbs

) {

p.finish = false;

if (p.name.equals(name)) {

for (int i = 0; i < p.allocation.length; i++) {

p.allocation[i].num += res[i];

}

}

}

System.out.println();

System.out.println(“再次申请资源完毕,开始资源分配”);

System.out.println();

}

}

}

}

结果


输入资源有几种:4

输入每个系统资源名称、系统资源数量

初始化第1个资源

资源名称:R1

该资源数量:6

第1个资源初始化完毕

====================

初始化第2个资源

资源名称:R2

该资源数量:7

第2个资源初始化完毕

====================

初始化第3个资源

资源名称:R3

该资源数量:12

第3个资源初始化完毕

====================

初始化第4个资源

资源名称:R4

该资源数量:12

第4个资源初始化完毕

====================

系统资源初始化完毕,开始初始化进程

输入进程个数:5

初始化第1个进程

输入进程名:P0

输入该进程最大需求资源:0 0 1 2

输入该进程已分配资源数目:0 0 1 2

第1个进程初始化完毕

====================

初始化第2个进程

输入进程名:P1

输入该进程最大需求资源:2 7 5 0

输入该进程已分配资源数目:2 0 0 0

第2个进程初始化完毕

====================

初始化第3个进程

输入进程名:P2

输入该进程最大需求资源:6 6 5 6

输入该进程已分配资源数目:0 0 3 4

第3个进程初始化完毕

====================

初始化第4个进程

输入进程名:P3

输入该进程最大需求资源:4 3 5 6

输入该进程已分配资源数目:2 3 5 4

第4个进程初始化完毕

====================

初始化第5个进程

输入进程名:P4

输入该进程最大需求资源:0 6 5 2

输入该进程已分配资源数目:0 3 3 2

第5个进程初始化完毕

====================

所有进程初始化完毕,开始资源分配

P0进程已得到足够资源

此时系统可用资源为:

====================

资源R1有2

资源R2有1

资源R3有1

资源R4有2

====================

P3进程已得到足够资源

此时系统可用资源为:

====================

资源R1有4

资源R2有4

资源R3有6

资源R4有6

====================

P4进程已得到足够资源

此时系统可用资源为:

====================

资源R1有4

资源R2有7

资源R3有9

资源R4有8

====================

P1进程已得到足够资源

此时系统可用资源为:

====================

资源R1有6

资源R2有7

资源R3有9

资源R4有8

====================

P2进程已得到足够资源

此时系统可用资源为:

====================

资源R1有6

资源R2有7

资源R3有12

资源R4有12

====================

系统处于安全状态

是否需要再次申请资源输入: y or n

Y

输入要申请资源的进程名:P2

输入要申请资源的数量:0 1 0 0

再次申请资源完毕,开始资源分配

P0进程已得到足够资源

此时系统可用资源为:

====================

资源R1有2

资源R2有0

资源R3有1

资源R4有2

====================

P3进程已得到足够资源

此时系统可用资源为:

====================

资源R1有4

资源R2有3

资源R3有6

资源R4有6

====================

P4进程已得到足够资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值