资源描述:
《操作系统pv操作实验》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#includc#include#include#definen10#defineNum1()〃定义结构体typedefstructPCB{charproName[10];〃进程名intstate;〃亠等待()■就绪1■运行2■完成intwait_reason;//^r待原因intinterrupt;//'11断状态structPCB*next;〃指针}*novPCB;〃初始化牛:产者进程控制块intinitProducer(novPCB&p){p=(PCB*)malloc(sizeof(PCB));
2、if(!p)exit(-l);p->next=NULL;strcpy(p->proName,"LlH);p->state=0;p->wait_reason=0;p・>imeirupt=0;return1;}staticchary=O;staticcharx=‘0:staticcharB[l()];〃初始化消费者进程控制块intinitConsumer(novPCB&c){c=(PCB*)malloc(sizeof(PCB));if(!c)cxit(-l);conext=NULL;strcpy(c->proName/L2M);c->state=0;c->wait_reason=0;c->in
3、terrupt=0;return1;intp_Op(novPCB&p,int&s);//p操作intv_Op(novPCB&pl,novPCB&p2,int&s);//v操作charproducc(intin,char*input);〃生产voidconsume(charx);〃打印/输岀xintcontrol(novPCB&p,novPCB&c,int&PC,int*PA,int*SA,int&si,int&s2,char*input,int&in,in(&out);intprocessor(novPCB&p,novFCB&c,int&PCjnt*PA,int*SA,int&sl.int
4、&s2,char*input,int&injnt&out);〃主函数intmain(void){〃初始化变量intsi=10;ints2=0;intin=0;intout=0;charinput[10J;intPA[5]={0,l,2,3,4};intSA[5]={0,1,2,3,4};intPC=0;〃初始化进程控制块novPCBp;novPCBc;initProducer(p);initConsumcr(c);inti=0;〃输出菜单printf(n===========================================H);printf(M**“);printf
5、(”*生产者与消费者问题模拟*u);printf(”**u);M);printf(M=====================printf(n请输入字符来表示产品“);for(i=0;i<10;i++){printffflush(stdin);scanf("%c",&input[i]);PC=O;processor(p,c,PC,PA,SA,s1,s2jnput,in,out);system(HpauseM);return1;}charproduce(intin,char*input)〃生产一个产品{returninput[in];voidconsume(charx)〃显示
6、消费的产甜{printf(Hx中的字符为:%c'*,x);intp_Op(novPCB&pjnt&s)//p操作{s=s-1;if(s<0)p->statc=-1;elsep->state=0;return1;}intv_Op(novPCB&pl,novPCB&p2,int&s)//v操作{s=s+1;if(s<=0)p2->state=0;pl->state=0;return1;intcontrol(novPCB&p,novPCB&c,int&PC,int*PA,int*SA,int&sl,int&s2,char*input,int&in,in(&out){inti=PC;intJ=
7、-1;intch=・1;if(p->statc=[=1)J=PALiJ;print”金不不不金苓不半水苓不半金半水不金苓水半不不水半金不水金不半金半不半金半金半金*不vprintf(”生产者进程“);printf(Mi=%dH,i);printf(nJ=%d”,J);printf("p->interrupt=%d",p->interrupt);switch(J){case0:y=produce(inj