美文网首页
预防死锁的银行家算法

预防死锁的银行家算法

作者: lenny611 | 来源:发表于2018-11-29 18:25 被阅读0次

package experiment;

import java.util.LinkedList;

import java.util.Queue;

import java.util.Scanner;

class Process

{

private String Name; //名称

private int []Allocation;//占用的资源

private int []Need;//仍需要的资源

private boolean Finish;//是否完成

private int LoopCount;

Process(){}

Process(String Name,int []Allocation,int []Need)

{

this.Name=Name;

this.Allocation=Allocation;

this.Need=Need;

}

public String getName() {

return Name;

}

public int[] getAllocation() {

return Allocation;

}

public int[] getNeed() {

return Need;

}

public int getLoopCount() {

return LoopCount;

}

public boolean isFinish() {

return Finish;

}

public void setName(String name) {

Name = name;

}

public void setAllocation(int[] allocation) {

Allocation = allocation;

}

public void setNeed(int[] need) {

Need = need;

}

public void setFinish(boolean finish) {

Finish = finish;

}

public void setLoopCount(int LoopCount) {

this.LoopCount = LoopCount;

}

public void SafeOrder(Process[]process,int []Available)

{

//求安全序列

/*

  从第一个进程开始,对每一个进程,去判断系统剩余的资源能不能满足该进程仍需要的资源,

  能则分配给该进程,然后将该进程名加入到安全序列,并将该进程原来占用的资源加入到系统 剩余资源;

不能则跳过该进程,对下一个进程进行判断;

  */

String safeOrder="";

Queue<Integer> queue = new LinkedList<Integer>(); //创建一个队列,用于队列的排队

for(int i=0;i<process.length;i++)

{

queue.add(i);//将进程加入队列

}

int oldSize=queue.size();//原队列数量

int ProcessCantEnough=0;//进程不能满足数量

for(int i=queue.peek();i<process.length; i=queue.peek())

{

if(oldSize==ProcessCantEnough)

{

//每一个进程都无法满足

for(int n=0;n<queue.size();n++)

{

int peek=queue.peek();

System.out.println("进程"+process[peek].Name+"无法满足");

queue.remove();

}

System.out.println("所以该新请求不能允许");

break;

}

if(Compare(process[i].getNeed(),Available)&&process[i].isFinish()==false)

{

//当前进程的请求系统可以满足

Add(process[i].getAllocation(),Available);//把进程占用的资源还给系统

safeOrder=safeOrder+process[i].getName()+"->";//把当前进程的名字加入安全序列

process[i].setFinish(true); //标记进程完成

queue.remove();//完成则将进程移除队列

            if(!queue.isEmpty())

            continue;

}

if(!Compare(process[i].getNeed(),Available))

{

//当前进程的请求系统不能满足

process[i].setLoopCount(process[i].getLoopCount()+1);//当前进程第一次不能满足

int peek=queue.peek();  //取队首

queue.remove();  //移除队首

queue.add(peek);//加入队尾

ProcessCantEnough++;//不能满足进程数量+1

}

if(queue.isEmpty())

break;

}

if(queue.size()==0)

System.out.println("当前状态安全  ;"+"安全序列为:"+new String(safeOrder.substring(0, safeOrder.length()-2)));  

}

public boolean Compare(int []Need,int []Available )

{

if(Need.length==Available.length)

{

for(int i=0;i<Need.length;i++)

{

if(Available[i]>=Need[i])

continue;

else

return false;

}

return true;

}

else return false;

}

public int[] Add(int []Allocation,int []Available)

{

if(Allocation.length==Available.length)

{

for(int i=0;i<Allocation.length;i++)

Available[i]+=Allocation[i];

}

else

System.out.println("长度不一样,相加失败");

return Available;

}

public void Subtraction (int []Allocation,int []Available)

{

if(Allocation.length==Available.length)

{

for(int i=0;i<Allocation.length;i++)

Available[i]-=Allocation[i];

}

else

System.out.println("长度不一样,相减失败");

}

public void Request(Process[]process,int []Available,int []newNeed,String Name)

{

//安全则提出新一轮的安全请求

for(int i=0;i<process.length;i++)

{

if(process[i].getName().equals(Name))

{

Add(newNeed,process[i].getNeed());

Subtraction(newNeed,Available);

break;

}

}

new Process().SafeOrder(process,Available);

}

public void Print_Allocation_Need(Process[]process,int []Available)

{

System.out.println("进程的名称"+"        "+"进程占用的资源"+"            "+"  进程仍需要的资源");

for(int i=0;i<process.length;i++)

{

System.out.print("  "+process[i].getName()+"      ");

for(int j=0;j<process[i].Allocation.length;j++)

System.out.print(process[i].Allocation[j]+" ");

System.out.print("        ");

for(int j=0;j<process[i].getNeed().length;j++)

System.out.print(+process[i].Need[j]+" ");

System.out.println();

}

System.out.print("系统剩余资源为:");

    for(int i=0;i<Available.length;i++)

    System.out.print(Available[i]+" ");

    System.out.println();

}

}

