已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2027届安徽省怀远县包集中学数学九上期末考试试题含解析
- 河南省郑州市高新区2027届八年级数学第一学期期末质量检测模拟试题含解析
- 河北省保定定兴县联考2027届九年级数学第一学期期末调研模拟试题含解析
- 祖国啊题目与详细答案
- 2026年食品快速检测市场调查报告
- 2026年财务专员模拟题及答案详解
- 2026年故事趣味模拟题及答案详解
- 2026年产品认证考模拟题及答案详解
- 2026年螺旋压榨脱水机现状及发展趋势
- 2026年大型安全模拟题及答案详解
- T/TMAC 248-2025连续石墨化炉能源消耗限额
- 武汉市2027届高中毕业生九月调研考试物理试卷(含答案及解析)
- 2026广东惠州市博罗县自然资源局补充招聘编外人员6人(第二次)笔试备考题库及答案详解
- IMDG Code 42-24 修正案 锂电池海运包装标记与标签规范 中文版(P903 包装标识更新全解)
- 供应链韧性的理论内涵与战略框架构建
- 《生态环境法典》企业负责人合规培训
- 2026年高中数学与思政融合课教学设计
- 改良早期预警评分系统在急诊内科危重患者院内转运中的应用
- GB/T 4340.2-2025金属材料维氏硬度试验第2部分:硬度计的检验与校准
- 2025年公务员考试《行测》模拟题及答案(详细解析)
- 幼儿园大班健康活动《预防感冒》课件
评论
0/150
提交评论