操作系統銀行家算法C語言代碼實現


    計算機操作系統課設需要,寫了兩個下午的銀行家算法(陷在bug里出不來耽誤了很多時間),參考計算機操作系統(湯子瀛)

    實現過程中不涉及難度較大的算法,僅根據銀行家算法的思想和步驟進行實現。以下為詳細步驟:  

  •  定義:

              max1[ ][ ] : 最大需求矩陣,max1[i][j]為第i條進程的第j項資源的最大需求數目;

      allocation[ ][ ] : 分配矩陣,allocation[i][j]為第i條進程已分得的第j項資源的數目;

      need[ ][ ] : 需求矩陣,need[i][j]為第i條進程尚需要的第j項資源的數目;

      available[ ] : 可利用資源量,available[i]為系統中第i項資源的可分配數目; 

      request[ ][ ] : 請求矩陣,request[i][j]表示第i條進程對第j項資源的請求數目;//可以改成一維數組

    int safe (int n,int m,int work) : n條進程,m項進程,返回值為1時當前狀態安全,否則不安全;

  •  程序流程:   
  1. 鍵盤輸入max1矩陣,allocation矩陣,available數組,計算出need矩陣。
  2. 判斷當前時刻系統的狀態是否安全。true 轉向3,false轉向7 
  3. 判斷當前時刻request<=need。true 轉向4,false 轉向7 
  4. 判斷當前時刻request<=available。true 轉向5,false 轉向7
  5. 進行安全性算法檢測。true 轉向6,false 轉向7
  6. 系統分配資源並繼續等待指令。
  7. 系統不予分配資源並輸出原因。
  •  安全性算法  :  每次從第一個進程開始檢測,如遇到所有的m項資源都可以滿足時,work+=allocation,否則轉入下一個進程的檢測。兩種情況跳出第20行的循環。
  1.  所有finish均為1,i無法置為-1 ,i==N時跳出循環
  2.  存在為0的finish,但直至i==N時,仍未有新的work<need出現(從最近的一次i==-1算起),i==N時跳出循環

  第50行進行檢測區分上述兩種情況,如安全返回1,否則返回0;

 

