銀行家算法C++程序


此程序在Windows10    CodeBlocks17.12環境下測試運行,其他編程環境未經測試!

 

 


 

作業需求↓↓↓↓↓↓

 


 

運行效果圖如下

 

 

 

 


 

(codeblocks下載地址http://www.codeblocks.org/downloads/binaries)


 

 

C++代碼

  1 #include<iostream>
  2 #include<string>
  3 #include<stdlib.h>
  4 using namespace std;
  5 
  6 int **all,**ma,*work,**need,*a,*sum,*finish;//定義已分配矩陣,最大需求矩陣,工作向量,需求矩陣,可利用資源向量,各類資源總個數,finish標志
  7 string *pN,*rN;//定義進程名稱數組,資源名稱數組
  8 int row,col;//定義未知矩陣的行數和列數,動態輸入
  9 
 10 
 11 //------安全性算法-----
 12 void isSafe(int row,int col){
 13     int t2,t3,t4;//整型中間變量
 14     string t1;//字符型中間變量
 15     int i,j,g,k;//循環變量i,j,標志位g,k
 16 
 17     //finish標志全部置零
 18     for(i=0;i<row;i++){
 19         finish[i]=0;
 20     }
 21 
 22     //令work向量等於可利用資源向量
 23     for(j=0;j<col;j++){
 24         work[j]=a[j];
 25     }
 26 
 27     g=0;
 28     for(i=0;i<row;i++){
 29         k=0;
 30         //用以判斷某類資源的需求是否全部小於可分配資源
 31         for(j=0;j<col;j++){
 32             if(need[i][j]<=work[j])
 33                 {k++;}
 34         }
 35         if(k==col){
 36             for(j=0;j<col;j++){
 37                 work[j]=work[j]+all[i][j];
 38             }
 39 
 40 
 41             //對Need矩陣進行排序
 42             for(j=0;j<col;j++){
 43                 t2=need[i][j];
 44                 for(int temp=i;temp>g;temp--){
 45                     need[temp][j]=need[temp-1][j];
 46                 }
 47                 need[g][j]=t2;
 48             }
 49 
 50             //對已分配矩陣進行排序
 51             for(j=0;j<col;j++){
 52                 t3=all[i][j];
 53                 for(int temp=i;temp>g;temp--){
 54                     all[temp][j]=all[temp-1][j];
 55                 }
 56                 all[g][j]=t3;
 57             }
 58             t1=pN[i];
 59 
 60             //對進程名稱數組進行排序
 61             for(int temp=i;temp>g;temp--){
 62                 pN[temp]=pN[temp-1];
 63             }
 64             pN[g]=t1;
 65 
 66             finish[i]=1;//置標志位為1
 67 
 68             //對標志位finish進行排序
 69             t4=finish[i];
 70             for(int temp=i;temp>g;temp--){
 71                 finish[temp]=finish[temp-1];
 72             }
 73             finish[g]=t4;
 74             i=g;
 75             g++;
 76         }
 77 
 78     }
 79 
 80     //判斷標志位,只要有finish標志位0的進程,結束整個程序,若全為1,則輸出安全序列
 81     for(i=0;i<row;i++){
 82         if(finish[i]==0){
 83             cout<<"(不安全!!!!!!!)"<<endl<<"(程序已退出!)"<<endl;
 84             exit(1);
 85         }
 86     }
 87     cout<<"(存在安全序列為){";
 88     for(i=0;i<row;i++){
 89         cout<<pN[i]<<",";
 90     }
 91     cout<<"} 所以此時系統處於安全狀態"<<endl;
 92 }
 93 
 94 
 95 
 96 //------銀行家算法-----
 97 void request(int col){
 98     int *py,i,j;//定義t0后進程請求資源的資源數組和循環變量i,j
 99     string px;//資源名變量
100     py=new int[col];//分配數組大小
101     cout<<"(請輸入請求資源的進程名)"<<endl;
102     cin>>px;//輸入t0后進程請求資源的進程名
103 
104     //尋找請求資源的進程名在進程名數組中的位置
105     for(i=0;i<row;i++){
106         if(px==pN[i]){
107             cout<<"(請依次輸入對各類資源的請求數目,數之間以空格隔開)"<<endl;
108             for(j=0;j<col;j++){
109                 cin>>py[j];
110             }
111          break;
112         }
113     }
114 
115     int re1=0,re2=0;//定義標志位,分別用以判斷某進程請求的各類資源是否全部小於等於最大需求和可分配資源
116     for(j=0;j<col;j++){
117         if(py[j]<=need[i][j])
118             {re1++;}
119         if(py[j]<=a[j])
120             {re2++;}
121     }
122 
123 
124     //若符合標志位判斷標准,對資源進行修改
125     if(re1==col&&re2==col){
126         for(j=0;j<col;j++){
127             need[i][j]=need[i][j]-py[j];
128             a[j]=a[j]-py[j];
129             all[i][j]=all[i][j]+py[j];
130         }
131         isSafe(row,col);//對資源重新分配后 調用安全性算法
132     }else{cout<<"(不安全!!!)"<<endl;}
133 
134     delete[] py;//釋放
135 }
136 int main()
137 {
138     int i,j,ch;//定義循環變量i,j和判斷變量ch(判斷是否有進程請求資源)
139     cout<<"(請依次輸入進程個數和資源個數(以空格隔開))"<<endl;
140     cin>>row>>col;
141 
142     //以下是對數組或者矩陣分配大小
143     pN=new string[row];//進程名數組
144     rN=new string[col];//資源名數組
145     a=new int[col];//可利用資源數組
146     sum=new int[col];//各類資源總個數
147     finish=new int[row];//各類資源總個數
148     work=new int[col];
149 
150     //動態分配 分配矩陣
151     all=new int*[row];
152     for(i=0;i<row;i++){
153         all[i]=new int[col];
154     }
155 
156     //動態分配最大需求矩陣
157     ma=new int*[row];
158     for(i=0;i<row;i++){
159         ma[i]=new int[col];
160     }
161 
162     //動態分配需求矩陣
163     need=new int*[row];
164     for(i=0;i<row;i++){
165         need[i]=new int[col];
166     }
167 
168 
169     cout<<"(請輸入進程名(以空格隔開,按回車鍵結束))"<<endl;
170     for(i=0;i<row;i++){
171         cin>>pN[i];
172     }
173 
174     cout<<"(請輸入資源名(以空格隔開,按回車鍵結束))"<<endl;
175     for(i=0;i<col;i++){
176         cin>>rN[i];
177     }
178 
179     cout<<"(請輸入各類資源的總數量)"<<endl;
180     for(i=0;i<col;i++){
181         cin>>sum[i];
182     }
183 
184 
185     //最大矩陣
186     for(i=0;i<row;i++){
187         cout<<"(請輸入進程 "+pN[i]+" 對各類資源的最大需求(以空格隔開,按回車鍵結束))"<<endl;
188         for(j=0;j<col;j++){
189             cin>>ma[i][j];
190         }
191     }
192 
193 
194     //分配矩陣
195     for(i=0;i<row;i++){
196         cout<<"(請依次輸入進程 "+pN[i]+" 已分配資源情況(以空格隔開,按回車鍵結束))"<<endl;
197         for(j=0;j<col;j++){
198             cin>>all[i][j];
199         }
200     }
201 
202 
203     //需求矩陣
204     for(i=0;i<row;i++){
205         for(j=0;j<col;j++){
206             need[i][j]=ma[i][j]-all[i][j];
207         }
208     }
209 
210 
211     //可利用資源
212     for(j=0;j<row;j++){
213             a[j]=sum[j]-all[0][j];
214         }
215     for(j=0;j<col;j++){
216         for(i=1;i<row;i++){
217             a[j]=a[j]-all[i][j];
218         }
219     }
220 
221     isSafe(row,col);//調用安全性算法
222     cout<<"(是否有進程對資源發出請求,是---1,否---0)"<<endl;
223     cin>>ch;
224     if(ch==1){
225         request(col);//調用銀行家算法
226     }
227 
228 
229 
230     //釋放
231     for(i=0;i<row;i++){
232         delete[] all[i];
233         delete[] ma[i];
234         delete[] need[i];
235     }
236     delete[] all,ma,need,pN,rN,a,sum,work,finish;
237     return 0;
238 }

 

 

 2019-04-20-20:39:05

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM