操作系统银行家算法Java实现

操作系统银行家算法Java实现
关于银行家算法,我认为其中最重要的一部分就是系统安全性判断和安全序列的查找。安全性的判断是通过循环判断进程所需要的资源是否小于资源剩余,这其中的关键和难点便是何时跳出循环。我是看了这篇帖子理解的。
链接: https://blog.csdn.net/qq_36260974/article/details/84404369
依据,安全性算法,我们首先要找出一个所需资源小于等于剩余可分配资源的进程,然后从它开始进行寻找。如果刚开始有多个进程符合所需资源小于等于剩余可分配资源的条件,我们就可以设置一个数组来存储这些进程,若第一个符合条件的进程不能找到安全序列,那么则由下一个符合条件的进程来寻找。
接下来就是跳出寻找安全序列循环的判断。首先定义一个变量count来记录符合条件的进程,然后设置一个count_temp来保存经过一轮后的count值,一轮循环后count和count_temp的值相等则说明没有符合条件的进程了则可以跳出循环了。
在这里插入图片描述
一、T0系统是否处于安全状态?请打印输出找到的资源分配的安全序列。
二、如果T0时刻进程P1提出需要(1、0、2)个资源的请求,系统能否满足它的请求?
如果T0时刻进程P4提出需要(3、3、0)个资源的请求,系统能否满足它的请求?
如果T0时刻进程P0提出需要(0、2、0)个资源的请求,系统能否满足它的请求?
附上代码:`package 银行家算法;

public class PCB {

String name;

int max_A;
int max_B;
int max_C;

int all_A;
int all_B;
int all_C;

int ned_A;
int ned_B;
int ned_C;

boolean finish;
boolean flag=false;
PCB(String s,int max_A,int max_B,int max_C,int all_A,int all_B,int all_C,int ned_A,int ned_B,int ned_C){
	this.name = s;
	
	this.all_A = all_A;
	this.all_B = all_B;
	this.all_C = all_C;
	
	this.all_A = all_A;
	this.all_B = all_B;
	this.all_C = all_C;
	
	this.ned_A = ned_A;
	this.ned_B = ned_B;
	this.ned_C = ned_C;
}

public void is_finishi() {
	if(this.ned_A==0&&this.ned_B==0&&this.ned_C==0)
		this.finish = true;
	else {
		this.finish = false;
	}
}

}
package 银行家算法;

import java.util.*;

public class Main {

static int A = 3;
static int B = 3;
static int C = 2;
public static void main(String[] args) {
	
	PCB pcb[] = new PCB[5];
	pcb[0] = new PCB("P0", 7, 5, 3, 0, 1, 0, 7, 4, 3);
	pcb[1] = new PCB("P1", 3, 2, 2, 2, 0, 0, 1, 2, 2);
	pcb[2] = new PCB("P2", 9, 0, 2, 3, 0, 2, 6, 0, 0);
	pcb[3] = new PCB("P3", 2, 2, 2, 2, 1, 1, 0, 1, 1);
	pcb[4] = new PCB("P4", 4, 3, 3, 0, 0, 2, 4, 3, 1);
	
	safe(A, B, C, pcb);
	System.out.println("P1提出需要(1、0、2)个资源的请求:");
	work(1, 1, 0, 2, pcb);
	System.out.println("P4提出需要(3、3、0)个资源的请求:");
	work(4, 3, 3, 0, pcb);
	System.out.println("P0提出需要(0、2、0)个资源的请求:");
	work(0, 0, 2, 0, pcb);
	
}
//安全性判断
public static  boolean safe(int a,int b,int c,PCB pcb[] ) {
	int index=0;
	int workA = a;
	int workB = b;
	int workC = c;
	int t[] = new int[5];
	int j=0;
	int safe[] = new int[5];
	int count = 0;
	int count_temp = 0;
	boolean issafe = false;
	//所有需要少于剩余资源的进程
	for(int i=0;i<5;i++) {
		if(pcb[i].ned_A<=workA&&pcb[i].ned_B<=workB&&pcb[i].ned_C<=workC) {
			t[j++] = i;
		}
	}
	if(j==0) {
		System.out.println("系统不安全");
		return false;
	}
	for(int i=0;i<j;i++) {
		index = t[i];
		count = 0;
		count_temp = 0;
		safe[0]=0;
		safe[1]=0;
		safe[2]=0;
		safe[3]=0;
		safe[4]=0;
		workA = a;
		workB = b;
		workC = c;
		while(index<5) {				if(pcb[index].flag==false&&pcb[index].ned_A<=workA&&
				pcb[index].ned_B<=workB&&pcb[index].ned_C<=workC) {
				workA += pcb[index].all_A;
				workB += pcb[index].all_B;
				workC += pcb[index].all_C;
				pcb[index].flag = true;
				safe[count++] = index;
				index++;
			}
			else {
				index++;
			}
			if(count==5) {
				issafe = true;
			break;
			}
			if(index==5) {
				index = index%5;
				if(count_temp==count) {
					break;
				}
				count_temp = count;
			}
		}
		for(int e=0;e<5;e++) {
			pcb[e].flag=false;
		}
	}
	if(issafe) {
		System.out.println("安全序列为:");
		for(int r=0;r<5;r++) {
			if(r==4)
				System.out.printf(pcb[safe[r]].name);
			else {
				System.out.printf(pcb[safe[r]].name+"->");
			}
		}
		System.out.println("");
		return true;
	}
	System.out.println("系统不安全");
	return false;
}
public static void work(int p,int a,int b,int c,PCB pcb[]) {
	int index1 = p;
	int workA = A;
	int workB = B;
	int workC = C;
	if(a<=pcb[index1].ned_A&&a<=A
	   &&b<=pcb[index1].ned_B&&b<=B
	   &&c<=pcb[index1].ned_C&&c<=C) {
		workA = workA-a;
		workB = workB-b;
		workC = workC-c;
		
		pcb[index1].ned_A = pcb[index1].ned_A-a;
		pcb[index1].ned_B = pcb[index1].ned_B-b;
		pcb[index1].ned_C = pcb[index1].ned_C-c;
		
		pcb[index1].all_A += a;
		pcb[index1].all_B += b;
		pcb[index1].all_C += c;
		if(safe(workA,workB,workC,pcb)==false) {
			//System.out.println("不能分配");
			workA = workA+a;
			workB = workB+b;
			workC = workC+c;
			
			pcb[index1].ned_A = pcb[index1].ned_A+a;
			pcb[index1].ned_B = pcb[index1].ned_B+b;
			pcb[index1].ned_C = pcb[index1].ned_C+c;
			
			pcb[index1].all_A -= a;
			pcb[index1].all_B -= b;
			pcb[index1].all_C -= c;
		}

	}
	else {
		System.out.println("不可以分配");
	}
}

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值