操作系统-生产者与消费者源代码

操作系统-生产者与消费者源代码

ID:38632789

大小:42.50 KB

页数:6页

时间:2019-06-16

操作系统-生产者与消费者源代码_第1页
操作系统-生产者与消费者源代码_第2页
操作系统-生产者与消费者源代码_第3页
操作系统-生产者与消费者源代码_第4页
操作系统-生产者与消费者源代码_第5页
资源描述:

《操作系统-生产者与消费者源代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

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);//创建进程进入就绪队列

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。