银行家算法java实现

关于银行家算法的理论知识,课本或者百度上有好多资料,我就不再多说了,这里把我最近写的银行家算法的实现带码贴出来。

由于这是我们的一个实验,对系统资源数和进程数都指定了,所以这里也将其指定了,其中系统资源数为3,进程数为5.

import java.util.Scanner;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;
import javax.swing.text.StyledEditorKit.ForegroundAction;


public class Banker {
	static int available[]=new int[3];         //资源数
	static int max[][]=new int[5][3];          //最大需求
	static int allocation[][]=new int[5][3];   //分配
	static int need[][]=new int[5][3];         //需求
	static int request[]=new int[3];           //存放请求
	Scanner scanner=new Scanner(System.in);
	int thread;  //线程号

	//初始化
	public void getData(){
		System.out.println("请输入A,B,C三类资源的数目:");
		//输入A,B,C三类资源数量
		for(int i=0;i<3;i++){
			available[i]=scanner.nextInt();
		}
		//输入进程对三类资源的最大需求
		for(int i=0;i<5;i++){
			System.out.println("请输入进程"+i+"对A,B,C三类资源的最大需求");
			for(int j=0;j<3;j++){
				max[i][j]=scanner.nextInt();
			}
		}
		//输入进程分配的三类资源数
		for(int i=0;i<5;i++){
			System.out.println("请输入进程"+i+"已分配的A,B,C三类资源数");
			for(int j=0;j<3;j++){
				allocation[i][j]=scanner.nextInt();
			}
		}
		//计算进程还需要的三类资源数
		for(int i=0;i<5;i++){
			for(int j=0;j<3;j++){
				need[i][j]=max[i][j]-allocation[i][j];
			}
		}
		//重新计算available
		for(int i=0;i<3;i++){
			for(int j=0;j<5;j++){
				available[i]-=allocation[j][i];
			}
		}
	}
	//用户输入要申请资源的线程和申请的资源,并进行判断
	public void getThread(){
		System.out.println("请输入申请资源的线程");
		int thread=scanner.nextInt();     //线程
		if(thread<0||thread>4){
			System.out.println("该线程不存在,请重新输入");
			getThread();
		}else{
			this.thread=thread;
			System.out.println("请输入申请的资源(三种,若某种资源不申请则填0)");
			for(int i=0;i<3;i++){
				request[i]=scanner.nextInt();
			}
			if(request[0]>need[thread][0]||request[1]>need[thread][1]||request[2]>need[thread][2]){
				System.out.println(thread+"线程申请的资源超出其需要的资源,请重新输入");
				getThread();
			}else{
				if(request[0]> available[0]||request[1]> available[1]||request[2]> available[2]){
					System.out.println(thread+"线程申请的资源大于系统资源,请重新输入");
					getThread();
				}
			}
			changeData(thread);
			if(check(thread)){
				 getThread();
			}else{
				recoverData(thread);
				getThread();
			}

		}
	}
	
	 //thread线程请求响应后,试探性分配资源
	 public void changeData(int thread){
		 for(int i=0;i<3;i++){
			 //重新调整系统资源数
			 available[i]-=request[i];
			 //计算各个线程拥有资源
			 allocation[thread][i]+=request[i];
			 //重新计算需求
			 need[thread][i]-=request[i];
		 }		 
   }
  
	   //安全性检查为通过,分配失败时调用,恢复系统原状		 
	    public void recoverData(int thread){
		   for(int i=0;i<3;i++){
				 //重新调整系统资源数
				 available[i]+=request[i];
				 //计算各个线程拥有资源
				 allocation[thread][i]-=request[i];
				 //重新计算需求
				 need[thread][i]+=request[i];
		  }	
	    }
	    
