資源描述:
《高響應比算法.docx》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、#include#include#definebuffersize5//5個緩沖區(qū)intprocessnum=0;//初始化產(chǎn)品數(shù)量structpcb//定義進程控制塊PCB{intflag;intnumlabel;charproduct;charstate;structpcb*processlink;}*exe=NULL,*over=NULL;typedefstructpcbPCB;PCB*readyhead=NULL,*readytail=NULL;PCB*consu
2、merhead=NULL,*consumertail=NULL;PCB*producerhead=NULL,*producertail=NULL;intproductnum=0;//產(chǎn)品數(shù)量intfull=0,empty=buffersize;//信號量charbuffer[buffersize];//緩沖區(qū)intbufferpoint=0;//緩沖區(qū)指針voidlinklist(PCB*p,PCB*listhead){//創(chuàng)建就緒隊列PCB*cursor=listhead;while(cursor->pro
3、cesslink!=NULL){cursor=cursor->processlink;}cursor->processlink=p;}voidfreelink(PCB*linkhead){PCB*p;while(linkhead!=NULL){p=linkhead;linkhead=linkhead->processlink;free(p);}}voidlinkqueue(PCB*process,PCB**tail)//初始化隊列{if((*tail)!=NULL){(*tail)->processlink=
4、process;(*tail)=process;}else{printf("隊列序列化!");}}PCB*getq(PCB*head,PCB**tail){PCB*p;p=head->processlink;if(p!=NULL){head->processlink=p->processlink;p->processlink=NULL;if(head->processlink==NULL)(*tail)=head;}elsereturnNULL;returnp;}boolprocessproc()//初始化進
5、程{inti,f,num;charch;PCB*p=NULL;PCB**p1=NULL;printf("請輸入希望產(chǎn)生的進程個數(shù):");scanf("%d",&num);getchar();for(i=0;ifla
6、g=f;processnum++;p->numlabel=processnum;p->state='w';p->processlink=NULL;if(p->flag==1){printf("您要產(chǎn)生的進程是生產(chǎn)者,它是第%d個進程。請您輸入您要該進程產(chǎn)生的字符:",processnum);scanf("%c",&ch);getchar();p->product=ch;productnum++;printf("您要該進程產(chǎn)生的字符是%c",p->product);}else{printf("您要生產(chǎn)的
7、進程是消費者,它是第%d個進程。",p->numlabel);}linkqueue(p,&readytail);}returntrue;}boolhasElement(PCB*pro)//判斷隊列中是否有進程存在{if(pro->processlink==NULL)returnfalse;elsereturntrue;}boolwaitempty()//判斷生產(chǎn)者等待隊列是否為空{(diào)if(empty<=0){printf("進程%d:緩存區(qū)存數(shù),緩存區(qū)滿,該進程進入生產(chǎn)者等待序列",exe->numla
8、bel);linkqueue(exe,&producertail);returnfalse;}else{empty--;returntrue;}}voidsignalempty()//喚醒生產(chǎn)者進程{PCB*p;if(hasElement(producerhead)){p=getq(producerhead,&producertail);linkqueue(p,&readytail);printf(