• 避免死锁的银行家算法


    死锁的定义>

       如果一组进程中的每一个进程都在等待仅由该组进程中的其他进程才能引发的事件,那仫该组进程就是死锁的.

    产生死锁的必要条件>

        1).互斥条件:进程对所分配到的资源进行排它性使用,即在一段时间内,某资源只能被一个进程占用。如果此时还有其他进程请求该资源,则请求资源只能等待,直至占有该资源的进程用毕释放.

        2).请求和保持条件:进程已经保持了至少一个资源,但又提出了新的资源请求,而该资源已经被其他进程占有,此时请求进程被保持,但对自己所获得的资源又保持不放.

        3).不可抢占条件:进程已获得的资源在未使用完之前不可被抢占,只能在使用完成后自己释放.

        4).环路等待:在发生死锁时,必然存在一个进程,资源的循环链.

       

     死锁产生的原因>

       1).竞争可重用不可抢占式的资源

       2).竞争可消耗资源

       3).进程推进顺序不当.

       可重用性资源:可供重复使用多次的资源.

       不可抢占性资源:一旦系统把某资源分配给该进程后,就不能将它强行收回,只能在进程使用完后自动释放.

       可消耗资源:又叫临时性资源,它是在进程运行期间,由进程动态的创建和消耗的.

     利用银行家算法解决死锁>

    1).银行家算法中的数据结构

          (1).可利用资源向量Available

          (2).最大需求矩阵Max

          (3).分配矩阵Allocation

          (4).需求矩阵Need

    2).银行家算法

    Request请求向量,

         (1).如果Request[i] <= Need[i][j]转下步,否则它所需要的资源数已超过它所需要的最大值

         (2).如果Request[i] <= Available[i][j]转下步,否则尚无足够资源,进程需等待  

         (3).系统试分配给进程p,并修改Available,Allocation和Need

                       Available[j] -= Request[j]

                       Allocation[i][j] += Request[j]

                       Need[i][j] -= Request[j]

         (4)系统执行安全性算法,检查此次资源分配后系统是否处于安全状态.若安全,才正式分配;否则恢复原来的分配状态,让该进程等待

    3).安全性算法

        (1).设置两个向量,工作向量Work,在执行安全性算法开始时 Work=Available;Finish:表示有足够的资源分配给进程,使之运行完成,Finish[i]=false;当有足够资源分配给进程时,再另Finish[i]=false

       (2).从进程集合中找到一个满足该条件的进程:

               Finish[i]=false

               Need[i][j] <= Work[j]

       (3).当进程获得资源后,可顺利执行,并修改Work向量和Finsh向量

              Work[i] += Allocation[i][j]

              Finish[i]=true

       (4).如果所有进程的Finish[i]=true说明系统处于安全状态,否则系统处于不安全状态.

       在实现这份代码的时候陷入了一个误区那就是当你找到了一个安全序列之后,它查找过的Finish必定会被修改为true,而Finish这个数组又是在全局定义的,所以只要找到一次正确的安全序列这个Finish所找到的位就会被置为true会一直出现找到安全序列的,所以在找到安全序列之后一定要将Finish重新赋初值false,这个问题让我排错了半天,在定义变量的时候最好不要定义为全局的。。。

        

    1. const int MAXPROCESS=100;  
    2. const int MAXRESOURCE=100;   
    3.   
    4. int Available[MAXRESOURCE];         //可用资源向量    
    5. int Max[MAXPROCESS][MAXRESOURCE];   //最大需求矩阵    
    6. int Allocation[MAXPROCESS][MAXRESOURCE];  //分配矩阵    
    7. int Need[MAXPROCESS][MAXRESOURCE];        //需求矩阵    
    8.   
    9. int Request[MAXRESOURCE];      //请求向量    
    10.    
    11. int Work[MAXRESOURCE];         //工作向量    
    12. bool Finish[MAXPROCESS];   
    13. int SafeSeries[MAXPROCESS];    //安全序列    
    14.   
    15. int n;   //进程数    
    16. int m;   //资源数    
    17.   
    18. void Init()    
    19. {    
    20.     cout<<"请输入进程总数>";    
    21.     cin>>n;  
    22.     cout<<"请输入资源种类数>";    
    23.     cin>>m;  
    24.     int i,j;    
    25.     printf("请输入%d类资源的当前可利用资源数目> ",m);  
    26.     for( i = 0; i < m; ++i )    
    27.     {    
    28.         cin >> Available[i];    
    29.     }    
    30.     printf("最大需求矩阵(%d*%d输入)> ",n,m);  
    31.     for( i = 0; i < n; ++i )    
    32.     {    
    33.         for( j = 0; j < m; ++j )    
    34.         {    
    35.             cin >> Max[i][j];    
    36.         }    
    37.     }    
    38.     printf("分配矩阵(%d*%d输入)> ",n,m);   
    39.     for( i = 0; i < n; ++i )    
    40.     {    
    41.         for( j = 0; j < m; ++j )    
    42.         {    
    43.             cin >> Allocation[i][j];    
    44.         }    
    45.     }    
    46.     printf("需求矩阵(%d*%d输入) ",n,m);   
    47.     for( i = 0; i < n; ++i )    
    48.     {    
    49.         for( j = 0; j < m; ++j )    
    50.         {    
    51.             cin >> Need[i][j];    
    52.         }    
    53.     }    
    54. }    
    55.   
    56. bool IsSafe()    
    57. {    
    58.     int i=0;  
    59.     for (i=0;i<n;++i)  
    60.     {  
    61.         if(Finish[i] == true)  
    62.             continue;  
    63.         else  
    64.             break;  
    65.     }  
    66.     if(i == n)  
    67.         return true;    //安全  
    68.     else  
    69.         return false;   //不安全  
    70. }     
    71.   
    72. bool Select(int &tmp,int tmpNeed[][MAXRESOURCE])    
    73. {    
    74.     //选择一个Finish[i]=false,Need[i]<=Work[i]   
    75.     int j=0;   
    76.     for (int i=0;i<n;++i)  
    77.     {  
    78.         if(Finish[i])  
    79.             continue;  
    80.         for (j=0;j<m;++j)  
    81.         {  
    82.             if(tmpNeed[i][j] > Work[j])  
    83.                 break;  
    84.         }  
    85.         if(j == m)  
    86.         {  
    87.             tmp=i;  
    88.             return true;  
    89.         }  
    90.     }  
    91.     return false;  
    92. }    
    93.   
    94. bool Safe(int *tmpAvail,int tmpAlloc[][MAXRESOURCE],int tmpNeed[][MAXRESOURCE])    
    95. {    
    96.     for(int i = 0; i < n; ++i)    
    97.     {    
    98.         Finish[i] = false;    
    99.     }    
    100.     for (int j = 0; j < m; ++j)    
    101.     {    
    102.         Work[j] = tmpAvail[j];    
    103.     }    
    104.     int tmp=0;    
    105.     int index = 0;    
    106.     while(Select(tmp,tmpNeed))    
    107.     {    
    108.         Finish[tmp] = true;    
    109.         for (int k = 0; k < m; ++k)    
    110.         {    
    111.             Work[k] += tmpAlloc[tmp][k];    
    112.         }    
    113.         SafeSeries[index++] = tmp;  
    114.     }    
    115.     if(IsSafe())    
    116.         return true;    //安全  
    117.     else  
    118.         return false;   //不安全  
    119. }     
    120.   
    121. void Display()  
    122. {  
    123.     int i=0;  
    124.     int j=0;  
    125.     cout<<"当前可利用的资源数目"<<endl;  
    126.     for(i = 0; i < m; ++i)    
    127.     {    
    128.         cout << Available[i]<<" ";    
    129.     }    
    130.     cout<<endl;  
    131.     cout<<"最大需求矩阵"<<endl;  
    132.     for(i = 0; i < n; ++i )    
    133.     {    
    134.         for( j = 0; j < m; ++j)    
    135.         {    
    136.             cout<<Max[i][j]<<" ";  
    137.         }    
    138.         cout<<endl;  
    139.     }    
    140.     cout<<"分配矩阵"<<endl;  
    141.     for( i = 0; i < n; ++i )    
    142.     {    
    143.         for( j = 0; j < m; ++j )    
    144.         {    
    145.             cout<<Allocation[i][j]<<" ";   
    146.         }    
    147.         cout<<endl;  
    148.     }    
    149.     cout<<"需求矩阵"<<endl;  
    150.     for( i = 0; i < n; ++i )    
    151.     {    
    152.         for( j = 0; j < m; ++j )    
    153.         {    
    154.             cout<<Need[i][j]<<" ";  
    155.         }    
    156.         cout<<endl;  
    157.     }   
    158. }  
    159.   
    160. void BankA()  
    161. {  
    162.     int i=0;  
    163.     int index=0;  
    164.     cout<<"请输入请求资源的进程下标>";  
    165.     cin>>index;  
    166.     assert(index < n && index >= 0);  
    167.     cout<<"请输入当前的请求资源>"<<endl;  
    168.     for (i=0;i<m;++i)  
    169.     {  
    170.         cin>>Request[i];  
    171.     }  
    172.     for (i=0;i<m;++i)  
    173.     {  
    174.         if(Request[i] <= Need[index][i])  
    175.         {  
    176.             continue;  
    177.         }  
    178.         else  
    179.         {  
    180.             cout<<"第一次试分配失败"<<endl;  
    181.             break;  
    182.         }  
    183.     }  
    184.     if(i < 3)   //如果第一次试分配失败则不执行后面的语句  
    185.     {  
    186.         return ;  
    187.     }  
    188.     for(i=0;i<m;++i)  
    189.     {  
    190.         if(Request[i] <= Available[i])  
    191.         {  
    192.             continue;  
    193.         }  
    194.         else  
    195.         {  
    196.             cout<<"第二次试分配失败"<<endl;  
    197.             break;  
    198.         }  
    199.     }  
    200.     if(i < 3)     //如果第二次试分配失败则不同执行后面的语句  
    201.     {  
    202.         return ;  
    203.     }  
    204.     //开始试分配  
    205.     int tmpAvail[MAXRESOURCE]={0};  
    206.     int tmpAlloc[MAXPROCESS][MAXRESOURCE]={0};  
    207.     int tmpNeed[MAXPROCESS][MAXRESOURCE]={0};  
    208.     memmove(tmpAvail,Available,sizeof(int)*MAXRESOURCE);  
    209.     memmove(tmpAlloc,Allocation,sizeof(int)*MAXPROCESS*MAXRESOURCE);  
    210.     memmove(tmpNeed,Need,sizeof(int)*MAXPROCESS*MAXRESOURCE);  
    211.     for (int i=0;i<m;++i)  
    212.     {  
    213.         tmpAvail[i] -= Request[i];  
    214.         tmpAlloc[index][i] += Request[i];  
    215.         tmpNeed[index][i] -= Request[i];  
    216.     }  
    217.     //开始执行安全性算法  
    218.     bool ret=Safe(tmpAvail,tmpAlloc,tmpNeed);  
    219.     if(ret == true)  
    220.     {  
    221.         //如果试分配成功则更新Available,Allocation,Allocation的状态  
    222.         memmove(Available,tmpAvail,sizeof(int)*MAXRESOURCE);  
    223.         memmove(Allocation,tmpAlloc,sizeof(int)*MAXPROCESS*MAXRESOURCE);  
    224.         memmove(Need,tmpNeed,sizeof(int)*MAXPROCESS*MAXRESOURCE);  
    225.         cout<<"进程p"<<index<<"请求资源允许"<<endl;  
    226.     }  
    227.     else  
    228.     {  
    229.         //只要试分配失败则将Finish置为false  
    230.         for(int i = 0; i < n; ++i)    
    231.         {    
    232.             Finish[i] = false;    
    233.         }    
    234.         cout<<"第三次试分配失败"<<endl;  
    235.     }  
    236. }  
    237.   
    238. void Menu()  
    239. {  
    240.     cout<<"*************银行家算法*************"<<endl;  
    241.     cout<<"**********1.测试安全性代码**********"<<endl;  
    242.     cout<<"**********2.测试银行家算法**********"<<endl;  
    243.     cout<<"**********3.初始化******************"<<endl;  
    244.     cout<<"**********4.打印矩阵****************"<<endl;  
    245.     cout<<"**********0.退出********************"<<endl;  
    246. }  

       test.cpp

       

    1. void testBank()  
    2. {  
    3.     int index=0;  
    4.     int flag=1;  
    5.     while (flag)  
    6.     {  
    7.         Menu();  
    8.         cout<<"请输入您的选择"<<endl;  
    9.         cin>>index;  
    10.         switch(index)  
    11.         {  
    12.         case 1:  
    13.             Safe(Available,Allocation,Need);  
    14.             if(IsSafe())    
    15.             {    
    16.                 cout<<"该时刻是安全的,安全序列为>";   
    17.                 for (int i = 0; i < n ; ++i)    
    18.                 {    
    19.                     printf("p%d->",SafeSeries[i]);  
    20.                 }    
    21.                 cout<<"end"<<endl;  
    22.                 //分配成功将Finish的值置为false  
    23.                 for(int i = 0; i < n; ++i)    
    24.                 {    
    25.                     Finish[i] = false;    
    26.                 }    
    27.             }    
    28.             else    
    29.             {    
    30.                 cout<<"该时刻是不安全的"<<endl;  
    31.             }   
    32.             break;  
    33.         case 2:  
    34.             BankA();  
    35.             if(IsSafe())    
    36.             {    
    37.                 cout<<"安全序列为>";   
    38.                 for (int i = 0; i < n ; ++i)    
    39.                 {    
    40.                     printf("p%d->",SafeSeries[i]);  
    41.                 }    
    42.                 cout<<"end"<<endl;  
    43.                 //分配成功将Finish的值置为false  
    44.                 for(int i = 0; i < n; ++i)    
    45.                 {    
    46.                     Finish[i] = false;    
    47.                 }    
    48.             }    
    49.             else    
    50.             {    
    51.                 cout<<"进程请求资源不被允许"<<endl;  
    52.             }   
    53.             break;  
    54.         case 3:  
    55.             Init();  
    56.             break;  
    57.         case 4:  
    58.             Display();  
    59.             break;  
    60.         case 0:  
    61.             flag=0;  
    62.             break;  
    63.         default:  
    64.             cout<<"您的输入错误,请重新输入"<<endl;  
    65.             break;  
    66.         }  
    67.     }  
    68. }  

      
     

      虽然搞了一下午写完整了这个银行家算法,整体效果还算满意,但是作为网络工程专业的学生今年虽然学了java的GUI但是仍然不会用图形用户界面(GUI)来对这个程序做一个界面出来,感觉好悲催哭,以后一定努力学习这方面的知识

      实现效果

       

         

         

        

        

        

    下面是java代码实现
     
    1. import java.util.Scanner;  
    2.   
    3. public class Bankers{  
    4.     private int need[][],allocate[][],max[][],avail[][],np,nr;  
    5.       
    6.     private void input(){  
    7.      Scanner sc=new Scanner(System.in);  
    8.      System.out.print("Enter no. of processes and resources : ");  
    9.      np=sc.nextInt();  //no. of process  
    10.      nr=sc.nextInt();  //no. of resources  
    11.      need=new int[np][nr];  //initializing arrays  
    12.      max=new int[np][nr];  
    13.      allocate=new int[np][nr];  
    14.      avail=new int[1][nr];  
    15.        
    16.      System.out.println("Enter allocation matrix -->");  
    17.      for(int i=0;i<np;i++)  
    18.           for(int j=0;j<nr;j++)  
    19.          allocate[i][j]=sc.nextInt();  //allocation matrix  
    20.         
    21.      System.out.println("Enter max matrix -->");  
    22.      for(int i=0;i<np;i++)  
    23.           for(int j=0;j<nr;j++)  
    24.          max[i][j]=sc.nextInt();  //max matrix  
    25.         
    26.         System.out.println("Enter available matrix -->");  
    27.         for(int j=0;j<nr;j++)  
    28.          avail[0][j]=sc.nextInt();  //available matrix  
    29.           
    30.         sc.close();  
    31.     }  
    32.       
    33.     private int[][] calc_need(){  
    34.        for(int i=0;i<np;i++)  
    35.          for(int j=0;j<nr;j++)  //calculating need matrix  
    36.           need[i][j]=max[i][j]-allocate[i][j];  
    37.          
    38.        return need;  
    39.     }  
    40.    
    41.     private boolean check(int i){  
    42.        //checking if all resources for ith process can be allocated  
    43.        for(int j=0;j<nr;j++)   
    44.        if(avail[0][j]<need[i][j])  
    45.           return false;  
    46.      
    47.     return true;  
    48.     }  
    49.   
    50.     public void isSafe(){  
    51.        input();  
    52.        calc_need();  
    53.        boolean done[]=new boolean[np];  
    54.        int j=0;  
    55.   
    56.        while(j<np){  //until all process allocated  
    57.        boolean allocated=false;  
    58.        for(int i=0;i<np;i++)  
    59.         if(!done[i] && check(i)){  //trying to allocate  
    60.             for(int k=0;k<nr;k++)  
    61.             avail[0][k]=avail[0][k]-need[i][k]+max[i][k];  
    62.          System.out.println("Allocated process : "+i);  
    63.          allocated=done[i]=true;  
    64.                j++;  
    65.              }  
    66.           if(!allocated) break;  //if no allocation  
    67.        }  
    68.        if(j==np)  //if all processes are allocated  
    69.         System.out.println(" Safely allocated");  
    70.        else  
    71.         System.out.println("All proceess cant be allocated safely");  
    72.     }  
    73.       
    74.     public static void main(String[] args) {  
    75.   new Bankers().isSafe();  
    76.     }  
    77. }  
    78. --------------------------------------------------------------------------------------------------------------------------  
    79. Output  
    80. --------------------------------------------------------------------------------------------------------------------------  
    81. Enter no. of processes and resources : 4  
    82. Enter allocation matrix -->  
    83. 1  
    84. 3  
    85. 0  
    86. Enter max matrix -->  
    87. 2  
    88. 4  
    89. 0  
    90. Enter available matrix -->  
    91. 2  
    92. Allocated process : 0  
    93. Allocated process : 1  
    94. Allocated process : 2  
    95. Safely allocated 
  • 相关阅读:
    资源放送丨《Oracle存储过程中的性能瓶颈点》PPT&视频
    警示:一个update语句引起大量gc等待和业务卡顿
    周末直播丨细致入微
    Java VS Python:哪个未来发展更好?
    【LeetCode】279.完全平方数(四种方法,不怕不会!开拓思维)
    事件驱动
    Android初级教程以动画的形式弹出窗体
    Android简易实战教程--第五话《开发一键锁屏应用》
    迎战大数据-Oracle篇
    Android初级教程获取手机位置信息GPS与动态获取最佳方式
  • 原文地址:https://www.cnblogs.com/sessionbest/p/8706555.html
Copyright © 2020-2023  润新知