C++版银行家算法

完整源代码:http://yuncode.net/code/c_50797f9ec7ad094

 

算法:

1.试分配

2.安全性测试

3.如果找到一个安全序列,过安全检测,分配完成;  否则不能过安全检测,系统已恢复试分配前状态

 

 

  
  
  
  
  1. #include<iostream>  
  2. using namespace std;  
  3. int avaResour[3]={3,3,2};  
  4. int allocation[5][3]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};  
  5. int maxRequest[5][3]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};  
  6. int nneed[5][3]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};  
  7. void rrequest();  
  8. bool safeTest();  
  9.  
  10. int main()  
  11. {  
  12.     rrequest();  
  13.     return 0;  
  14. }  
  15.  
  16. void rrequest()  
  17. {  
  18.     int sqjc=0,i=0,l=0,a=0,b=0,c=0;  
  19.     char contn=0;  
  20.     cout<<endl<<"进程个数:5   资源个数:3"<<endl<<"可用资源向量Available:"<<endl;  
  21.     for(i=0;i<3;i++)  
  22.         cout<<avaResour[i]<<"       ";  
  23.     cout<<endl<<"最大需求矩阵Max:"<<endl;  
  24.     for(i=0;i<5;i++)  
  25.     {  
  26.         for(l=0;l<3;l++)  
  27.             cout<<maxRequest[i][l]<<"       ";  
  28.         cout<<endl;  
  29.     }  
  30.     cout<<"已分配矩阵Allocation:"<<endl;  
  31.     for(i=0;i<5;i++)  
  32.     {  
  33.         for(l=0;l<3;l++)  
  34.             cout<<allocation[i][l]<<"       ";  
  35.         cout<<endl;  
  36.     }  
  37.     cout<<"需求矩阵Need:"<<endl;  
  38.     for(i=0;i<5;i++)  
  39.     {  
  40.         for(l=0;l<3;l++)  
  41.             cout<<nneed[i][l]<<"       ";  
  42.         cout<<endl;  
  43.     }  
  44.     cout<<"输入发起请求的进程(0-4): ";  
  45.     cin>>sqjc;  
  46.     while(sqjc>4||sqjc<0)  
  47.     {  
  48.         cout<<endl<<"不要乱输! ^_^"<<endl;  
  49.         cout<<endl<<"输入发起请求的进程(0-4): ";  
  50.         cin>>sqjc;  
  51.     }  
  52.     cout<<"输入请求资源的数目,按照这样的格式输入 x x x: ";  
  53.     cin>>a>>b>>c;  
  54.     if(a<=nneed[sqjc][0] && b<=nneed[sqjc][1] && c<=nneed[sqjc][2])  
  55.     {  
  56.         cout<<endl<<"开始执行银行家算法..."<<endl;  
  57.         if(a<=avaResour[0] && b<=avaResour[1] && c<=avaResour[2])  
  58.         {  
  59.             avaResour[0]=avaResour[0]-a; avaResour[1]=avaResour[1]-b;   
  60.             avaResour[2]=avaResour[2]-c;  
  61.             allocation[sqjc][0]=allocation[sqjc][0]+a;  
  62.             allocation[sqjc][1]=allocation[sqjc][1]+b;  
  63.             allocation[sqjc][2]=allocation[sqjc][2]+c;  
  64.             nneed[sqjc][0]=nneed[sqjc][0]-a;  
  65.             nneed[sqjc][1]=nneed[sqjc][1]-b;  
  66.             nneed[sqjc][2]=nneed[sqjc][2]-c;  
  67.             cout<<endl<<"试分配完成..."<<endl;   
  68.             if(safeTest())  
  69.             {  
  70.                 cout<<endl<<"开始给第"<<sqjc<<"个进程分配资源..."<<endl  
  71.                     <<"分配完成,已更新Allocation_list.txt"<<endl;  
  72.                 for(i=0;i<5;i++)  
  73.                     if(nneed[i][0]==0 && nneed[i][1]==0 && nneed[i][2]==0)  
  74.                         for(l=0;l<3;l++)  
  75.                         {  
  76.                             avaResour[l]=avaResour[l]+allocation[i][l];  
  77.                             allocation[i][l]=0;  
  78.                         }  
  79.             }                             
  80.             else 
  81.             {  
  82.                 cout<<endl<<"系统已恢复试分配前状态。"<<endl;  
  83.                 avaResour[0]=avaResour[0]+a; avaResour[1]=avaResour[1]+b;  
  84.                 avaResour[2]=avaResour[2]+c;  
  85.                 allocation[sqjc][0]=allocation[sqjc][0]-a;  
  86.                 allocation[sqjc][1]=allocation[sqjc][1]-b;  
  87.                 allocation[sqjc][2]=allocation[sqjc][2]-c;  
  88.                 nneed[sqjc][0]=nneed[sqjc][0]+a;  
  89.                 nneed[sqjc][1]=nneed[sqjc][1]+b;  
  90.                 nneed[sqjc][2]=nneed[sqjc][2]+c;          
  91.             }  
  92.         }  
  93.         else 
  94.             cout<<endl<<"试分配失败,系统无足够资源"<<endl;  
  95.     }  
  96.     else 
  97.         cout<<endl<<"Error!申请的资源大于需求值"<<endl;  
  98.     xz:  
  99.     cout<<"\n\n"<<"要继续吗?(y-继续;n-终止):";  
  100.     cin>>contn;   
  101.     switch(contn)  
  102.     {  
  103.         case 'y':rrequest();  
  104.         case 'n':break;  
  105.         default:cout<<"亲,不要乱输哦"<<endl;  
  106.                 goto xz;break;  
  107.     }  
  108. }  
  109.  
  110. bool safeTest()  
  111. {  
  112.     int s=0,m=0,z=0,r[5]={0},y=0,wwork[3]={0};  
  113.     bool ffinish[5]={0};  
  114.     cout<<endl<<"进入安全性测试!"<<endl;  
  115.     for(s=0;s<3;s++)  
  116.         wwork[s]=avaResour[s];  
  117.     for(z=0;z<5;z++)  
  118.     {  
  119.         for(s=0;s<5;s++)  
  120.             if(ffinish[s]==0 && nneed[s][0]<=wwork[0] && nneed[s][1]<=wwork[1]  
  121.                 && nneed[s][2]<=wwork[2])  
  122.             {  
  123.                 for(m=0;m<3;m++)  
  124.                     wwork[m]=wwork[m]+allocation[s][m];  
  125.                 ffinish[s]=1;  
  126.                 r[y]=s;  
  127.                 y++;  
  128.             }  
  129.     }  
  130.     if(y==5)  
  131.     {  
  132.         cout<<endl<<"找到一个安全序列:";  
  133.         for(s=0;s<2;s++)  
  134.             cout<<"P"<<r[2*s]<<"--->"<<"P"<<r[2*s+1]<<"--->";  
  135.         cout<<"P"<<r[4]<<endl<<"已通过安全检测!"<<endl;  
  136.         return 1;  
  137.     }  
  138.     cout<<endl<<"没有找到安全序列!"<<endl<<"安全测试失败。"<<endl;  
  139.     return 0;  

 

你可能感兴趣的:(算法,源代码,银行家算法,云代码,yuncode)