操作系统-生产者与消费者源代码(共6页).doc
精选优质文档-倾情为你奉上/*生产者/消费者*/#include<stdio.h>#include<malloc.h>#include<iostream.h>struct 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;cout<<"请输入进程的个数:"/确定进程个数,默认为0cin>>processNum;for(int i = 0; i < processNum; i+)cout<<"输入第"<<i+1<<"个进程的种类,如果是生产者请输入(1)/如果是消费者请输入(2)"struct PCB pcb;cin>>pcb.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 < BufferSize)full+;return 1;return 0;int ConsumeRun(int &full, int BufferSize)/运行消费者进程if(full > 0)full-;return 1;return 0;void DisPlay(LinkQueue Q)/打印队列QueuePtr p = Q.front;while(p->next)cout<<"进程"<<p->next->data.numLabel<<endl;p = p->next;void main()int BufferSize;/设置缓冲区大小cout<<"请设置缓冲区的大小: "cin>>BufferSize; 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<<"进程"<<ReadyQueue.front->next->data.numLabel<<"申请运行,他是一个"/*/if(ReadyQueue.front->next->data.flag = 1)/如果它是一个生产者cout<<"生产者"<<endl;if(ProduceRun(full, BufferSize) = 1)/判断缓存区是否还有空间cout<<"进程"<<ReadyQueue.front->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<<"生产者等待队列中没有进程"<<endl;/*/*输出消费者等待队列*/if(HasElement(ConsumerWaitQueue)cout<<"消费者等待队列中有下列进程:"<<endl;DisPlay(ConsumerWaitQueue);else cout<<"消费者等待队列中没有进程"<<endl;getchar();cout<<"是否继续?Y (输入:1)/N( 输入:0)"cin>>temp;专心-专注-专业