




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/*生产者/消费者*/#include#include#includestruct PCB int flag;/1为生产者,2为消费者int numLabel;typedef struct QNodePCB data;/数据域struct QNode* next;/指针域QNode, *QueuePtr;typedef structQueuePtr front;/队头指针QueuePtr rear;/队尾指针LinkQueue;typedef struct LNodeQueuePtr data;struct LNode *next;LNode, *LinkList;void QueueInit(LinkQueue &Q)/初始化队列Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode);Q.front-next = NULL;void LinkListInit(LinkList& L)/初始化链表L = (LinkList)malloc(sizeof(LNode);void EnQueue(LinkQueue &Q, QueuePtr p)/入队p-next = NULL;Q.rear-next = p;Q.rear = p;QueuePtr DeQueue(LinkQueue &Q)/出队QueuePtr p = Q.front-next;Q.front-next = p-next;if(Q.rear = p)Q.rear =Q.front;return p;void outqueue(LinkQueue& Q) QueuePtr p = Q.front-next;Q.front-next = p-next;if(Q.rear = p)Q.rear =Q.front;void LinkListInsert(LinkList &L, QueuePtr e)/进入链表LinkList p = L;LinkList q = (LinkList)malloc(sizeof(LNode);while(p-next)p+;q-data = e;q-next = NULL;p-next = q;void processproc(LinkQueue &Q)/创建进程进入初始队列int processNum = 0;coutprocessNum;for(int i = 0; i processNum; i+)cout输入第i+1pcb.flag;/输入进程的种类pcb.numLabel = i+1;/进程序号赋值QueuePtr p = (QueuePtr)malloc(sizeof(QNode);p-data = pcb;EnQueue(Q, p);bool HasElement(LinkQueue Q)/判断队列是否为空if(Q.front = Q.rear)return 0;elsereturn 1;int ProduceRun(int &full, int BufferSize)/运行生产者进程if(full 0)full-;return 1;return 0;void DisPlay(LinkQueue Q)/打印队列QueuePtr p = Q.front;while(p-next)cout进程next-data.numLabelnext;void main()int BufferSize;/设置缓冲区大小coutBufferSize; int full = 0;/当前缓冲区中的进程数目int temp = 1;LinkList over;/用于收集已经运行结束的进程LinkListInit(over);LinkQueue ReadyQueue;/就绪队列LinkQueue ProducerWaitQueue;/生产者等待队列LinkQueue ConsumerWaitQueue;/消费者等待队列/初始化QueueInit(ReadyQueue);QueueInit(ProducerWaitQueue);QueueInit(ConsumerWaitQueue);while(temp)/死循环processproc(ReadyQueue);/创建进程进入就绪队列bool element=HasElement(ReadyQueue);/判断队列是否为空while(element)/当它不是空的cout进程next-data.numLabelnext-data.flag = 1)/如果它是一个生产者cout生产者endl;if(ProduceRun(full, BufferSize) = 1)/判断缓存区是否还有空间cout进程next-data.numLabel执行完毕endl;LinkListInsert(over, DeQueue(ReadyQueue);/运行结束,进入over链表/*/if(HasElement(ConsumerWaitQueue) ConsumeRun(full, BufferSize);outqueue(ConsumerWaitQueue); /*/if(HasElement(ProducerWaitQueue)/检查生产者等待队列,激活队列中的进程进入就绪队列EnQueue(ReadyQueue, DeQueue(ProducerWaitQueue);else if(ProduceRun(full, BufferSize) = 0)/就绪队列出一个元素,进入生产者等待队列EnQueue(ProducerWaitQueue, DeQueue(ReadyQueue);else/*/如果它是一个消费者cout消费者endl;if(ConsumeRun(full, BufferSize) = 1)/判断缓存区是否存在进程LinkListInsert(over, DeQueue(ReadyQueue);/运行结束,进入over链表if(HasElement(ConsumerWaitQueue)/检查消费者等待队列,激活队列中的进程进入就绪队列EnQueue(ReadyQueue, DeQueue(ConsumerWaitQueue); /*/if(HasElement(ProducerWaitQueue) ProduceRun(full, BufferSize);outqueue(ProducerWaitQueue); /*/else if(ConsumeRun(full, BufferSize) = 0)/若没有进程,则进入消费者等待队列EnQueue(ConsumerWaitQueue, DeQueue(ReadyQueue);/*/*输出就绪队列*/element = HasElement(ReadyQueue);if(HasElement(ReadyQueue)cout就绪队列中有下列进程:endl;DisPlay(ReadyQueue);else cout就绪队列里面没有进程endl;/*/*输出生产者等待队列*/if(HasElement(ProducerWaitQueue)cout生产者等待队列中有下列进程:endl;DisPlay(ProducerWaitQueue);else cout
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 推拿治疗学试题附答案详解【综合题】
- 解析卷-黑龙江省虎林市中考数学真题分类(数据分析)汇编定向练习试题(解析卷)
- 组合逻辑电路分析与设计试题及答案
- 2025年学前教育机构师资队伍教师培训与课程设计研究报告
- 济南先行投资集团有限责任公司及权属公司招聘笔试题库参考答案详解
- 2025年智能物流系统研发资金申请报告
- 考点解析-北师大版8年级数学上册期中试题及答案详解一套
- 2025至2030年中国硫铝酸盐水泥行业发展前景及投资战略咨询报告
- 2025年深海矿产资源勘探技术深海资源勘探技术产业发展与政策支持报告
- 2025年肿瘤早筛技术在宫颈癌早期诊断中的应用前景与市场潜力报告
- 18项医疗核心制度题库(含答案)
- 科技美肤基础知识培训课件
- 《幼儿园开学安全第一课》课件
- 2026届高考山东省启思教育高三暑假线上第一次模拟考试数学试题
- 企业质量管理培训
- 《俗世奇人》整本书导读课件
- 餐饮管理安全管理制度
- 传统白酒生产项目商业计划书
- 2025年物流仓储行业当前竞争格局与未来发展趋势分析报告
- 西柏坡景点讲解
- 2025年教师资格证考试综合素质(中学)试题及答案
评论
0/150
提交评论