public class Experiment {

public static void main(String[] args) {

// System.out.print("请输入进程个数:");

Scanner Input=new Scanner(System.in);

// int num=Input.nextInt();

// Process[] process=new  Process[num];

// System.out.print("请输入资源种类:");

// int typeCount=Input.nextInt();

// for(int i=0;i<process.length;i++)

// {

// System.out.println("请输入第"+(i+1)+"个进程的名称,已分配的资源,还需要分配的资源");

// String Name=Input.next();

// String getAllocation=Input.next();

//     String getNeed=Input.next();

//     int Allocation[]=new int [typeCount];

//       for(int j=0;j<typeCount;j++)

//       Allocation[j]=(int)(getAllocation.charAt(j)-'0');

//       int Need[]=new int [typeCount];

//       for(int j=0;j<typeCount;j++)

//       Need[j]=(int)(getNeed.charAt(j)-'0');

//       process[i]=new Process(Name,Allocation,Need);

// }

// System.out.println("请输入系统剩余资源:");

// String getAvailable=Input.next();

// int Available[]=new int [typeCount];

// for(int i=0;i<typeCount;i++)

// Available[i]=(int)(getAvailable.charAt(i)-'0');

// int []Available_copy=Available.clone();

// new Process().SafeOrder(process,Available);  

// new Process().Print_Allocation_Need(process,Available_copy);

// System.out.println("请输入新一轮的请求: 进程名 + 需求的资源"+"(提示:需求的资源最好不要超过系统剩余资源)");

// String name=Input.next();

// String getNewNeed=Input.next();

// int newNeed[]=new int [typeCount];

// for(int i=0;i<typeCount;i++)

// newNeed[i]=(int)(getNewNeed.charAt(i)-'0');

// new Process().Request(process,Available_copy,newNeed,name);

// Input.close();

  int Allocation0[]= {0,0,3,2};

  int Allocation1[]= {1,0,0,0};

  int Allocation2[]= {1,3,5,4};

  int Allocation3[]= {0,3,3,2};

  int Allocation4[]= {0,0,1,4};  

  int Need0[]= {0,0,1,2};

  int Need1[]= {1,7,5,0};

  int Need2[]= {2,3,5,6};

  int Need3[]= {0,6,5,2};

  int Need4[]= {0,6,5,6};

  Process[] process=new  Process[5];

  process[0]=new Process("A",Allocation0,Need0);

  process[1]=new Process("B",Allocation1,Need1);

  process[2]=new Process("C",Allocation2,Need2);

  process[3]=new Process("D",Allocation3,Need3);

  process[4]=new Process("E",Allocation4,Need4);

  int Available[]= {1,6,2,2};

  int []Available_copy=Available.clone();

  new Process().SafeOrder(process,Available);  

  new Process().Print_Allocation_Need(process,Available_copy);

//   System.out.println("请输入新一轮的请求: 进程名 + 需求的资源"+"(提示,需求的资源最好不要超过系统剩余资源)");

//

//   String name=Input.next();

// String getNewNeed=Input.next();

// int newNeed[]=new int [4];

// for(int i=0;i<4;i++)

// newNeed[i]=(int)(getNewNeed.charAt(i)-'0');

// new Process().Request(process,Available_copy,newNeed,name);

  int []newNeed= {1,2,2,2};

  new Process().Request(process,Available_copy,newNeed,"C");

}

}

相关文章

  • 银行家算法

    银行家算法是一种预防死锁的算法。具体算法步骤可以参考百度百科:银行家算法 例子:某系统有A、B、C、D , 4类...

  • 操作系统复习(自用)1

    2012级:操作系统 第一题是用英文解释概念 比如进程线程等等 还有死锁以及死锁检测和死锁预防算法 就是那个银行家...

  • 银行家算法笔记

    死锁 在了解银行家算法前,有必要了解一下死锁。因为银行家算法是用于避免死锁的。 什么是死锁? 死锁是指两个或两个以...

  • 银行家实现C++算法网络爬虫无死锁调度!

    一、银行家算法与死锁 银行家算法是一种最有代表性的避免死锁的算法。在避免死锁方法中允许进程动态地申请资源,但系统在...

  • 预防死锁的银行家算法

    package experiment; import java.util.LinkedList; import j...

  • 死锁的预防算法

    银行家算法银行家算法是一种最有代表性的避免[死锁]的算法。在避免死锁方法中允许进程动态地申请资源,但系统在进行资源...

  • 第二章 数据查找与资源分配算法——银行家算法

    2.4 银行家算法 银行家算法时一种资源分配算法,在操作系统理论中它是一个避免死锁的算法,是以银行借贷系统的分配策...

  • 银行家算法

    Dijkstra(1965)提出了一种能够避免死锁的调度算法,称为银行家算法(banker's algorithm...

  • 银行家算法

    银行家算法是避免进程死锁的方法之一。那什么是银行家算法呢? 银行家,他们想的是我把钱贷出去,能不能收的回来,能不能...

  • 死锁 & 银行家算法

    死锁是多线程环境中由于对资源竞争分配不合理而产生的阻塞行为,银行家算法是一种动态避免死锁的策略。 I、死锁 1.1...

网友评论

      本文标题:预防死锁的银行家算法

      本文链接:https://www.haomeiwen.com/subject/kyafcqtx.html