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

上传人:b****4 文档编号:4974567 上传时间:2023-05-07 格式:DOCX 页数:9 大小:15.75KB
下载 相关 举报
操作系统生产者与消费者源代码.docx_第1页
第1页 / 共9页
操作系统生产者与消费者源代码.docx_第2页
第2页 / 共9页
操作系统生产者与消费者源代码.docx_第3页
第3页 / 共9页
操作系统生产者与消费者源代码.docx_第4页
第4页 / 共9页
操作系统生产者与消费者源代码.docx_第5页
第5页 / 共9页
操作系统生产者与消费者源代码.docx_第6页
第6页 / 共9页
操作系统生产者与消费者源代码.docx_第7页
第7页 / 共9页
操作系统生产者与消费者源代码.docx_第8页
第8页 / 共9页
操作系统生产者与消费者源代码.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

《操作系统生产者与消费者源代码.docx》由会员分享,可在线阅读,更多相关《操作系统生产者与消费者源代码.docx(9页珍藏版)》请在冰点文库上搜索。

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

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

/*生产者/消费者*/

#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;

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 表格模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2