自己写的银行家算法,有兴趣的可以拿去测试,欢迎提出建议

Banker.java

import java.util.LinkedList;

public class Banker {
	private int[] available; // 拥有各类资源的数目
	private int[] work;

	Banker() {
	}

	Banker(int[] available, int type) {
		this.available = new int[type];
		this.work = new int[type];
		for (int i = 0; i < type; i++) {
			this.available[i] = available[i];
			this.work[i] = available[i];
		}
	}

	/*
	 * 初步检查客户请求资源是否能给予满足,是return true
	 * 
	 * @param available 可用资源量
	 * 
	 * @param request 客户请求资源量
	 * 
	 * @param need 客户达到最大值所需要的资源量
	 */

	public boolean checkAvailable(int[] available, int[] request, int[] need) {
		for (int i = 0; i < request.length; i++) {
			if (request[i] > available[i] || request[i] > need[i]) {
				return false;
			}
		}
		return true;
	}

	/*
	 * 检查银行家的available是否大于等于客户的need,是return true
	 * 
	 * @param available 可用资源量
	 * 
	 * @param need 客户达到最大值所需要的资源量
	 */

	public boolean checkAvailable(int[] available, int[] need) {
		for (int i = 0; i < need.length; i++) {
			if (available[i] < need[i]) {
				return false;
			}
		}
		return true;
	}

	/*
	 * 
	 * @ 系统安全性检查算法 尝试给系统分配资源 每次获得链表中的第一个元素, 符合条件则进行修改,并将该元素放到队尾,然后将flag置为0;
	 * 否则将该元素放到队尾,并将flag++
	 */

	public boolean safeCheck(LinkedList<Customer> customers, Banker banker) {

		int count = customers.size();
		int[] needTemp;
		int[] available = banker.getAvailable();
		int[] work = available;
		int type = available.length;
		int flag = 0;
		Customer cusTemp;
		System.out
				.println("进程名" + "\t" + " work" + "\t\t" + " need" + "  \t"
						+ " Allocation" + "\t" + " Work+Allocation" + "\t "
						+ " finish");
		while (flag < count) {
			cusTemp = customers.getFirst();
			needTemp = cusTemp.getNeed();
			if (checkAvailable(work, needTemp) && cusTemp.isFinish() == false) {
				int[] ava;
				int[] work1 = ava = banker.getAvailable();
				int[] n;
				int[] al;
				n = cusTemp.getNeed();
				al = cusTemp.getAllocation();
				System.out.print(" " + cusTemp.getName() + "\t");
				/* work = ava */

				for (int j = 0; j < ava.length; j++) {
					// work[j] += al[j];
					System.out.print(work1[j] + " ");
				}

				System.out.print("\t");
				/* need */
				for (int j = 0; j < ava.length; j++) {
					System.out.print(n[j] + " ");
				}
				System.out.print("\t");
				/* allocation */
				for (int j = 0; j < ava.length; j++) {
					System.out.print(al[j] + " ");
				}

				customers.remove(0);
				cusTemp.setFinish(true);
				customers.addLast(cusTemp);
				int[] allocT = cusTemp.getAllocation();
				for (int k = 0; k < type; k++) {
					work[k] += allocT[k];
				}
				banker.setWork(work);
				

				System.out.print("   \t");
				for (int j = 0; j < ava.length; j++) {
					System.out.print(work1[j] + " ");
				}
				System.out.print("\t");
				System.out.println(cusTemp.isFinish() + "  ");
				
				flag = 0;
			} else {
				customers.removeFirst();
				customers.addLast(cusTemp);
				flag++;
			}
		}
		for (int i = 0; i < count; i++) {
			if (customers.get(i).isFinish() == false) {
				return false;
			}
		}

		return true;
	}

	/*
	 * 客户请求之后,经过初步判断通过之后,假设给予某个客户所要求的量,然后进行判断
	 * 
	 * @param customers 客户链表
	 * 
	 * @param banker 银行家
	 * 
	 * @param request 请求量
	 * 
	 * @param name 客户名
	 * 
	 * @return 通过的话返回true
	 */

	public boolean checkSafe(LinkedList<Customer> customers, Banker banker,
			int[] request, String name) {

		for (int k = 0; k < customers.size(); k++) {
			if (customers.get(k).getName().equals(name)) {
				if (!checkAvailable(available, request, customers.get(k)
						.getNeed())) {
					return false;
				}
				int[] alloc = customers.get(k).getAllocation();
				for (int c = 0; c < alloc.length; c++) {
					alloc[c] += request[c];
				}
				customers.get(k).setAllocation(alloc);
			}
		}
		return safeCheck(customers, banker);
	}

	public int[] getAvailable() {
		return available;
	}

	public void setAvailable(int[] available) {
		this.available = available;
	}

