进程管理之银行家算法

                                                银行家算法的核心机制:

 

针对3类资源5个进程的情况,设计相应的数据结构,分别表示每个进程占用各类资源的情况;编程实现安全性算法函数,

编制主函数,动态输入资源的占用情况,进程的资源申请,调用安全性函数,实现银行家算法;测试:输入可分配和不可分配的请求,测试系统的正确性。

 

 

import java.util.*; 

 

/*此类主要功能如下 

 * 1.初始化资源 

 * 2.进行死锁避免 

 * 3.检索进程请求资源是否可行 

 * */ 

class TestBanker{ 

 int m; 

 int n; 

 int[][] max; 

 int[][] max1; 

 int[][] allocation; 

 int[][] allocation1; 

 int[][] need; 

 int[][]need1; 

 int[] available; 

 int[] availablebak; 

 

 public TestBanker(){   

  Scanner s = new Scanner(System.in); 

 

  System.out.print("请依次输入系统中的进程数"); 

  m = s.nextInt(); 

  System.out.print("请依次输入系统中的资源类型数"); 

  n = s.nextInt(); 

  max =new int[m][n]; 

  max1 = new int[m][n]; 

  allocation = new int[m][n]; 

  allocation1 = new int[m][n]; 

  need = new int[m][n]; 
  need1 = new int[m][n]; 

  available = new int[n]; 

  availablebak = new int[n]; 

 

  for(int i=0;i<max.length;i++){//初始化向量MAX、ALLOCATION、NEED、AVAILABLE 

   System.out.print("请依次输入第" + i + "进程所需的最大max的各资源数"); 

   for(int j=0;j<max[i].length;j++){ 

    max[i][j] = s.nextInt(); 

    max1[i][j] = max[i][j];    } 

  }      

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

   System.out.print("请依次输入第" + i + "进程中已分配(allocation)资源的数量"); 

   for(int j=0;j<allocation[i].length;j++){ 

    allocation[i][j] = s.nextInt(); 

    allocation1[i][j] = allocation[i][j]; 

   } 

  } 
  System.out.println("自动计算得出各进程需要的资源数量"); 
  for(int i=0;i<need.length;i++){ 

   //System.out.println("请一次输入第"+i+"进程尚需的(need)资源数量"); 

   for(int j=0;j<need[i].length;j++){ 

    need[i][j] = max[i][j] - allocation[i][j]; 

    need1[i][j] = need[i][j]; 

   } 

  }      

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

   System.out.print("请输入系统中第" + i + "种可利用的资源数量"); 

   available[i] = s.nextInt(); 

   availablebak[i] = available[i]; 

  } 

  System.out.println("初始化结果============="); 

  init(); 

 

 } 

 public void init(){//输出分配资源的状态 

  System.out.println("       MAX        ALLOCATION      NEED      AVAILABLE");      

  for(int i=0;i<m;i++){ 

   System.out.print("P" + i + ": "); 

   for(int j=0;j<n;j++){ 

    if(max[i][j]>9){//如果是两位数控制格式在数字前少输出一个" "。 

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

    }else{ 

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

    }  

   } 

   System.out.print("  |   "); 

   for(int j=0;j<n;j++){ 

    if(allocation[i][j]>9){ 

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

    }else{ 

     System.out.print(" " + allocation[i][j] + " ");     } 
   } 
   System.out.print("  |   "); 
   for(int j=0;j<n;j++){ 
    if(need[i][j]>9){ 
     System.out.print(need[i][j] + " "); 

    }else{ 
     System.out.print(" " + need[i][j] + " "); 
    } 

   } 
   if(i==0){ 
    System.out.print("   |  "); 
    for(int j=0;j<n;j++){ 
     if(available[j]>9){ 
      System.out.print(available[j] + " "); 
     }else{ 

      System.out.print(" " + available[j] + " "); 
     } 

    } 

   } 
   System.out.println(); 
  }      
  System.out.println(); 

 } 
 public void securityMethods(){//死锁检测 
  int[] security = new int[m]; 
  boolean[] finish = new boolean[m]; 
  int[] tar = new int[n]; 
  int count = 0; 
  int num1 = m+1;//计数器,每循环一遍所有进程就自减1 
  int num2 = m;//计数器每遇到一个被满足的进程就自减1 
  while(num1>0){//如果num1==0则说明依次循环下来没有能够满足的进程因此终止
   for(int i=0;i<m;i++){ 
   if(finish[i]==false){//只有没有被满足的进程才可以进入内层循环 
    finish[i] = true; 
     for(int j=0;j<n;j++){       
      tar[j] = available[j] - need[i][j]; 
      if(tar[j]<0){ 
       finish[i] = false;  
      } 
     } 
     if(finish[i]==true){ 
      for(int k=0;k<n;k++){ 
       available[k] = available[k] + allocation[i][k]; 
      } 
     security[count] = i;//记录以满足的进程号 
      count++; 
      num2--;      } 
    }        
   } 
   num1--; 
   while((num2==0)&&(num1>0)){ 
    System.out.println("可以产生新的安全序列"); 
    System.out.print("安全序列为"); 
    for(int i=0;i<m;i++){ 
     if(i==(m-1)){ 
      System.out.print("P" + security[i]); 
     }else{ 
      System.out.print("P" + security[i] + "-->"); 
     }      
    } 
    System.out.println(); 
    System.out.println("=====死锁检测结束====="); 
    System.out.println(); 
    return; 
   } 
  while((num1==0)&&(num2>0)){ 
    System.out.println("没有安全序列"); 
    System.out.println("系统不能将申请的资源分配给给该进程"); 
    System.out.println("=====死锁检测结束====="); 
    System.out.println(); 
    return; 
   } 
  }  
 } 
} 
//主运行类 
public class Banker2 { 
 public static void main(String[] args) { 
	 System.out.println("欢迎使用"); 
  Scanner scanner = new Scanner(System.in); 
  TestBanker tb = new TestBanker(); 
  tb.securityMethods(); 
 } 
} 


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值