您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 5进程3资源的银行家算法(Linux)
#includestdio.hintRescore[3]={17,5,20};intAvailable[3];intMax[5][3];intAllocated[5][3];intNeed[5][3];intFinish[5];intError=1;intp=0;inteme[120][5];voidswit(int*a,int*b){intm;m=*a;*a=*b;*b=m;}voidttt(inttable[],intk,intm){inti,tmp=0;if(km){for(i=0;i=m;i++){eme[p][i]=table[i];}p++;}else{for(i=k;i=m;i++){swit(&table[k],&table[i]);ttt(table,k+1,m);swit(&table[k],&table[i]);}}}voidinitRescore(intRescore[3]){intj;printf(pleaseinputRescorenum\n);for(j=0;j3;j++){printf(Rescore%d=,j+1);scanf(%d,&Rescore[j]);}}voidinitMax(intMax[5][3]){inti,j;printf(PleaseinputMax.\n);for(i=0;i5;i++){printf(pleaseinputthemaxofprocess%d\n,i+1);for(j=0;j3;j++){scanf(%d,&Max[i][j]);}}}voidprint(intarry[5][3]){intm,n;printf(\tA\tB\tC\n);for(m=0;m5;m++){printf(p%d\t,m+1);for(n=0;n3;n++){printf(%d\t,arry[m][n]);}printf(\n);}}voidinitAllocated(intAllocated[5][3]){inti,j;printf(Pleaseinputallocated.\n);for(i=0;i5;i++){printf(pleaseinputtheallocatedofprocess%d\n,i+1);for(j=0;j3;j++){scanf(%d,&Allocated[i][j]);if(Max[i][j]Allocated[i][j]){printf(toobig\n);Error=0;return;}}}}voidneed(intarry[5][3]){inti,j;for(i=0;i5;i++){for(j=0;j3;j++){Need[i][j]=Max[i][j]-Allocated[i][j];}}}voidavailable(intRescore[3],intAvailable[3]){inti,j;intsum=0;for(j=0;j3;j++){for(i=0;i5;i++){sum=Allocated[i][j]+sum;}Available[j]=Rescore[j]-sum;if(Available[j]0){Available[j]=0;}sum=0;}}voidReAllocated(inti,intarry[3]){intj=0;i=i-1;for(;j3;j++){Allocated[i][j]=Allocated[i][j]+arry[j];}}voidpanbie(intAvailable[3],intAllocated[5][3],intNeed[5][3]){inti,j,k=0,m=0,h,p,q,s,r,x,c=0,t=0;inttmp2[3];for(s=0;s3;s++){tmp2[s]=Available[s];}for(h=0;h120;h++){for(i=0;i5;i++){for(j=0;j3;j++){if(Available[j]=Need[eme[h][i]-1][j])k++;}if(k==3){t++;for(j=0;j3;j++){Available[j]=Available[j]+Allocated[eme[h][i]-1][j];}Finish[m]=eme[h][i];m++;}k=0;}m=0;if(t==5){c++;for(x=0;x5;x++){printf(p%d\t,Finish[x]);}printf(\n);}for(r=0;r3;r++){Available[r]=tmp2[r];}t=0;}if(c==0){printf(Notsafe\n);}printf(c=%d\n,c);}intmain(void){ints;intj;intp,q;intm,n;intadd[3];inttable[]={1,2,3,4,5};ttt(table,0,4);initRescore(Rescore);initMax(Max);initAllocated(Allocated);while(Error==0){printf(pleaseinputtherightsize\n);initAllocated(Allocated);Error=1;}if(Error==1){printf(wewillprintftheMax\n);print(Max);printf(wewillprintftheAllocated\n);print(Allocated);}need(Need);printf(wewillprintftheNeed\n);print(Need);available(Rescore,Available);for(j=0;j3;j++){printf(Available[%d]=%d\n,j+1,Available[j]);}panbie(Available,Allocated,Need);printf(DoyouwanttorequestrescoresYes(1)orNo(0).\n);scanf(%d,&s);if(s==1){printf(plaeseinputwhichprocessyouwanttorequest\n);scanf(%d,&p);if(p5&&p=0){printf(wrong,pleaseinputagain);scanf(%d,&p);}elseif(p=5&&p0){printf(pleaseinputtheadd\n);for(q=0;q3;q++){scanf(%d,&add[q]);}ReAllocated(p,add);printf(wewillprintftheAllocated\n);print(Allocated);need(Need);printf(wewillprintftheNeed\n);print(Need);available(Rescore,Available);for(j=0;j3;j++){printf(Available[%d]=%d\n,j+1,Available[j]);}panbie(Available,Allocated,Need);}}else{printf(Thankyou\n);}}
本文标题:5进程3资源的银行家算法(Linux)
链接地址:https://www.777doc.com/doc-6137293 .html