银行家算法(C代码)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.hintMaxNeed[5][3];intAllocation[5][3];intNeed[5][3];intAll[1][3];intAvailable[1][3];voidHead()//开始{inti=0;intj=0;for(i=0;i15;i++){printf(\020);}printf(\n);printf(欢迎使用银行家算法!\n);for(j=0;j15;j++){printf(\020);}printf(\n);}voidInitArray()//各个数组初始化{inti;intj;for(i=0;i5;i++){intl=i+1;printf(请输入P%d进程最大需求的信息:\n,l);printf(A资源,B资源,C资源:);scanf(%d%d%d,&MaxNeed[i][0],&MaxNeed[i][1],&MaxNeed[i][2]);}for(j=0;j5;j++){intl=j+1;printf(请输入P%d进程已获得资源的信息:\n,l);printf(A资源,B资源,C资源:);scanf(%d%d%d,&Allocation[j][0],&Allocation[j][1],&Allocation[j][2]);}printf(请输入总资源的信息:\n);printf(A资源,B资源,C资源:);scanf(%d%d%d,&All[0][0],&All[0][1],&All[0][2]);}voidCalculateAvailable()//计算可利用的资源{inti=0;inta=0;intb=0;intc=0;for(i=0;i5;i++){a=a+Allocation[i][0];b=b+Allocation[i][1];c=c+Allocation[i][2];}Available[0][0]=All[0][0]-a;Available[0][1]=All[0][1]-b;Available[0][2]=All[0][2]-c;}voidCalculateNeed()//计算每个进程还需要的资源{inti=0;intj=0;for(i=0;i5;i++){for(j=0;j3;j++){Need[i][j]=MaxNeed[i][j]-Allocation[i][j];}}}voidPrintMaxNeed()//打印每个进程的最大需求量{inti=0;intj=0;for(i=0;i5;i++){for(j=0;j3;j++){printf(%5d,MaxNeed[i][j]);}printf(\n);}printf(\n);}voidPrintAllocation()//打印每个进程已分配的资源{inti=0;intj=0;for(i=0;i5;i++){for(j=0;j3;j++){printf(%5d,Allocation[i][j]);}printf(\n);}printf(\n);}voidPrintNeed()//打印每个进程还需要的资源数量{inti=0;intj=0;for(i=0;i5;i++){for(j=0;j3;j++){printf(%5d,Need[i][j]);}printf(\n);}printf(\n);}voidPrintAll()//打印总共资源数量{inti=0;for(i=0;i3;i++){printf(%5d,All[0][i]);}printf(\n);}voidPrintAvailable()//打印还可利用的资源数量{inti=0;for(i=0;i3;i++){printf(%5d,Available[0][i]);}printf(\n);}voidJudgeSafeCondition()//判断安全状态{inti=0;intj=0;intk=0;intcount=0;intSafeSequence[5]={7,7,7,7,7};for(i=0;i5;i++){inta;for(j=0;j5;j++){a=((Available[0][0]=Need[j][0])&&(Available[0][1]=Need[j][1])&&(Available[0][2]=Need[j][2]));if(a==1&&j!=SafeSequence[0]&&j!=SafeSequence[1]&&j!=SafeSequence[2]&&j!=SafeSequence[3]&&j!=SafeSequence[4]){count=count+1;SafeSequence[i]=j;Available[0][0]=Allocation[j][0]+Available[0][0];Available[0][1]=Allocation[j][1]+Available[0][1];Available[0][2]=Allocation[j][2]+Available[0][2];printf(调用进程P%d后的Available资源:,j+1);PrintAvailable();printf(\n);break;}}}if(count==5){printf(存在安全序列:\n);for(k=0;k5;k++){printf(P%d,SafeSequence[k]+1);}printf(\n);}else{printf(查找安全序列失败!\n);}}voidApplySource()//T0时刻申请资源{intpro;//请求资源进程数ints_a,s_b,s_c;//请求资源的数量printf(请选择要请求资源的进程数[1,2,3,4,5]:);scanf(%d,&pro);printf(请输入还需要的A,B,C的资源数:);scanf(%d%d%d,&s_a,&s_b,&s_c);Available[0][0]=Available[0][0]-s_a;Available[0][1]=Available[0][1]-s_b;Available[0][2]=Available[0][2]-s_c;Allocation[pro-1][0]=Allocation[pro-1][0]+s_a;Allocation[pro-1][1]=Allocation[pro-1][1]+s_b;Allocation[pro-1][2]=Allocation[pro-1][2]+s_c;CalculateNeed();}voidmain(){charapp_source;Head();InitArray();CalculateAvailable();CalculateNeed();printf(MaxNeed:\n);PrintMaxNeed();printf(Allocation:\n);PrintAllocation();printf(Need:\n);PrintNeed();printf(All:\n);PrintAll();printf(Available:\n);PrintAvailable();printf(是否有进程要申请资源[y/n]:);fflush(stdin);scanf(%c,&app_source);switch(app_source){case'y':{ApplySource();printf(Allocation:\n);PrintAllocation();printf(Need:\n);PrintNeed();printf(All:\n);PrintAll();printf(Available:\n);PrintAvailable();JudgeSafeCondition();break;}case'n':JudgeSafeCondition();break;}}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功