操作系统之银行家算法避免死锁

银行家算法避免死锁

要求:

  1. 完成程序数据结构的设计,数据的录入。
  2. 完成进程需求矩阵的输出,包括最大需求矩阵,尚需资源矩阵,可获得资源显示。
  3. 完成某进程请求资源试分配。
  4. 完成安全性检查。
  1. 1.         试探分配

当进程pi提出资源申请时,系统执行下列步骤:

(1)若Request[i][j]≤Need[i][j],转(2);

     否则错误返回

(2)若Request[i][j]≤Available[j],

     转(3);否则进程等待

(3)试探着把资源分配给进程Pi,则有:

     Available[j]=Available[j]-Request[i][j];� 

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

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

  1. 2.         安全性检测算法:

n  借助于死锁的安全性测试算法来实现。

n  定义布尔型向量finish[k],k=1,..,n。检测死锁算法如下:

(1)Work= Available

(2)在剩余的进程集合中查每一个进程Pk,如果Claim[k,*]- Allocation [k,*]=0,则finish[k]:=true;否则finish[k]:=false;这里k=1,..,n

(3)在剩余的进程集合中找一个进程Pk,需满足条件:

finish[k]=false&(request[*]≤Work)

找到这样的Pk便转(4);否则转(5)

(4)Work:= Work + Allocation;finish[k]:=true;然后转(3)

(5) 如果对k=1,..,n若finish[k]=true不成立,那么,系统出现了死锁,并且finish[k]=false的Pk为死锁进程。

源程序:

#include <string.h> 
#include <iostream.h> 
#define FALSE 0  
#define TRUE 1   
#define W 10   //最大进程数W=10 
#define R 20   //最大资源总数R=20 
int M;       
int N;                   
int ALL_RESOURCE[W];    
int AVAILABLE[R];     //可利用资源向量 
int MAX[W][R];   //最大需求矩阵 
int ALLOCATION[W][R]; //分配矩阵 
int NEED[W][R];       //需求矩阵 
int Request[R];    //进程请求向量  
void inputdata();    //数据输入     
void showdata();       //数据显示    
void changdata(int k);//进程请求资源数据改变 
void restoredata(int k); //数据恢复 
int  chksec(int s); //系统安全性的检测 
int  chkmax(int s); //检测最大需求       
void bank();   //检测分配的资源是否合理   


void main()  
{  
    int i,j;     
    inputdata();       
    for(i=0;i<M;i++)    
    { j=chksec(i);     
    if (j==0) 
        break;    
    }        
    if (i>=M)   
        cout<<"错误提示:经安全性检查发现,系统的初始状态不安全!!!
"<<endl;    
    else     
    {  
        cout<<"提示:经安全性检查发现,系统的初始状态安全!"<<endl;     
        bank();    
    } 
}  

void inputdata() 
{ 
    int i=0,j=0,p;      
    cout<<"请输入总进程数:"<<endl;    
    do
    {    
        cin>>M;     
        if (M>W) 
            cout<<endl<<"总进程数超过了程序允许的最大进程数,请重新输入:"<<endl;    
    }while (M>W);    
    cout<<endl;        
    cout<<"请输入资源的种类数:"<<endl;    
    do 
    {
        cin>>N;     
        if (N>R)  
            cout<<endl<<"资源的种类数超过了程序允许的最大资源种类数,请重新输入:"<<endl;   
    }while (N>R);     
    cout<<endl;     
    cout<<"请依次输入各类资源的总数量,即设置向量all_resource:"<<endl;    
    for(i=0;i<N;i++)    
        cin>>ALL_RESOURCE[i];    
    cout<<endl;        
    cout<<"请依次输入各进程所需要的最大资源数量,即设置矩阵max:"<<endl;    
    for (i=0;i<M;i++)    
    {        
        for (j=0;j<N;j++)      
        {          
            do 
            {  
                cin>>MAX[i][j];            
                if (MAX[i][j]>ALL_RESOURCE[j])            
                    cout<<endl<<"该最大资源数量超过了声明的该资源总数,请重新输入:"<<endl; 
            }while (MAX[i][j]>ALL_RESOURCE[j]);       
        }    
    }     
    cout<<endl;        
    cout<<"请依次输入各进程已经占据的各类资源数量,即设置矩阵allocation:"<<endl;    
    for (i=0;i<M;i++)    
    {     
        for (j=0;j<N;j++)     
        {      
            do
            { 
                cin>>ALLOCATION[i][j];       
                if (ALLOCATION[i][j]>MAX[i][j])     
                    cout<<endl<<"已占有的资源数量超过了声明的最大资源数量,请重新输入:"<<endl;      
            }while (ALLOCATION[i][j]>MAX[i][j]);     
        }    
    }     
    cout<<endl;         
    for (i=0;i<M;i++)     
        for(j=0;j<N;j++)      
            NEED[i][j]=MAX[i][j]-ALLOCATION[i][j];          
        for (j=0;j<N;j++)

   {  
            p=ALL_RESOURCE[j];     
            for (i=0;i<M;i++)     
            {    
                p=p-ALLOCATION[i][j];               
                AVAILABLE[j]=p;            
                if(AVAILABLE[j]<0)       
                    AVAILABLE[j]=0;     
            }    

        } 
}   