	public int[] getWork() {
		return work;
	}

	public void setWork(int[] work) {
		this.work = work;
	}

}

Customer.java

public class Customer {
	
	private String name;
	private boolean finish; //true表示该请求已经被完成
	private int type; //需求类型的种类
	private int[] need;  //尚需的给类资源数
	private int[] max;   //最大需求
	private int[] allocation;  //已经分得的给类资源数
	
	Customer(){
	}
	Customer(String name,int type, int[] max, int[] allocation){
		this.name = name;
		this.max = new int[type];
		this.allocation = new int[type];
		this.need = new int[type];
		for(int i=0; i<type; i++)
		{
			this.allocation[i] = allocation[i];
			this.max[i] = max[i];
			this.need[i] = max[i] - allocation[i];
		}
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public boolean isFinish() {
		return finish;
	}
	public void setFinish(boolean finish) {
		this.finish = finish;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int[] getNeed() {
		return need;
	}
	public void setNeed(int[] need) {
		this.need = need;
	}
	public int[] getMax() {
		return max;
	}
	public void setMax(int[] max) {
		this.max = max;
	}
	public int[] getAllocation() {
		return allocation;
	}
	public void setAllocation(int[] allocation) {
		this.allocation = allocation;
	}
	
	
}

MyOS.java

import java.util.LinkedList;
import java.util.Scanner;


public class MyOS {

	static LinkedList<Customer> customers = new LinkedList<Customer>();
	static Banker banker = new Banker();
	
	public static void main(String[] args) {
		String name = null;
		int myCase = 0;
		int type ;
		int cno;
		int[] max ;
		int[] allocation;
		int[] available;
		Scanner input = new Scanner(System.in);
		System.out.println("请输入资源种类 :");
		type = input.nextInt();
		max = new int[type];
		allocation = new int[type];
		available = new int[type];
		System.out.println("请输入客户个数:");
		cno = input.nextInt();
		for(int k=0; k<type; k++){
			System.out.println("请输入银行家的available");
			available[k] = input.nextInt(); 
		}
		 banker = new Banker(available,type);
		for(int i=0; i<cno; i++)
		{
			for(int c=0; c<type; c++){
				System.out.println("请输入第"+i+"位客户的max");
				max[c] = input.nextInt();
			}
			for(int j=0; j<type; j++){
				System.out.println("请输入第"+i+"位客户的allocation");
				allocation[j] = input.nextInt();
			}
			name = i+1+"号";
			Customer cus = new Customer(name, type, max, allocation);
			customers.add(i, cus);
		}
		showCustomer(customers);
		System.out.println("输入1检查系统的安全性,输入2向系统发出请求,其他退出");
		myCase = input.nextInt();
		switch(myCase){
		case 1:			//检查系统的安全性
			if(banker.safeCheck(customers, banker)){
			System.out.println("safe!");
		   }else{
			   System.out.println("not safe!");
		   }
			break;
		case 2:			//
			System.out.println("Please input a name:");
			String pName = input.next();
			System.out.println("Please input the request(Press enter key after input a numer):");
			int[] request = new int[type];
			for(int dd=0; dd<type; dd++)
			{
				request[dd] = input.nextInt();
			}
			if(banker.checkSafe(customers, banker, request, pName)){
				System.out.println("the request is available!");
			}
			break;
		default:
				System.out.println("bye!");
				break;
		}
	}

	public static void showCustomer(LinkedList<Customer> customers){
		
		int[] n;
		int[] al;
		System.out.println("进程名"+"\t\t"+" need"+"  \t"+" Allocation"+"\t "+" finish");
		for(int i=0; i<customers.size(); i++)
		{
			Customer cus = customers.get(i);
			n = cus.getNeed();
			al = cus.getAllocation();
			System.out.print(" "+cus.getName()+"\t");
	
			System.out.print("\t");
			/*need*/
			for(int j=0; j<al.length; j++){
				System.out.print(n[j]+" ");
			}
			System.out.print("\t");
			/*allocation*/
			for(int j=0; j<al.length; j++){
				System.out.print(al[j]+" ");
			}
			System.out.print("\t");
			System.out.println(cus.isFinish()+"  ");
		}
	}
	
}

输入的格式控制不好看,有什么办法可以改善一下吗?

【实验目的】 1. 理解死锁的概念; 2. 用高级语言编和调试一个银行家算法程序,以加深对死锁的理解。 【实验准备】 1. 产生死锁的原因  竞争资源引起的死锁  进程推进顺序不当引起死锁 2.产生死锁的必要条件  互斥条件  请求和保持条件  不剥夺条件  环路等待条件 3.处理死锁的基本方法  预防死锁  避免死锁  检测死锁  解除死锁 【实验内容】 1. 实验原理 银行家算法是从当前状态出发,逐个按安全序列检查各客户中谁能完成其工作,然后假定其完成工作且归还全部贷款,再进而检查下一个能完成工作的客户。如果所有客户都能完成工作,则找到一个安全序列,银行家才是安全的。与预防死锁的几种方法相比较,限制条件少,资源利用程度提高了。缺点:该算法要求客户数保持固定不变,这在多道程序系统中是难以做到的;该算法保证所有客户在有限的时间内得到满足,但实时客户要求快速响应,所以要考虑这个因素;由于要寻找一个安全序列,实际上增加了系统的开销.Banker algorithm 最重要的一点是:保证操作系统的安全状态!这也是操作系统判断是否分配给一个进程资源的标准!那什么是安全状态?举个小例子,进程P 需要申请8个资源(假设都是一样的),已经申请了5个资源,还差3个资源。若这个时候操作系统还剩下2个资源。很显然,这个时候操作系统无论如何都不能再分配资源给进程P了,因为即使全部给了他也不够,还很可能会造成死锁。若这个时候操作系统还有3个资源,无论P这一次申请几个资源,操作系统都可以满足他,因为操作系统可以保证P不死锁,只要他不把剩余的资源分配给别人,进程P就一定能顺利完成任务。 2.实验题目 设计五个进程{P0,P1,P2,P3,P4}共享三类资源{A,B,C}的系统,{A,B,C}的资源数量分别为10,5,7。进程可动态地申请资源和释放资源,系统按各进程的申请动态地分配资源。要求程序具有显示和打印各进程的某一时刻的资源分配表和安全序列;显示和打印各进程依次要求申请的资源号以及为某进程分配资源后的有关资源数据。 3.算法描述 我们引入了两个向量:Resourse(资源总量)、Available(剩余资源量) 以及两个矩阵:Claim(每个进程的最大需求量)、Allocation(已为每个进程分配的数量)。它们共同构成了任一时刻系统对资源的分配状态。 向量模型: R1 R2 R3 矩阵模型: R1 R2 P1 P2 P3 这里,我们设置另外一个矩阵:各个进程尚需资源量(Need),可以看出 Need = Claim – Allocation(每个进程的最大需求量-剩余资源量) 因此,我们可以这样描述银行家算法: 设Request[i]是进程Pi的请求向量。如果Request[i , j]=k,表示Pi需k个Rj类资源。当Pi发出资源请求后,系统按下述步骤进行检查: (1) if (Request[i]<=Need[i]) goto (2); else error(“over request”); (2) if (Request[i]<=Available[i]) goto (3); else wait(); (3) 系统试探性把要求资源分给Pi(类似回溯算法)。并根据分配修改下面数据结构中的值。 剩余资源量:Available[i] = Available[i] – Request[i] ; 已为每个进程分配的数量: Allocation[i] = Allocation[i] + Request[i]; 各个进程尚需资源量:Need[i] = Need[i]-Request[i]; (4) 系统执行安全性检查,检查此次资源分配后,系统是否处于安全状态。若安全,才正式将资源分配给进程以完成此次分配;若不安全,试探方案作废,恢复原资源分配表,让进程Pi等待。 系统所执行的安全性检查算法可描述如下: 设置两个向量:Free、Finish 工作向量Free是一个横向量,表示系统可提供给进程继续运行所需要的各类资源数目,它含有的元素个数等于资源数。执行安全算法开始时,Free = Available .标记向量Finish是一个纵向量,表示进程在此次检查中中是否被满足,使之运行完成,开始时对当前未满足的进程做Finish[i] = false;当有足够资源分配给进程(Need[i]<=Free)时,Finish[i]=true,Pi完成,并释放资源。 (1)从进程集中找一个能满足下述条件的进程Pi ① Finish[i] == false(未定) ② Need[i] D->B->A A 1 6 B 1 5 C 2 4 D 4 7 Available = (2) ; Resourse = (10) ; 测试结果如下 process number:5 resource number:4 resource series:6 3 4 2 assined matrix:p0:3 0 1 1 p1:0 1 0 0 p2:1 1 1 0 p3:1 1 0 1 p4:0 0 0 0 needed matrix: p0:1 1 0 0 p1:0 1 1 2 p2:3 1 0 0 p3:0 0 1 0 p4:2 1 1 0 p3-->p4-->p0-->p2-->p1 p3-->p4-->p0-->p1-->p2 p3-->p0-->p4-->p2-->p1 p3-->p0-->p4-->p1-->p2 p3-->p0-->p2-->p4-->p1 p3-->p0-->p2-->p1-->p4 p3-->p0-->p1-->p4-->p2 p3-->p0-->p1-->p2-->p4 it is safe,and it has 8 solutions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值