	//对线程thread安全性检查
	 public boolean check(int thread){
		 boolean finish[]=new boolean[5];
		 int work[]=new int[3];
		 int queue[]=new int[5];   //由于存放安全队列
		 int k=0;//安全队列下标
		 int j;  //要判断的线程
		 int i;
		 //是否分配的标志
		 for(i=0;i<5;i++)
			 finish[i]=false;
		 j=thread;
		 for(i=0;i<3;i++){
			 work[i]=available[i];
		 }
		 while(j<5){
			 for( i=0;i<3;i++){	
				 if(finish[j]){
					 j++;
					 break;
				 }else if(need[j][i]>work[i]){
					 //System.out.println(need[j][i]+"*"+i+work[i]);
					 j++;
					 break;
				 }else if(i==2){
					 for(int m=0;m<3;m++){
						 work[m]+=allocation[j][m];
					 }
					 finish[j]=true;
					 queue[k]=j;
					 k++;
					 j=0;   //从最小线程再开始判断
				 }
			 }
		 }
		 
		 //判断是否都属于安全状态
		 for(int p=0;p<5;p++){
			 if(finish[p]=false){
				 System.out.println("系统不安全,资源申请失败");
				 return false;
			 }
		 }
		 System.out.println("资源申请成功,安全队列为:");
		 for(int q=0;q<5;q++){
			 System.out.println(queue[q]);
		 } 
		 return true;
	 }
	 //打印need和available,需要时调用
	  public void showData(){
		  System.out.println("need");
		  for(int i=0;i<5;i++){
			  for(int j=0;j<3;j++){
				  System.out.print(need[i][j]+"     ");
			  }
		  }
		     System.out.println("available");
			  for(int j=0;j<3;j++){
				  System.out.print(available[j]+"     ");
			  }
	  }
   public static void main(String[] args) {
	  Banker bk=new Banker();
	  bk.getData();
	  bk.getThread();

	}

}





版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dingxiaoyue/p/4931802.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行家算法是一种避免死锁的算法,它通过动态地分配资源来避免进程因争夺资源而产生死锁。下面是银行家算法Java实现: ```java import java.util.Scanner; public class BankerAlgorithm { private int[][] need; // 需求矩阵 private int[][] allocation; // 分配矩阵 private int[][] max; // 最大需求矩阵 private int[] available; // 可用资源向量 private int processNum; // 进程数 private int resourceNum; // 资源public BankerAlgorithm(int[][] need, int[][] allocation, int[][] max, int[] available) { this.need = need; this.allocation = allocation; this.max = max; this.available = available; this.processNum = allocation.length; this.resourceNum = available.length; } // 判断是否满足需求 private boolean check(int[] work, boolean[] finish, int[] need, int[] allocation) { for (int i = 0; i < resourceNum; i++) { if (need[i] > work[i]) { return false; } } for (int i = 0; i < resourceNum; i++) { work[i] += allocation[i]; } finish[processNum] = true; return true; } // 执行银行家算法 public boolean execute() { boolean[] finish = new boolean[processNum]; int[] work = new int[resourceNum]; for (int i = 0; i < resourceNum; i++) { work[i] = available[i]; } int count = 0; while (count < processNum) { boolean flag = false; for (int i = 0; i < processNum; i++) { if (!finish[i] && check(work, finish, need[i], allocation[i])) { flag = true; count++; } } if (!flag) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("输入进程数:"); int processNum = scanner.nextInt(); System.out.print("输入资源数:"); int resourceNum = scanner.nextInt(); int[][] max = new int[processNum][resourceNum]; int[][] allocation = new int[processNum][resourceNum]; int[][] need = new int[processNum][resourceNum]; int[] available = new int[resourceNum]; System.out.println("输入最大需求矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { max[i][j] = scanner.nextInt(); } } System.out.println("输入分配矩阵:"); for (int i = 0; i < processNum; i++) { for (int j = 0; j < resourceNum; j++) { allocation[i][j] = scanner.nextInt(); need[i][j] = max[i][j] - allocation[i][j]; } } System.out.println("输入可用资源向量:"); for (int i = 0; i < resourceNum; i++) { available[i] = scanner.nextInt(); } BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(need, allocation, max, available); if (bankerAlgorithm.execute()) { System.out.println("系统是安全的!"); } else { System.out.println("系统是不安全的!"); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值