联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> javajava

日期:2018-09-05 03:42

import java.util.Scanner;

public class A {

   final static int M=4; //进程数

   final static int N=3; //资源种类数

   static int available[]={0,0,0};                         //各进程可利用的资源情况

   static int max[][]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};          //各进程最大需求的资源情况

   static int allocation[][]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};   //各进程已经分配的资源情况

   static int need[][]={{0,0,0},{0,0,0},{0,0,0},{0,0,0}};         //各进程仍需要的资源情况

   static int request[]={0,0,0};

   public static void main(String[] args) {

       Scanner in=new Scanner(System.in);

       int m=0,n=0,i=0,j=0;

       System.out.print(" 死锁的检测与解除 ");

       input();

       output();

       enter1(n);

       enter2(n,j);

       System.out.println();

       change(n);

       if(checksafe(n)==1)

       {

           check(m,n,i,j);

           output();

       }

   }

   static void enter2(int n,int j){

       Scanner in=new Scanner(System.in);

       System.out.printf("请输入进程%d对3类资源的需求量:",n);

       for(j=0;j<3;j++)

       {

           request[j]=in.nextInt();

           if(request[j]>need[n][j])

           {

               System.out.println("申请的资源数量大于自身需求的最大值!请重新输入!");

               enter2(n,j);

           }

           else

               {

                   if(request[j]>available[j])

                   {

                       System.out.println("申请的资源数量大于可用的资源数量!请重新输入!");

                       enter2(n,j);

                   }

               }

       }

   }

   static void enter1(int n){

       Scanner in=new Scanner(System.in);

       System.out.print("请输入请求资源的进程号:");


       n=in.nextInt();

   if(n<0||n>=M)

{

               System.out.printf("您输入的进程号不存在,请重新输入!\n");

enter1(n);

}

   }

   static void check(int m,int n,int i,int j){

       m=findmany();

       System.out.println();

       System.out.printf("撤销进程%d所占用的资源!\n",m);

       for(j=0;j<3;j++)

       {

           available[j]+=allocation[m][j];

           need[m][j]+=allocation[m][j];

           allocation[m][j]=0;

       }

       n=findfew();

       for(j=0;j<3;j++)

       {

           request[j]=need[n][j];

       }

       change(n);

       if(checksafe(n)==1)  check(m,n,i,j);

   }

   static void input()

   {

       Scanner in=new Scanner(System.in);

       int i=0,j=0;

       System.out.println("\t\t\t\tA  B  C");

       System.out.printf("请输入进程0对3类资源的最大需求:");

       for(j=0;j<3;j++)

       {

           max[0][j]=in.nextInt();

       }

       System.out.printf("请输入进程1对3类资源的最大需求:");

       for(j=0;j<3;j++)

       {

           max[1][j]=in.nextInt();

       }

       System.out.printf("请输入进程2对3类资源的最大需求:");


       for(j=0;j<3;j++)

       {

           max[2][j]=in.nextInt();

       }

       System.out.printf("请输入进程3对3类资源的最大需求:");

       for(j=0;j<3;j++)

       {

           max[3][j]=in.nextInt();

       }

       System.out.printf("请输入进程0已分配的3类资源量:");

       for(j=0;j<3;j++)

       {

           allocation[0][j]=in.nextInt();

       }

       System.out.printf("请输入进程1已分配的3类资源量:");

       for(j=0;j<3;j++)

       {

           allocation[1][j]=in.nextInt();

       }

       System.out.printf("请输入进程2已分配的3类资源量:");

       for(j=0;j<3;j++)

       {

           allocation[2][j]=in.nextInt();

       }

       System.out.printf("请输入进程3已分配的3类资源量:");

       for(j=0;j<3;j++)

       {

           allocation[3][j]=in.nextInt();

       }

       System.out.printf("请输入可利用的3类资源量:");


       for(j=0;j<3;j++)

       {

           available[j]=in.nextInt();

       }

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

           for(j=0;j<3;j++)

           {

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

           }

       System.out.println();

   }

   /***************显示当前的资源状况***************/

   static void output()

   {

       int i=0,j=0;

       System.out.printf("该时刻3类资源的分配情况如下:\n");

       System.out.printf("\tMAX\t\tALOCAT\t\tNEED\t\tAVAILABLE\n");

       System.out.printf("进程0:\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",max[0][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",allocation[0][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",need[0][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",available[j]);

       }

       System.out.println();

       System.out.printf("进程1:\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",max[1][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",allocation[1][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",need[1][j]);

       }

       System.out.printf("\n");

       System.out.printf("进程2:\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",max[2][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",allocation[2][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",need[2][j]);

       }

       System.out.printf("\n");

       System.out.printf("进程3:\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",max[3][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",allocation[3][j]);

       }

       System.out.printf("\t");

       for(j=0;j<3;j++)

       {

           System.out.printf("%d  ",need[3][j]);

       }

       System.out.printf("\n");

   }

   /***************给某进程分配资源***************/

   static void change(int n)

   {

       int j;

       for(j=0;j<N;j++)

       {

           available[j]=available[j]-request[j];

           allocation[n][j]=allocation[n][j]+request[j];

           need[n][j]=need[n][j]-request[j];

       }

   }

   /***************找出占用资源最多的进程***************/

   static int findmany()

   {

       int i=0,j=0,k=0,l=0;

       i=allocation[0][0]+allocation[0][1];

       i+=allocation[0][2];

       j=allocation[1][0]+allocation[1][1];

       j+=allocation[1][2];

       k=allocation[2][0]+allocation[2][1];

       k+=allocation[2][2];

       l=allocation[3][0]+allocation[3][1];

       l+=allocation[3][2];

       if(i>=j)

       {

           if(i>=k)

           {

               if(i>=l)

                   return 0;

               else

                   return 3;

           }

           else

           {

               if(k>=l)

                   return 2;

               else

                   return 3;

           }

       }

       else

       {

           if(j>=k)

           {

               if(j>=l)

                   return 1;

               else

                   return 3;

           }

           else

           {

               if(k>=l)

                   return 2;

               else

                   return 3;

           }

       }

   }

   /***************找出需要资源最少的进程***************/

   static int findfew()

   {

       int i=0,j=0,k=0,l=0;

       i=need[0][0]+need[0][1];

       i+=need[0][2];

       j=need[1][0]+need[1][1];

       j+=need[1][2];

       k=need[2][0]+need[2][1];

       k+=need[2][2];

       l=need[3][0]+need[3][1];

       l+=need[3][2];

       if(i<=j)

       {

           if(i<=k)

           {

               if(i<=l)

                   return 0;

               else

                   return 3;

           }

           else

           {

               if(k<=l)

                   return 2;

               else

                   return 3;

           }

       }

       else

       {

           if(j<=k)

           {

               if(j<=l)

                   return 1;

               else

                   return 3;

           }

           else

           {

               if(k<=l)

                   return 2;

               else

                   return 3;

           }

       }

   }

   /***************安全性检测***************/

   static int checksafe(int n)

   {

       int work[]=new int[3],finish[]=new int[M],que[]=new int[M];

       int i=0,k=0;

       for(i=0;i<M;i++)

           finish[i]=0;

       for(;;)

       {

           work[0]=available[0];

           work[1]=available[1];

           work[2]=available[2];

           i=n;

           while(i<M)

           {

               if(finish[i]==0&&need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[2])

               {

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

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

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

                   finish[i]=1;

                   que[k]=i;

                   k++;

                   i=0;

               }

               else

                   i++;

           }

           for(i=0;i<M;i++)

               if(finish[i]==0)

               {

                   System.out.printf("存在死锁!\n");

                   return 1;

               }

           break;

       }

       System.out.printf("\n");

       System.out.printf("检测结果:不存在死锁!\n");

       System.out.printf("安全序列为:");

       for(i=0;i<M;i++)

           System.out.printf("%d\t\t",que[i]);

       System.out.println();

       output();

       return 0;

   }

}


版权所有:留学生程序网 2020 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。