void showdata()  
{  
    int i,j;         
    cout<<"各种资源的总数量,即向量all_resource为:"<<endl;     
    cout<<" ";      
    for (j=0;j<N;j++)     
        cout<<" 资源"<<j<<": "<<ALL_RESOURCE[j];     
    cout<<endl<<endl;      
    cout<<"当前系统中各类资源的可用数量,即向量available为:"<<endl;     
    cout<<" ";      
    for (j=0;j<N;j++)     
        cout<<" 资源"<<j<<": "<<AVAILABLE[j];     
    cout<<endl<<endl;      
    cout<<"各进程还需要的资源数量,即矩阵need为:"<<endl<<endl;     
    for (i=0;i<M;i++)      
    { 
        cout<<"进程P"<<i<<":   ";       
        for (j=0;j<N;j++)    
            cout<<NEED[i][j]<<"        ";       
        cout<<endl;     
}      
    cout<<endl;         
    cout<<"各进程已经得到的资源量,即矩阵allocation为: "<<endl<<endl;     
    for (i=0;i<M;i++)      
    { 
        cout<<"进程P"<<i<<":    ";     
        for (j=0;j<N;j++)      
            cout<<ALLOCATION[i][j]<<"       ";      
        cout<<endl;     
    } 
    cout<<endl;  
}     

void changdata(int k)  
{ 
    int j;   
    for (j=0;j<N;j++)   
    {    
        AVAILABLE[j]=AVAILABLE[j]-Request[j];    
        ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];    
        NEED[k][j]=NEED[k][j]-Request[j];  
    } 
}    

void restoredata(int k)                          
{  
    int j;   
    for (j=0;j<N;j++)   
    { 
        AVAILABLE[j]=AVAILABLE[j]+Request[j];    
        ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];    
        NEED[k][j]=NEED[k][j]+Request[j];  
    } 
    
}   

int chksec(int s)  
{  
    int WORK,FINISH[W];      
    int i,j,k=0;      
    for(i=0;i<M;i++)   
        FINISH[i]=FALSE;      
    for(j=0;j<N;j++)   
    { 
        WORK=AVAILABLE[j];          
        i=s;          
        do   
        { 
            if(FINISH[i]==FALSE&&NEED[i][j]<=WORK)    
            {     
                WORK=WORK+ALLOCATION[i][j];      
                FINISH[i]=TRUE;      
                i=0;     
            }
            else     
            {  
                i++;     
            }    
        }while(i<M);         
        for(i=0;i<M;i++)     
            if(FINISH[i]==FALSE)     
            { 
                return 1;     
            }   
    }  
    return 0;              
}    

int chkmax(int s)          
{   
    int j,flag=0;  
    for(j=0;j<N;j++)   
    {   
        if (MAX[s][j]==ALLOCATION[s][j])   
        {   
            flag=1;    
            AVAILABLE[j]=AVAILABLE[j]+MAX[s][j];    
            MAX[s][j]=0;   
        }  
    } 
    return flag; 
}
   
void bank()
{       
    int i=0,j=0;        
    char flag='Y';             
    while(flag=='Y'||flag=='y')   
    {         
        i=-1;          
        while(i<0||i>=M)      
        { 
            cout<<"请输入需申请资源的进程号(从P0到P"<<M-1<<",否则重新输入!):";           
            cout<<"p";    cin>>i;           
            if(i<0||i>=M)     
                cout<<"输入的进程号不存在,重新输入!"<<endl;      
        }      
        cout<<"请输入进程P"<<i<<"申请的资源数:"<<endl;      
        for (j=0;j<N;j++)      
        {  
            cout<<"  资源"<<j<<": ";
            cin>>Request[j];             
            if(Request[j]>NEED[i][j])       
            {  
                cout<<"进程P"<<i<<"申请的资源数大于进程P"<<i<<"还需要"<<j<<"类资源的资源量!";        
                cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;        
                flag='N';        
                break;       
}       
            else       
            { 
                if(Request[j]>AVAILABLE[j])        
                {  
                    cout<<"进程P"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";         
                    cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;         
                    flag='N';         
                    break;        
                }       

            }      
}          
        if(flag=='Y'||flag=='y')      
        {  
            changdata(i);       
            if(chksec(i))       
            { 
                cout<<endl;        
                cout<<"该分配会导致系统不安全!!! 本次资源申请不成功,不予分配!!!"<<endl;        
                cout<<endl;        
                restoredata(i);       
}       
            else            
            {   
                cout<<endl;      
                cout<<"经安全性检查,系统安全,本次分配成功,且资源分配状况如下所示:"<<endl;      
                cout<<endl;     
                showdata();      
                if(chkmax(i))     
                {
                    cout<<"在资源分配成功之后,由于该进程所需的某些资源的最大需求量已经满足,"<<endl;   
                    cout<<"因此在进程结束后系统将回收这些资源!"<<endl;    
                    cout<<"在资源收回之后,各进程的资源需求和分配情况如下所示:"<<endl;      
                    showdata();      
                }          

            }           
}      
        cout<<endl;      
        cout<<" 是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: ";      
        cin>>flag;  
    }    
}

运行结果:

原文地址:https://www.cnblogs.com/gjpg/p/5513007.html