银行家算法
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://maple.blog.51cto.com/180963/55189 |
import java.io.*;
public class bank { int MAX_PROCESS = 5; //最大进程数 int MAX_COURCE = 3; //最大资源类别 int[] Available=new int[MAX_COURCE]; //可利用资源向量
int[][] Max= new int[MAX_PROCESS][MAX_COURCE]; //最大需求矩阵 int[][] Allocation=new int[MAX_PROCESS][MAX_COURCE]; //分配矩阵 int[][] Need=new int[MAX_PROCESS][MAX_COURCE]; //需求矩阵 int Request_PROCESS; //发出请求的进程
int[][] Request_COURCE = new int[MAX_PROCESS][MAX_COURCE]; //线程请求资源的数量 public bank()
{ } public void InitState()
{ System.out.println("初始化当前状态......"); Max[0][0] = 7;Max[0][1] = 5;Max[0][2] = 3; Allocation[0][0] = 0;Allocation[0][1] = 1;Allocation[0][2] = 0; Need[0][0] = 7;Need[0][1] = 4;Need[0][2] = 3; //初始化进程二
Max[1][0] = 3;Max[1][1] = 2;Max[1][2] = 2; Allocation[1][0] = 2;Allocation[1][1] = 0;Allocation[1][2] = 0; Need[1][0] = 1;Need[1][1] = 2;Need[1][2] = 2; //初始化进程三
Max[2][0] = 9;Max[2][1] = 0;Max[2][2] = 2; Allocation[2][0] = 3;Allocation[2][1] = 0;Allocation[2][2] = 2; Need[2][0] = 6;Need[2][1] = 0;Need[2][2] = 0; //初始化进程四
Max[3][0] = 2;Max[3][1] = 2;Max[3][2] = 2; Allocation[3][0] = 2;Allocation[3][1] = 1;Allocation[3][2] = 1; Need[3][0] = 0;Need[3][1] = 1;Need[3][2] = 1; //初始化进程五
Max[4][0] = 4;Max[4][1] = 3;Max[4][2] = 3; Allocation[4][0] = 0;Allocation[4][1] = 0;Allocation[4][2] = 2; Need[4][0] = 4;Need[4][1] = 3;Need[4][2] = 1; //初始化可用资源
Available[0] = 3;Available[1] = 3;Available[2] = 2; }
public void bankmethod()
{ try { System.out.println("请输入请求资源的进程(0-4):"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Request_PROCESS = Integer.parseInt(reader.readLine()); System.out.println("请输入请求资源的数量:");
for(int i=0;i<MAX_COURCE;i++)
{ Request_COURCE[Request_PROCESS][i] = Integer.parseInt(reader.readLine()); }
}catch(IOException e){} for(int i=0;i<MAX_COURCE;i++)
{ //判断Request<=Need if( Request_COURCE[Request_PROCESS][i] > Need[Request_PROCESS][i]) { System.out.println("请求资源大于需求,输入错误。:"); return; }
//判断Request<=Available
if(Request_COURCE[Request_PROCESS][i] > Available[i]) { System.out.println("请求资源大于可用资源,此进程进入等待状态。"); System.out.println("请继续下一进程........"); return; } } //修改Available, Allocation, Need
for(int i=0;i<MAX_COURCE;i++) { Available[i] = Available[i] - Request_COURCE[Request_PROCESS][i]; Allocation[Request_PROCESS][i] = Allocation[Request_PROCESS][i] + Request_COURCE[Request_PROCESS][i]; Need[Request_PROCESS][i] = Need[Request_PROCESS][i] - Request_COURCE[Request_PROCESS][i]; } if(IsSafe()) //判断当前状态是否是安全状态
{ System.out.println("系统仍处于安全状态,请求资源成功。"); System.out.println("请继续........"); } else { //非安全状态 System.out.println("系统进入不安全状态,请求资源失败。"); for(int i=0;i<MAX_COURCE;i++) { Available[i] = Available[i] + Request_COURCE[Request_PROCESS][i]; Allocation[Request_PROCESS][i] = Allocation[Request_PROCESS][i] - Request_COURCE[Request_PROCESS][i]; Need[Request_PROCESS][i] = Need[Request_PROCESS][i] + Request_COURCE[Request_PROCESS][i]; } System.out.println("请继续........"); } } public boolean IsSafe()
{ int[] work = new int[3]; boolean[] Finish = {false,false,false,false,false}; for(int i=0;i<MAX_COURCE;i++) { work[i] = Available[i]; } //寻找满足Finish[i]=false,Need[i,j]<=Work[j]的进程 int i = 0; do { boolean flag = true; //是否Need[i][j]<=work[j]
for(int j=0;j<MAX_COURCE;j++) { if(Need[i][j]>work[j]) { flag = false; break; } }
//是否Finish[i]=false且Need[i,j]<=Work[j]
if(Finish[i]==false && flag) { for(int j=0;j<MAX_COURCE;j++) { work[j] = work[j] + Allocation[i][j]; } Finish[i] = true; i = -1; //重新遍历未结束的进程
} }while(++i<MAX_PROCESS);
i = 0;
while(Finish[i]==true) { if(i == 4) return true; //是安全状态,返回true i++; } return false; //非安全状态,返回false
}
/** * @param args the command line arguments */ public static void main(String[] args) { bank mybank = new bank(); mybank.InitState(); while(true) { mybank.bankmethod(); } } } 本文出自 “梦里依稀” 博客,请务必保留此出处http://maple.blog.51cto.com/180963/55189 本文出自 51CTO.COM技术博客 |


imgrey
博客统计信息
热门文章
最新评论
友情链接