以下為完整的代碼實現:(另附測試數據)

  1 #include<bits/stdc++.h>
  2 int max1[1000][1000]= {0};
  3 int allocation[1000][1000]= {0};
  4 int need[1000][1000]= {0};
  5 int finish[1000]= {0};
  6 int available[1000]= {0};
  7 int request[1000][1000]= {0};
  8 int waitq[1000]= {0};
  9 int waitnum=0;
 10 int safeq[1000]= {0};
 11 int safe (int N , int M ,int work[])
 12 {
 13     int s=0;
 14     memset(finish,0,1000*sizeof(int));
 15     for(int i=0; i<M; i++)
 16     {
 17         work[i]=available[i];
 18     }
 19     int flag=1;
 20     for(int i=0; i<N; i++)
 21     {
 22         flag=1;
 23         if(!finish[i])
 24         {
 25             for(int j=0; j<M; j++)
 26             {
 27                 if(need[i][j]>work[j])
 28                 {
 29                     flag=0;
 30                     break;
 31                 }
 32             }
 33             if(flag)
 34             {
 35                 for(int j=0; j<M; j++)
 36                 {
 37                     work[j]+=allocation[i][j];
 38                     printf(" %d          ",work[j]);
 39                 }
 40                 for(int j=0; j<3; j++)
 41                     printf("%d     ",available[j]);
 42                 printf("program %d\n",i);
 43                 safeq[s++]=i;
 44                 finish[i]=1;
 45                 i=-1;
 46             }
 47         }
 48     }
 49     int te=1;
 50     for(int i=0; i<5; i++)
 51         if(!finish[i])
 52             te=0;
 53     return te;
 54 }
 55 void print(int pn,int yn)
 56 {
 57     printf("current status\n");
 58     char a='A';
 59     int i2=0;
 60     for(i2=0; i2<4; i2++)
 61     {
 62         switch(i2)
 63         {
 64         case 0:
 65             printf("Max:");
 66             for(int i=0; i<yn-1; i++)
 67                 printf("     ");
 68             printf(" ");
 69             break;
 70         case 1:
 71             printf("Allocation:");
 72             for(int i=0; i<yn-3; i++)
 73                 printf("     ");
 74             printf("    ");
 75             break;
 76         case 2:
 77             printf("Need:");
 78             for(int i=0; i<yn-1; i++)
 79                 printf("     ");
 80             break;
 81         case 3:
 82             printf("Available:");
 83             for(int i=0; i<yn-2; i++)
 84                 printf("   ");
 85             printf(" ");
 86             printf("\n");
 87             break;
 88         }
 89     }
 90     for(i2=0; i2<4; i2++)
 91     {
 92         switch(i2)
 93         {
 94         case 0:
 95             for(int j=0; j<yn; j++)
 96                 printf("%c    ",a+j);
 97             break;
 98         case 1:
 99             for(int j=0; j<yn; j++)
100                 printf("%c    ",a+j);
101             break;
102         case 2:
103             for(int j=0; j<yn; j++)
104                 printf("%c    ",a+j);
105             break;
106         case 3:
107             for(int j=0; j<yn; j++)
108                 printf("%c    ",a+j);
109             break;
110 
111         }
112     }
113     printf("\n");
114     for(int i=0; i<pn; i++)
115     {
116         for(int j=0; j<yn; j++)
117         {
118             printf("%d    ",max1[i][j]);
119         }
120         for(int j=0; j<yn; j++)
121         {
122             printf("%d    ",allocation[i][j]);
123         }
124         for(int j=0; j<yn; j++)
125         {
126             printf("%d    ",need[i][j]);
127         }
128         if(i==0)
129             for(int j=0; j<yn; j++)
130                 printf("%d    ",available[j]);
131         printf("\n");
132     }
133 }
134 int main()
135 {
136     int work[1000]= {0};
137     int pn,yn;
138     printf("Please input the number of the program\n");
139     scanf("%d",&pn);
140     printf("Please input the number of the element\n");
141     scanf("%d",&yn);
142     printf("Please input Max and Allocation of the program \n");
143     for(int i=0; i<pn; i++)
144     {
145         for(int j=0; j<yn; j++)
146         {
147             scanf("%d",&max1[i][j]);
148         }
149         for(int j=0; j<yn; j++)
150         {
151             scanf("%d",&allocation[i][j]);
152         }
153         for(int j=0; j<yn; j++)
154         {
155             need[i][j]=max1[i][j]-allocation[i][j];
156         }
157     }
158     printf("Please input the Available \n");
159     for(int i=0; i<yn; i++)
160     {
161         scanf("%d",&available[i]);
162         work[i]=available[i];
163     }
164 
165     if(safe(pn,yn,work))
166     {
167         printf("it is safe now \n");
168         for(int i=0; i<pn; i++)
169             printf("%d  ",safeq[i]);
170         printf("\n");
171         printf("is the one of the safe sequence \n");
172     }
173     else
174         printf("it is not safe now\n");
175 
176 
177     if(safe(pn,yn,work))
178     {
179         while(1)
180         {
181             int num;
182             int ex;
183             int judge=1;
184             printf("if you want to exit , please input 0 else input 1 \n");
185             scanf("%d",&ex);
186             if(!ex)
187                 break;
188             printf("Please input the number of the request program \n");
189             scanf("%d",&num);
190             printf("Please input the Request \n");
191             for(int i=0; i<yn; i++)
192             {
193                 scanf("%d",&request[num][i]);
194                 if(request[num][i]>need[num][i])
195                 {
196                     judge=0;
197                     printf("error!\n");
198                     break;
199                 }
200             }
201             if(judge)
202             {
203                 int wait=0;
204                 for(int i=0; i<yn; i++)
205                 {
206                     if(request[num][i]>available[i])
207                     {
208                         wait=1;
209                         printf("wait because request>available!\n");
210                         break;
211                     }
212                 }
213                 if(!wait)
214                 {
215 
216                     for(int j1=0; j1<yn; j1++)
217                     {
218                         available[j1]-=request[num][j1];
219                         allocation[num][j1]+=request[num][j1];
220                         need[num][j1]-=request[num][j1];
221                     }
222                     if(safe(pn,yn,work))
223                     {
224                         printf("it is safe now \n");
225                         for(int i=0; i<pn; i++)
226                             printf("%d  ",safeq[i]);
227                         printf("\n");
228                         printf("is the one of the safe sequence \n");
229                         printf("complete !!!!!!!\n");
230                     }
231                     else
232                     {
233                         for(int j1=0; j1<yn; j1++)
234                         {
235                             available[j1]+=request[num][j1];
236                             allocation[num][j1]-=request[num][j1];
237                             need[num][j1]+=request[num][j1];
238                         }
239                         printf("wait because it is not safe \n");
240                     }
241                 }
242 
243             }
244         }
245     }
246     print(pn,yn);
247 }
248 
249 /*
250 5
251 3
252 7 5 3 0 1 0
253 3 2 2 2 0 0
254 9 0 2 3 0 2
255 2 2 2 2 1 1
256 4 3 3 0 0 2
257 3 3 2
258 1
259 1
260 1 0 2
261 1
262 4
263 3 3 0
264 1
265 0
266 0 2 0
267 0
268 
269 
270 */

 


免責聲明!

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



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