银行家算法

import java.util.Scanner;

public class bank1 {

       static int process;//定义进程数量

       static int resource;//定义资源种类是

       static int[] available;//可利用的资源

       static int[][] max;//分别是最大的需求数、已分配的资源、需求资源

       static int[][] allocation;

       static int[][] need;

    public static void main(String[] args) {

       Scanner scanner=new Scanner(System.in);

       boolean input=true;//定义一个标志如果计算得到的need中有复数就重新输入

       while(input){

           System.out.print("请输入进程数>>");

           process=scanner.nextInt();

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

           resource=scanner.nextInt();

           System.out.print("请输入可利用资源向量>>");

           available=new int[resource];

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

              available[i]=scanner.nextInt();}

           System.out.println("请输入分配矩阵");

           allocation=new int[process][resource];

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

              System.out.print("请输入进程"+(i+1)+"已分配的资源数>>");

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

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

              }}

           System.out.println("请输入最大需求矩阵");

           max=new int[process][resource];

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

              System.out.print("请输入进程"+(i+1)+"最大需求的资源数>>");

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

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

              }   }

           need=new int[process][resource];

           boolean error=false;

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

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

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

                  if(need[i][j]<0){

                     error=true;

                  }}}

           /*

            * 如果有need小于0的就重新输入

            * */

           if(error==true){

              System.out.println("系统请求资源不能为复数,请重新输入!");continue; }

           else{input=false;}   }

       System.out.println();

       /*

        * 打印资源分配表

        * */

       System.out.println("To时刻的资源分配表");

       print();

       /**

        * 检查安全序列

        * */

    int[] work=new int[resource];//定义一个数组work用来存放可利用的资源数目

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

           work[i]=available[i];//初始化work}

       boolean[] finish=new boolean[process];//定义标志finish,表示分配资源的置为true,没有非配的置为false

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

           finish[i]=false;//初始化数组finish }

       int[] array=new int[process];//定义一个数组保存安全序列

       int num=1;

       int count1=1;

       int flag=0;

       while(num<process){

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

              if(finish[i]==false){

                  for (int j2 = 0; j2 < resource; j2++) {

                         if(need[i][j2]<=work[j2]){

                            flag++;

                         }

                  }

                  if(flag==resource){

                     for (int j3 = 0; j3 < resource; j3++) {

                         work[j3]=work[j3]+allocation[i][j3];

                     }

                      finish[i]=true;

                     array[count1-1]=i;

                     count1++;

                    

                  }

                 

              }flag=0;

           }num++;

       }

       int count=0;

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

           if(finish[i]==true){

              count++;

           }

       }

       if(count==process){

           System.out.println("存在一个安全序列:");

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

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

           }

       }

       else{System.out.println("系统处于不安全状态!");System.out.close();}

       System.out.println();

       boolean flag1=true;

       int flag11=0;

       int count0=0;

       while(flag1){

           int[] req=new int[resource];

           System.out.print("请输入您要请求资源的 进程 >>");

           int choose=scanner.nextInt();

           System.out.print("请输入该进程的请求向量>>");

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

              req[i]=scanner.nextInt();

           }

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

              if(req[i]<=need[choose][i]&&req[i]<=available[i]){

                 

                  count0++;

              }

           }

           if(count0==resource){

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

                         available[i]=available[i]-req[i];

                         allocation[choose][i]=allocation[choose][i]+req[i];

                         need[choose][i]=need[choose][i]-req[i];

                     }

                     int[] work1=new int[resource];

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

                         work1[i]=available[i];

                     }

                     boolean[] finish1=new boolean[process];

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

                         finish1[i]=false;

                     }

                     int[] array1=new int[process];

                     int num1=1;

                     int count11=1;

                     while(num1<process){

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

                            if(finish1[i]==false){

                                for (int j2 = 0; j2 < resource; j2++) {

                                       if(need[i][j2]<=work1[j2]){

                                          flag11++;

                                       }

                                }

                                if(flag11==resource){

                                   for (int j3 = 0; j3 < resource; j3++) {

                                       work1[j3]=work1[j3]+allocation[i][j3];

                                   }

                                    finish1[i]=true;

                                   array1[count11-1]=i;

                                   count11++;

                                  

                                }

                               

                            }flag11=0;

                         }num1++;

                        

                     }

                     int count2=0;

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

                         if(finish1[i]==true){

                            count2++;

                         }

                     }

                     if(count2==process){

                         System.out.println("存在一个安全序列:");

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

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

                         }

                         System.out.println();

                         print();

                         count0=0;

                         int y=0;//测试是否有进程结束

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

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

                                if(need[i][j]==0){

                                   y++;

                                }

                               

                                if(y==resource){

                                   System.out.println("P"+i+"进程已结束!");

                                   y=0;

                                   for (int j2 = 0; j2 < resource; j2++) {

                                       available[j2]=available[j2]+allocation[i][j2];

                                       max[i][j2]=0;

                                       allocation[i][j2]=0;

                                   }

                                  

                                }

                            }y=0;

                           

                         }//测试到此结束

                         print();

                         //以下是如果所有的进程都结束了就退出

                         int test=0;//

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

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

                                if(need[i][j]==0){

                                   test++;

                                }}}

                         if(test==(process*resource)){

                            System.out.println("所有进程均结束,系统关闭!");System.out.close();

                         }test=0;

                     }

                     else{System.out.println("系统处于不安全状态!强制退出系统!");System.out.close();}

                     System.out.println();

                    

           }//if结束

           else{System.out.println("出错原因可能是:1、请求资源已超过所需资源   2、资源不够   3、该进程不需要资源!");count0=0;}

           int x=0;

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

              if(available[i]<=0){

                  x++;

              }

              if(x==resource){

                  System.out.println("资源数为0,请重登系统!");

                  System.out.close();

              }}

       }//while结束 

    }

    static void print(){

        System.out.println(" 进程     max/t/tallocation/t  need/t/tavailable");

       System.out.print("P0  ");

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

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

       }

       System.out.print("   ");

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

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

       }

       System.out.print("   ");

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

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

       }

       System.out.print("   ");

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

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

       }

       System.out.println();

       for (int i = 1; i < process; i++) {

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

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

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

           }

           System.out.print("   ");

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

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

           }

           System.out.print("   ");

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

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

           System.out.println();

       }}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值