#includestdio.h#includeiostreamusingnamespacestd;externunsignedintcount=0;typedefstruct{unsignedintA;unsignedintB;unsignedintC;}resource;typedefstruct{resourcemax;resourceallacation;resourceneed;resourcework;boolfinish;}process;resourcerequest(process&p){unsignedintx1,x2,x3;resourcere;printf(pleaseinputthenumberoftherequestingresourceoftheprocess:\n);printf(resourceA:);cinx1;printf(resourceB:);cinx2;printf(resourceC:);cinx3;re.A=x1;re.B=x2;re.C=x3;returnre;}voidneedResource(process&p){p.need.A=p.max.A-p.allacation.A;p.need.B=p.max.B-p.allacation.B;p.need.C=p.max.C-p.allacation.C;}resourceavailableresource(process*p){resourcea;a.A=10-p[0].allacation.A-p[1].allacation.A-p[2].allacation.A-p[3].allacation.A-p[4].allacation.A;a.B=5-p[0].allacation.B-p[1].allacation.B-p[2].allacation.B-p[3].allacation.B-p[4].allacation.B;a.C=7-p[0].allacation.C-p[1].allacation.C-p[2].allacation.C-p[3].allacation.C-p[4].allacation.C;returna;}voidprinttable(process*p,resourceavailable){printf(max);printf(allacation);printf(need);printf(available\n);for(unsignedintxx=0;xx5;xx++){printf(p%d%d%d%d,xx,p[xx].max.A,p[xx].max.B,p[xx].max.C);printf(%d%d%d,p[xx].allacation.A,p[xx].allacation.B,p[xx].allacation.C);printf(%d%d%d,p[xx].need.A,p[xx].need.B,p[xx].need.C);if(xx==0)printf(%d%d%d\n,available.A,available.B,available.C);elseprintf(\n);}}voidprintTableByLine(process&p,intxx,resource&available){count++;if(count==1){printf(work);printf(allacation);printf(need);printf(work+allacation);printf(finish\n);}printf(p%d%d%d%d,xx,p.work.A,p.work.B,p.work.C);printf(%d%d%d,p.allacation.A,p.allacation.B,p.allacation.C);printf(%d%d%d,p.need.A,p.need.B,p.need.C);printf(%d%d%d,available.A,available.B,available.C);printf(%d\n,p.finish,p.finish,p.finish);}voidmain(){processp[5];unsignedintbuffer[5];unsignedintb=0,j=0,temp=0;resourceavailable;p[0].max.A=7;p[0].max.B=5;p[0].max.C=3;p[1].max.A=3;p[1].max.B=2;p[1].max.C=2;p[2].max.A=9;p[2].max.B=0;p[2].max.C=2;p[3].max.A=2;p[3].max.B=2;p[3].max.C=2;p[4].max.A=4;p[4].max.B=3;p[4].max.C=3;p[0].allacation.A=0;p[0].allacation.B=1;p[0].allacation.C=0;p[1].allacation.A=2;p[1].allacation.B=0;p[1].allacation.C=0;p[2].allacation.A=3;p[2].allacation.B=0;p[2].allacation.C=2;p[3].allacation.A=2;p[3].allacation.B=1;p[3].allacation.C=1;p[4].allacation.A=0;p[4].allacation.B=0;p[4].allacation.C=2;printf(制作者:太原科技大学——计算机102002班——JMJ——!!\n);for(unsignedinth=0;h5;h++){needResource(p[h]);p[h].finish=false;}available=availableresource(p);loop:while(true){unsignedintnprocess;resourcereq;printtable(p,available);printf(pleaseinputthenumberoftheprocessrequestingtheresources:);cinnprocess;if(nprocess4){printf(the%dthprocessdoesn'texist!\n,nprocess);gotoloop;}printf(thisisthe%dthprocess'srequest.\n,nprocess);req=request(p[nprocess]);//available=availableresource(p);couthelloendl;if((req.Ap[nprocess].need.A)||(req.Bp[nprocess].need.B)||(req.Cp[nprocess].need.C)){printf(illegalrequest!pleaseinputagain!\n);gotoloop;}available=availableresource(p);if((req.Aavailable.A)||(req.Bavailable.B)||(req.Cavailable.C)){printf(thereisn'tenoughresource!\n);coutwaitplease.........endl;gotoloop;}p[nprocess].allacation.A=p[nprocess].allacation.A+req.A;p[nprocess].allacation.B=p[nprocess].allacation.B+req.B;p[nprocess].allacation.C=p[nprocess].allacation.C+req.C;printf(thisistheresourcetablethathasbeenmodified\n);needResource(p[nprocess]);available=availableresource(p);printtable(p,available);for(unsignedintt=0;t5;t++){p[t].work.A=available.A;p[t].work.B=available.B;p[t].work.C=available.C;}for(unsignedintk=0;k15;k++){temp=(k%5);if(p[temp].finish==false){if((p[temp].work.A=p[temp].need.A)&&(p[temp].work.B=p[temp].need.B)&&(p[temp].work.C=p[temp].need.C)){resourcetemp0;available.A=p[temp].work.A+p[temp].allacation.A;available.B=p[temp].work.B+p[temp].allacation.B;available.C=p[temp].work.C+p[temp].allacation.C;for(unsignedintt=0;t5;t++){if(t!=temp){p[t].work.A=available.A;p[t].work.B=available.B;p[t].work.C=available.C;}}p[temp].finish=true;printTableByLine(p[temp],temp,available);buffer[j]=temp;j++;}}}if(p[0].finish&&p[1].finish&&p[2].finish&&p[3].finish&&p[4].finish){coutthesafesequencehasbeenfound,asfollows:endl;for(unsignedintj=0;j5;j++){printf(p[%d],buffer[j]);}printf(\n);}else{couttheschemeisnotsafe!thesystemwillisrestoringtheresource........endl;p[nprocess].allacation.A-=req.A;p[nprocess].allacation.B-=req.B;p[nprocess].allacation.C-=req.C;p[nprocess].need.A+=req.A;p[nprocess].need.B+=req.B;p[nprocess].need.C+=req.C;}}}