操作系统--银行家算法(java实现)

import java.util.HashMap;
import java.util.Scanner;

class Banker{
	private int amount;//可用资源种类的个数
	private int m;//进程的个数
	private boolean flag;
	
	String name[]=new String[100];//资源名称
	int Available[]=new int[100];//当前资源剩余向量
	int atempt[]=new int[100];
	int MAX[][]=new int[100][100];//最大需求矩阵
	int Allocation[][]=new int[100][100];//已分配资源矩阵
	int Need[][]=new int[100][100];//当前还需资源数量矩阵
	int Request[]=new int[100];//请求资源向量
	
	
	public void init(){
		System.out.println("系统可用资源种类的个数:");
		Scanner in=new Scanner(System.in);
		amount=in.nextInt();
		System.out.println("请输入可用资源名称和相应个数:(格式为:名称 个数)");
		for(int i=0;i<amount;i++) {
			String name1=in.next();
			int n=in.nextInt();
			name[i]=name1;
			Available[i]=n;
		}
		
		//输入最大需求矩阵
		System.out.println("请输入当前进程的数量:");
		m=in.nextInt();
		System.out.println("请输入各进程的最大需求矩阵:[MAX]");
		do {
		    flag=false;
			for(int i=0;i<m;i++) {
				for(int j=0;j<amount;j++) {
					MAX[i][j]=in.nextInt();
					if(MAX[i][j]>Available[j]) {
						flag=true;
					}
				}
			}
			if(flag) {
				System.out.println("资源最大需求量大于系统资源最大数量,输入错误,请重新输入!");
			}
		}while(flag);
		
		//输入已分配资源矩阵
		System.out.println("请输入已分配资源矩阵:[Allocation]");
		do {
		    flag=false;
			for(int i=0;i<m;i++) {
				for(int j=0;j<amount;j++) {
					Allocation[i][j]=in.nextInt();
					if(Allocation[i][j]>Available[j]) {
						flag=true;
					}
					atempt[j]+=Allocation[i][j];
					Need[i][j]=MAX[i][j]-Allocation[i][j];
					
				}
			}
			if(flag) {
				System.out.println("已分配资源数量大于系统资源最大数量,输入错误,请重新输入!");
			}
		}while(flag);
		
		for(int i=0;i<amount;i++) {
			Available[i]-=atempt[i];
		}
			
	}
	
	public void show() {
		showAvailable();
		System.out.println();
		System.out.println("当前系统资源分配情况如下:");
		showMAX();
		showAllocation();
		showNeed();
	}
	
	public void showAvailable() {
		System.out.println("当前资源剩余向量:Available[]");
		for(int i=0;i<amount;i++) {
			System.out.print(name[i]+"\t");
		}
		System.out.println();
		for(int i=0;i<amount;i++) {
			System.out.print(Available[i]+"\t");
		}
	}
	
	public void showMAX() {
		System.out.println("[MAX]");
		System.out.print("进程名 \t");
		for(int i=0;i<amount;i++) {
			System.out.print(name[i]+"    \t");
		}
		System.out.println();
		for(int i=0;i<m;i++) {
			System.out.print("P"+i);
			for(int j=0;j<amount;j++) {
				System.out.print("    \t"+MAX[i][j]);
			}
			System.out.println();
		}
	}
	
	public void showAllocation() {
		System.out.println("[Allocation]");
		System.out.print("进程名 \t");
		for(int i=0;i<amount;i++) {
			System.out.print(name[i]+"    \t");
		}
		System.out.println();
		for(int i=0;i<m;i++) {
			System.out.print("P"+i);
			for(int j=0;j<amount;j++) {
				System.out.print("    \t"+Allocation[i][j]);
			}
			System.out.println();
		}
	}
	
	public void showNeed() {
		System.out.println("[Need]");
		System.out.print("进程名 \t");
		for(int i=0;i<amount;i++) {
			System.out.print(name[i]+"    \t");
		}
		System.out.println();
		for(int i=0;i<m;i++) {
			System.out.print("P"+i);
			for(int j=0;j<amount;j++) {
				System.out.print("    \t"+Need[i][j]);
			}
			System.out.println();
		}
	}
	
	public void tryTest() {
		
	}
	
	
	public void isSafe() {
		int w=0;
		int flag=0;
		int t=0;
		int safe[]=new int[m];
		int t1[]=new int[amount];
		int t0[]=new int[m];
		for(int i=0;i<amount;i++) {
			t1[i]=Available[i];
		}
		for(int i=0;i<m;i++) {
			safe[i]=1;
		}
		while(w<m) {
			MAX_LOOP:
				for(int i=0;i<m;i++) {
					if(safe[i]==1) {
						for(int j=0;j<amount;j++) {
							if(Need[i][j]<=t1[j]) {}
							else {
								continue MAX_LOOP;
							}
						}
						safe[i]=0;
						t0[t++]=i;
						for(int k=0;k<amount;k++) {
							t1[k]+=Allocation[i][k];
						}
					}
				}
			w++;
		}
		for(int i=0;i<m;i++) {
			if(safe[i]==0) {flag++;}
			else {
				System.out.println("此时刻系统不安全!");
				System.exit(0);
			}
		}
		if(flag==m) {
			System.out.println("此时刻系统安全!存在一个安全序列:");
			for(int i=0;i<m;i++) {
				System.out.print("p"+t0[i]+" ");
			}
		}
	}
	
	public void applyRequest() {
		Scanner in=new Scanner(System.in);
		System.out.println("发出资源请求的进程是哪一个:(输入格式:0,1,2....)");
		int pro=in.nextInt();
		System.out.println("请输入资源请求向量:(输入格式例如:1 2 3 4)");
		for(int i=0;i<amount;i++) {
			Request[i]=in.nextInt();
		}
		for(int i=0;i<amount;i++) {
			if(Request[i]<=Available[i]) {}
			else {
				System.out.println("请求资源个数大于剩余资源个数,不能够实施资源分配!");
				System.exit(0);
			}
		}
		for(int i=0;i<amount;i++) {
			if(Request[i]<=Need[pro][i]) {}
			else {
				System.out.println("请求资源个数大于需求资源个数,不能够实施资源分配!");
				System.exit(0);
			}
		}
		for(int i=0;i<amount;i++) {
			Available[i]-=Request[i];
			Need[pro][i]-=Request[i];
			Allocation[pro][i]+=Request[i];
		}
	}
	
}

public class Main{
	public static void main(String args[]) {
		Scanner in=new Scanner(System.in);
		System.out.println("*************************银行家算法*************************");
		Banker bank=new Banker();
		bank.init();
		bank.show();
		bank.isSafe();
		System.out.println();
		System.out.println("输入1:请求分配   输入0:退出程序");
		int choice=in.nextInt();
		switch(choice) {
		case 0:
			System.exit(0);
			break;
		case 1:
			bank.applyRequest();
			bank.show();
			bank.isSafe();
			break;
		default:
        	System.out.println("请正确选择!");
		}
	}
}

运行结果:

 

 

 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值