操作系统生产者与消费者源代码.docx
《操作系统生产者与消费者源代码.docx》由会员分享,可在线阅读,更多相关《操作系统生产者与消费者源代码.docx(9页珍藏版)》请在冰点文库上搜索。
操作系统生产者与消费者源代码
/*生产者/消费者*/
#include
#include
#include
structPCB
{
intflag;//1为生产者,2为消费者
intnumLabel;
};
typedefstructQNode
{
PCBdata;//数据域
structQNode*next;//指针域
}QNode,*QueuePtr;
typedefstruct
{
QueuePtrfront;//队头指针
QueuePtrrear;//队尾指针
}LinkQueue;
typedefstructLNode
{
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(LinkQueue&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.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<<"请输入进程的个数:
";//确定进程个数,默认为0
cin>>processNum;
for(inti=0;i{
cout<<"输入第"<
(1)/如果是消费者请输入
(2)";
structPCBpcb;
cin>>pcb.flag;//输入进程的种类
pcb.numLabel=i+1;//进程序号赋值
QueuePtrp=(QueuePtr)malloc(sizeof(QNode));
p->data=pcb;
EnQueue(Q,p);
}
}
boolHasElement(LinkQueueQ)//判断队列是否为空
{
if(Q.front==Q.rear)
return0;
else
return1;
}
intProduceRun(int&full,intBufferSize)//运行生产者进程
{
if(full{
full++;
return1;
}
return0;
}
intConsumeRun(int&full,intBufferSize)//运行消费者进程
{
if(full>0)
{
full--;
return1;
}
return0;
}
voidDisPlay(LinkQueueQ)//打印队列
{
QueuePtrp=Q.front;
while(p->next)
{
cout<<"进程"<next->data.numLabel<p=p->next;
}
}
voidmain()
{
intBufferSize;//设置缓冲区大小
cout<<"请设置缓冲区的大小:
";
cin>>BufferSize;
intfull=0;//当前缓冲区中的进程数目
inttemp=1;
LinkListover;//用于收集已经运行结束的进程
LinkListInit(over);
LinkQueueReadyQueue;//就绪队列
LinkQueueProducerWaitQueue;//生产者等待队列
LinkQueueConsumerWaitQueue;//消费者等待队列
//初始化
QueueInit(ReadyQueue);
QueueInit(ProducerWaitQueue);
QueueInit(ConsumerWaitQueue);
while(temp)//死循环
{
processproc(ReadyQueue);//创建进程进入就绪队列
boolelement=HasElement(ReadyQueue);//判断队列是否为空
while(element)//当它不是空的
{
cout<<"进程"<next->data.numLabel<<"申请运行,他是一个";
/********************************************************************/
if(ReadyQueue.front->next->data.flag==1)//如果它是一个生产者
{
cout<<"生产者"<if(ProduceRun(full,BufferSize)==1)//判断缓存区是否还有空间
{
cout<<"进程"<next->data.numLabel<<"执行完毕"<LinkListInsert(over,DeQueue(ReadyQueue));//运行结束,进入over链表
/***********/
if(HasElement(ConsumerWaitQueue))
{
ConsumeRun(full,BufferSize);
outqueue(ConsumerWaitQueue);
}
/***********/
if(HasElement(ProducerWaitQueue))//检查生产者等待队列,激活队列中的进程进入就绪队列
EnQueue(ReadyQueue,DeQueue(ProducerWaitQueue));
}
elseif(ProduceRun(full,BufferSize)==0)//就绪队列出一个元素,进入生产者等待队列
{
EnQueue(ProducerWaitQueue,DeQueue(ReadyQueue));
}
}
else
/*************************************************/
{//如果它是一个消费者
cout<<"消费者"<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);
}
/***********/
}
elseif(ConsumeRun(full,BufferSize)==0)//若没有进程,则进入消费者等待队列
{
EnQueue(ConsumerWaitQueue,DeQueue(ReadyQueue));
}
}
/***************************************************/
/*输出就绪队列*/
element=HasElement(ReadyQueue);
if(HasElement(ReadyQueue))
{
cout<<"就绪队列中有下列进程:
"<DisPlay(ReadyQueue);
}
elsecout<<"就绪队列里面没有进程"</***************************************************/
/*输出生产者等待队列*/
if(HasElement(ProducerWaitQueue))
{
cout<<"生产者等待队列中有下列进程:
"<DisPlay(ProducerWaitQueue);
}
elsecout<<"生产者等待队列中没有进程"</***************************************************/
/*输出消费者等待队列*/
if(HasElement(ConsumerWaitQueue))
{
cout<<"消费者等待队列中有下列进程:
"<DisPlay(ConsumerWaitQueue);
}
elsecout<<"消费者等待队列中没有进程"<getchar();
}
cout<<"是否继续?
Y(输入:
1)/N(输入:
0)";
cin>>temp;
}
}