欢迎来到天天文库
浏览记录
ID:38632789
大小:42.50 KB
页数:6页
时间:2019-06-16
《操作系统-生产者与消费者源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、/*生产者/消费者*/#include#include#includestructPCB{intflag;//1为生产者,2为消费者intnumLabel;};typedefstructQNode{PCBdata;//数据域structQNode*next;//指针域}QNode,*QueuePtr;typedefstruct{QueuePtrfront;//队头指针QueuePtrrear;//队尾指针}LinkQueue;typed
2、efstructLNode{QueuePtrdata;structLNode*next;}LNode,*LinkList;voidQueueInit(LinkQueue&Q)//初始化队列{Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));Q.front->next=NULL;}voidLinkListInit(LinkList&L)//初始化链表{L=(LinkList)malloc(sizeof(LNode));}voidEnQueue(LinkQu
3、eue&Q,QueuePtrp)//入队{p->next=NULL;Q.rear->next=p;Q.rear=p;}QueuePtrDeQueue(LinkQueue&Q)//出队{QueuePtrp=Q.front->next;Q.front->next=p->next;if(Q.rear==p)Q.rear=Q.front;returnp;}voidoutqueue(LinkQueue&Q){QueuePtrp=Q.front->next;Q.front->next=p->next;if(Q
4、.rear==p)Q.rear=Q.front;}voidLinkListInsert(LinkList&L,QueuePtre)//进入链表{LinkListp=L;LinkListq=(LinkList)malloc(sizeof(LNode));while(p->next)p++;q->data=e;q->next=NULL;p->next=q;}voidprocessproc(LinkQueue&Q)//创建进程进入初始队列{intprocessNum=0;cout<<"请输入进程的个数:
5、";//确定进程个数,默认为0cin>>processNum;for(inti=0;i>pcb.flag;//输入进程的种类pcb.numLabel=i+1;//进程序号赋值QueuePtrp=(QueuePtr)malloc(sizeof(QNode));p->data=pcb;EnQueue(Q,p);}}boolHasE
6、lement(LinkQueueQ)//判断队列是否为空{if(Q.front==Q.rear)return0;elsereturn1;}intProduceRun(int&full,intBufferSize)//运行生产者进程{if(full0){full--;return1;}return0;}voidDis
7、Play(LinkQueueQ)//打印队列{QueuePtrp=Q.front;while(p->next){cout<<"进程"<next->data.numLabel<next;}}voidmain(){intBufferSize;//设置缓冲区大小cout<<"请设置缓冲区的大小:";cin>>BufferSize;intfull=0;//当前缓冲区中的进程数目inttemp=1;LinkListover;//用于收集已经运行结束的进程LinkListInit
8、(over);LinkQueueReadyQueue;//就绪队列LinkQueueProducerWaitQueue;//生产者等待队列LinkQueueConsumerWaitQueue;//消费者等待队列//初始化QueueInit(ReadyQueue);QueueInit(ProducerWaitQueue);QueueInit(ConsumerWaitQueue);while(temp)//死循环{processproc(ReadyQueue);//创建进程进入就绪队列
此文档下载收益归作者所有