操作系统银行家算法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 */

 

转载于:https://www.cnblogs.com/gideonzsd/p/7230417.html

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
操作系统课的实验(银行家算法)#include "malloc.h"   #include "stdio.h"   #include "stdlib.h"   #define alloclen sizeof(struct allocation)   #define maxlen sizeof(struct max)   #define avalen sizeof(struct available)   #define needlen sizeof(struct need)   #define finilen sizeof(struct finish)   #define pathlen sizeof(struct path)   struct allocation   {   int value;   struct allocation *next;   };   struct max   {   int value;   struct max *next;   };   struct available /*可用资源数*/   {   int value;   struct available *next;   };   struct need /*需求资源数*/   {   int value;   struct need *next;   };   struct path   {   int value;   struct path *next;   };   struct finish   {   int stat;   struct finish *next;   };   int main()   {   int row,colum,status=0,i,j,t,temp,processtest;   struct allocation *allochead,*alloc1,*alloc2,*alloctemp;   struct max *maxhead,*maxium1,*maxium2,*maxtemp;   struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;   struct need *needhead,*need1,*need2,*needtemp;   struct finish *finihead,*finish1,*finish2,*finishtemp;   struct path *pathhead,*path1,*path2;   printf("\n请输入系统资源的种类数:");   scanf("%d",&colum);   printf("请输入现时内存中的进程数:");   scanf("%d",&row);   printf("请输入已分配资源矩阵:\n");   for(i=0;i<row;i++)   {   for (j=0;jnext=alloc2->next=NULL;   scanf("%d",&allochead->value);   status++;   }   else   {   alloc2=(struct allocation *)malloc(alloclen);   scanf("%d,%d",&alloc2->value);   if(status==1)   {   allochead->next=alloc2;   status++;   }   alloc1->next=alloc2;   alloc1=alloc2;   }   }   }   alloc2->next=NULL;   status=0;   printf("请输入最大需求矩阵:\n");   for(i=0;i<row;i++)   {   for (j=0;jnext=maxium2->next=NULL;   scanf("%d",&maxium1->value);   status++;   }   else   {   maxium2=(struct max *)malloc(maxlen);   scanf("%d,%d",&maxium2->value);   if(status==1)   {   maxhead->next=maxium2;   status++;   }   maxium1->next=maxium2;   maxium1=maxium2;   }   }   }   maxium2->next=NULL;   status=0;   printf("请输入现时系统剩余的资源矩阵:\n");   for (j=0;jnext=available2->next=NULL;   work1->next=work2->next=NULL;   scanf("%d",&available1->value);   work1->value=available1->value;   status++;   }   else   {   available2=(struct available*)malloc(avalen);   work2=(struct available*)malloc(avalen);   scanf("%d,%d",&available2->value);   work2->value=available2->value;   if(status==1)   {   avahead->next=available2;   workhead->next=work2;   status++;   }   available1->next=available2;   available1=available2;   work1->next=work2;   work1=work2;   }   }   available2->next=NULL;   work2->next=NULL;   status=0;   alloctemp=allochead;   maxtemp=maxhead;   for(i=0;i<row;i++)   for (j=0;jnext=need2->next=NULL;   need1->value=maxtemp->value-alloctemp->value;   status++;   }   else   {   need2=(struct need *)malloc(needlen);   need2->value=(maxtemp->value)-(alloctemp->value);   if(status==1)   {   needhead->next=need2;   status++;   }   need1->next=need2;   need1=need2;   }   maxtemp=maxtemp->next;   alloctemp=alloctemp->next;   }   need2->next=NULL;   status=0;   for(i=0;inext=finish2->next=NULL;   finish1->stat=0;   status++;   }   else   {   finish2=(struct finish*)malloc(finilen);   finish2->stat=0;   if(status==1)   {   finihead->next=finish2;   status++;   }   finish1->next=finish2;   finish1=finish2;   }   }   finish2->next=NULL; /*Initialization compleated*/   status=0;   processtest=0;   for(temp=0;temp<row;temp++)   {   alloctemp=allochead;   needtemp=needhead;   finishtemp=finihead;   worktemp=workhead;   for(i=0;istat==0)   {   for(j=0;jnext,worktemp=worktemp->next)   if(needtemp->valuevalue)   processtest++;   if(processtest==colum)   {   for(j=0;jvalue+=alloctemp->value;   worktemp1=worktemp1->next;   alloctemp=alloctemp->next;   }   if(status==0)   {   pathhead=path1=path2=(struct path*)malloc(pathlen);   path1->next=path2->next=NULL;   path1->value=i;   status++;   }   else   {   path2=(struct path*)malloc(pathlen);   path2->value=i;   if(status==1)   {   pathhead->next=path2;   status++;   }   path1->next=path2;   path1=path2;   }   finishtemp->stat=1;   }   else   {   for(t=0;tnext;   finishtemp->stat=0;   }   }   else   for(t=0;tnext;   alloctemp=alloctemp->next;   }   processtest=0;   worktemp=workhead;   finishtemp=finishtemp->next;   }   }   path2->next=NULL;   finishtemp=finihead;   for(temp=0;tempstat==0)   {   printf("\n系统处于非安全状态!\n");   exit(0);   }   finishtemp=finishtemp->next;   }   printf("\n系统处于安全状态.\n");   printf("\n安全序列为: \n");   do   {   printf("p%d ",pathhead->value);   }   while(pathhead=pathhead->next);   printf("\n");   return 0;   } #include "string.h" #include #include #define M 5 #define N 3 #define FALSE 0 #define TRUE 1 /*M个进程对N类资源最大资源需求量*/ int MAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; /*系统可用资源数*/ int AVAILABLE[N]={10,5,7}; /*M个进程对N类资源最大资源需求量*/ int ALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}}; /*M个进程已经得到N类资源的资源量 */ int NEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}}; /*M个进程还需要N类资源的资源量*/ int Request[N]={0,0,0}; void main() { int i=0,j=0; char flag='Y'; void showdata(); void changdata(int); void rstordata(int); int chkerr(int); showdata(); while(flag=='Y'||flag=='y') { i=-1; while(i=M) { printf("请输入需申请资源的进程号(从0到"); printf("%d",M-1); printf(",否则重输入!):"); scanf("%d",&i); if(i=M)printf("输入的进程号不存在,重新输入!\n"); } printf("请输入进程"); printf("%d",i); printf("申请的资源数\n"); for (j=0;jNEED[i][j]) { printf("进程"); printf("%d",i); printf("申请的资源数大于进程"); printf("%d",i); printf("还需要"); printf("%d",j); printf("类资源的资源量!申请不合理,出错!请重新选择!\n"); /*printf("申请不合理,出错!请重新选择!\n");*/ flag='N'; break; } else { if(Request[j]>AVAILABLE[j]) { printf("进程"); printf("%d",i); printf("申请的资源数大于系统可用"); printf("%d",j); printf("类资源的资源量!申请不合理,出错!请重新选择!\n"); /*printf("申请不合理,出错!请重新选择!\n");*/ flag='N'; break; } } } if(flag=='Y'||flag=='y') { changdata(i); if(chkerr(i)) { rstordata(i); showdata(); } else showdata(); } else showdata(); printf("\n"); printf("是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: "); scanf("%c",&flag); } } void showdata() { int i,j; printf("系统可用的资源数为:\n"); printf(" "); for (j=0;j<N;j++){ printf(" 资源"); printf("%d",j); printf(":"); printf("%d",AVAILABLE[j]); /*printf("\n");*/ /* cout<<endl; // cout<<"各进程资源的最大需求量:"<<endl<<endl; // for (i=0;i<M;i++) // { // cout<<"进程"<<i<<":"; // for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<MAX[i][j]; // cout<<endl; */ } printf("\n"); printf("各进程还需要的资源量:\n"); for (i=0;i<M;i++) { printf(" 进程"); printf("%d",i); printf(":"); for (j=0;j<N;j++){ printf("资源"); printf("%d",j); printf(":"); printf("%d",NEED[i][j]); /*printf("\n");*/ } printf("\n"); } printf("各进程已经得到的资源量: \n"); for (i=0;i<M;i++) { printf(" 进程"); printf("%d",i); /*printf(":\n");*/ for (j=0;j<N;j++){ printf("资源"); printf("%d",j); printf(":"); printf("%d",ALLOCATION[i][j]); /*printf("\n");*/ } printf("\n"); } } 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 rstordata(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 chkerr(int s) { int WORK,FINISH[M],temp[M]; 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; while(i<M) { if (FINISH[i]==FALSE&&NEED[i][j]<=WORK) { WORK=WORK+ALLOCATION[i][j]; FINISH[i]=TRUE; temp[k]=i; k++; i=0; } else { i++; } } for(i=0;i<M;i++) if(FINISH[i]==FALSE) { printf("\n"); printf("系统不安全!!! 本次资源申请不成功!!!\n"); printf("\n"); return 1; } } printf("\n"); printf("经安全性检查,系统安全,本次分配成功。\n"); printf("\n"); printf(" 本次安全序列:"); for(i=0;i"); } printf("\n"); return 0; }
下面是一个简单的银行家算法C语言代码实现: ```c #include <stdio.h> #include <stdbool.h> #define MAX_PROCESS_NUM 10 #define MAX_RESOURCE_NUM 10 int available[MAX_RESOURCE_NUM] = {0}; int max[MAX_PROCESS_NUM][MAX_RESOURCE_NUM] = {0}; int allocation[MAX_PROCESS_NUM][MAX_RESOURCE_NUM] = {0}; int need[MAX_PROCESS_NUM][MAX_RESOURCE_NUM] = {0}; bool finish[MAX_PROCESS_NUM] = {false}; int safe_sequence[MAX_PROCESS_NUM] = {0}; int process_num, resource_num; bool safety_check() { int work[MAX_RESOURCE_NUM]; bool finish_copy[MAX_PROCESS_NUM]; for (int i = 0; i < resource_num; i++) { work[i] = available[i]; } for (int i = 0; i < process_num; i++) { finish_copy[i] = finish[i]; } int count = 0; while (count < process_num) { bool found = false; for (int i = 0; i < process_num; i++) { if (!finish_copy[i]) { bool enough = true; for (int j = 0; j < resource_num; j++) { if (need[i][j] > work[j]) { enough = false; break; } } if (enough) { found = true; for (int j = 0; j < resource_num; j++) { work[j] += allocation[i][j]; } finish_copy[i] = true; safe_sequence[count] = i; count++; } } } if (!found) { return false; } } return true; } int main() { printf("Enter the number of processes: "); scanf("%d", &process_num); printf("Enter the number of resources: "); scanf("%d", &resource_num); printf("Enter the available amount of each resource:\n"); for (int i = 0; i < resource_num; i++) { scanf("%d", &available[i]); } printf("Enter the maximum demand of each process:\n"); for (int i = 0; i < process_num; i++) { for (int j = 0; j < resource_num; j++) { scanf("%d", &max[i][j]); } } printf("Enter the amount of each resource currently allocated to each process:\n"); for (int i = 0; i < process_num; i++) { for (int j = 0; j < resource_num; j++) { scanf("%d", &allocation[i][j]); need[i][j] = max[i][j] - allocation[i][j]; } } if (safety_check()) { printf("Safe sequence:"); for (int i = 0; i < process_num; i++) { printf(" P%d", safe_sequence[i]); } printf("\n"); } else { printf("Unsafe state detected.\n"); } return 0; } ``` 注意:本代码实现仅供参考,实际应用中需要根据具体情况进行修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值