您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > 生产者消费者关系转换程序代码
/***************************************************************//*PROGRAMNAME:PRODUCER_CONSUMER*//*Thisprogramsimulatestwoprocesses,producerwhich*//*continuestoproducemessageandputitintoabuffer*//*[implementedbyPIPE],andconsumerwhichcontinuestoget*//*messagefromthebufferanduseit.*//*Theprogramalsodemonstratesthesynchronismbetween*//*processesandusesofPIPE.*//***************************************************************/#includestdio.h#includestdlib.h#definePIPESIZE8#definePRODUCER0#defineCONSUMER1#defineRUN0/*statuofprocess*/#defineWAIT1/*statuofprocess*/#defineREADY2/*statuofprocess*/#defineNORMAL0#defineSLEEP1#defineAWAKE2#includestdio.hstructpcb{char*name;intstatu;inttime;};/*timesofexecution*/structpipetype{chartype;intwriteptr;intreadptr;structpcb*pointp;/*writewaitpoint*/structpcb*pointc;};/*readwaitpoint*/intpipe[PIPESIZE];structpipetypepipetb;structpcbprocess[2];main(){intoutput,ret,i;charin[2];intrunp(),runc(),prn();pipetb.type='c';pipetb.writeptr=0;pipetb.readptr=0;pipetb.pointp=pipetb.pointc=NULL;process[PRODUCER].name=Producer\0;process[CONSUMER].name=Consumer\0;process[PRODUCER].statu=process[CONSUMER].statu=READY;process[PRODUCER].time=process[CONSUMER].time=0;output=0;printf(Nowstartingtheprogram!\n);printf(Press'p'torunPRODUCER,press'c'torunCONSUMER.\n);printf(Press'e'toexitfromtheprogram.\n);for(i=0;i1000;i++){in[0]='N';while(in[0]=='N'){scanf(%s,in);if(in[0]!='e'&&in[0]!='p'&&in[0]!='c')in[0]='N';}if(in[0]=='p'&&process[PRODUCER].statu==READY){output=(output+1)%100;if((ret=runp(output,process,pipe,&pipetb,PRODUCER))==SLEEP)pipetb.pointp=&process[PRODUCER];if(ret==AWAKE){(pipetb.pointc)-statu=READY;pipetb.pointc=NULL;runc(process,pipe,&pipetb,CONSUMER);}}if(in[0]=='c'&&process[CONSUMER].statu==READY){if((ret=runc(process,pipe,&pipetb,CONSUMER))==SLEEP)pipetb.pointc=&process[CONSUMER];if(ret==AWAKE){(pipetb.pointp)-statu=READY;pipetb.pointp=NULL;runp(output,process,pipe,&pipetb,PRODUCER);}}if(in[0]=='p'&&process[PRODUCER].statu==WAIT)printf(PRODUCERiswaiting,can'tbescheduled.\n);if(in[0]=='c'&&process[CONSUMER].statu==WAIT)printf(CONSUMERiswaiting,can'tbescheduled.\n);if(in[0]=='e')exit(1);prn(process,pipe,pipetb);in[0]='N';}}runp(out,p,pipe,tb,t)/*runproducer*/intout,pipe[],t;structpcbp[];structpipetype*tb;{p[t].statu=RUN;printf(runPRODUCER.product%d,out);if(tb-writeptr=PIPESIZE){p[t].statu=WAIT;return(SLEEP);}pipe[tb-writeptr]=out;tb-writeptr++;p[t].time++;p[t].statu=READY;if((tb-pointc)!=NULL)return(AWAKE);return(NORMAL);}runc(p,pipe,tb,t)/*runconsumer*/intpipe[],t;structpcbp[];structpipetype*tb;{intc;p[t].statu=RUN;printf(runCONSUMER.);if(tb-readptr=tb-writeptr){p[t].statu=WAIT;return(SLEEP);}c=pipe[tb-readptr];tb-readptr++;printf(use%d,c);if(tb-readptr=tb-writeptr)tb-readptr=tb-writeptr=0;p[t].time++;p[t].statu=READY;if((tb-readptr)==0&&(tb-pointp)!=NULL)return(AWAKE);return(NORMAL);}prn(p,pipe,tb)intpipe[];structpipetypetb;structpcbp[];{inti;printf(\n);for(i=0;iPIPESIZE;i++)printf(------);printf(\n|);for(i=0;iPIPESIZE;i++)if((i=tb.readptr)&&(itb.writeptr))printf(%2d|,pipe[i]);elseprintf(|);printf(\n);for(i=0;iPIPESIZE;i++)printf(------);printf(\nwriteptr=%d,readptr=%d,,tb.writeptr,tb.readptr);if(p[PRODUCER].statu==WAIT)printf(PRODUCERwait);elseprintf(PRODUCERready);if(p[CONSUMER].statu==WAIT)printf(CONSUMERwait);elseprintf(CONSUMERready);printf(\n);}
本文标题:生产者消费者关系转换程序代码
链接地址:https://www.777doc.com/doc-5078751 .html