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

*/

//系统资源的数据结构

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

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行家算法 1. 实验目的和要求 银行家算法是避免死锁的一种重要方法,要求编写和调试一个简单的银行家算法程序。加深了解有关资源申请、避免死锁等概念,并体会和了解死锁和避免死锁的具体实施方法。 2. 实验内容 1.设计进程对各类资源最大申请表示及初值确定。 2.设定系统提供资源初始状况。 3.设定每次某个进程对各类资源的申请表示。 4.编制程序,依据银行家算法,决定其申请是否得到满足。 3. 实验说明 1.数据结构 假设有M个进程N类资源,则有如下数据结构: MAX[M*N] M个进程对N类资源的最大需求量 AVAILABLE[N] 系统可用资源数 ALLOCATION[M*N] M个进程已经得到N类资源的资源量 NEED[M*N] M个进程还需要N类资源的资源量 2.银行家算法 设进程I提出请求Request[N],则银行家算法按如下规则进行判断。 (1)如果Request[N]<=NEED[I,N],则转(2);否则,出错。 (2)如果Request[N]<=AVAILABLE,则转(3);否则,出错。 (3)系统试探分配资源,修改相关数据: AVAILABLE=AVAILABLE-REQUEST ALLOCATION=ALLOCATION+REQUEST NEED=NEED-REQUEST (4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。 3.安全性检查 (1)设置两个工作向量WORK=AVAILABLE;FINISH[M]=FALSE (2)从进程集合中找到一个满足下述条件的进程, FINISH[i]=FALSE NEED<=WORK 如找到,执行(3);否则,执行(4) (3)设进程获得资源,可顺利执行,直至完成,从而释放资源。 WORK=WORK+ALLOCATION FINISH=TRUE GO TO 2 (4)如所有的进程Finish[M]=true,则表示安全;否则系统不安全。 4. 参考程序 #include "string.h" #include "iostream.h" #define M 5 //总进程数 #define N 3 //总资源数 #define FALSE 0 #define TRUE 1 //M个进程对N类资源最大资源需求量 int MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; //系统可用资源数 int AVAILABLE[N]={10,5,7}; //M个进程已经得到N类资源的资源量 int ALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}; //M个进程还需要N类资源的资源量 int NEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; int Request[N]={0,0,0};

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值