实验一进程同步Word文档格式.docx
- 文档编号:16460806
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:11
- 大小:17.14KB
实验一进程同步Word文档格式.docx
《实验一进程同步Word文档格式.docx》由会员分享,可在线阅读,更多相关《实验一进程同步Word文档格式.docx(11页珍藏版)》请在冰豆网上搜索。
运行结束的进程进入over链表。
重复这一过程直至就绪队列为空。
③程序询问是否要继续?
如果继续转开始执行,否则退出程序。
源程序:
#include<
iostream>
usingnamespacestd;
intconstbuffersize=5;
/*定义进程控制块PCB*/
structPCB
{
intflag;
//flag=1denoteproducer;
flag=2denoteconsumer;
intnumlabel;
charproduct;
charstate;
structPCB*processlink;
}*exe=0,*over=0;
PCB*readyhead=0,*readytail=0;
PCB*consumerhead=0,*consumertail=0;
PCB*producerhead=0,*producertail=0;
intproductnum=0;
//产品数量
intprocessnum=0;
//进程数
intfull=0,empty=buffersize;
//semaphore
charbuffer[buffersize];
//缓冲区
intbufferpoint=0;
//缓冲区指针
voidlinkqueue(PCB*process,PCB*&
tail);
PCB*getp(PCB*head,PCB*&
tail);
inthasElement(PCB*pro);
voiddisplay(PCB*p);
voidlinklist(PCB*p,PCB*listhead);
voidfreelink(PCB*linkhead);
intprocessproc();
intwaitfull();
intwaitempty();
voidsignalempty();
voidsignalfull();
voidproducerrun();
voidcomsuerrun();
intmain()
charbegin;
intelement;
cout<
<
"
你想开始程序吗?
(y/n)"
;
cin>
>
begin;
producerhead=newPCB;
if(!
producerhead)return1;
producertail=producerhead;
producerhead->
processlink=0;
flag=1;
numlabel=processnum;
consumerhead=newPCB;
consumerhead)return1;
consumertail=consumerhead;
consumerhead->
flag=2;
readyhead=newPCB;
readyhead)return1;
readytail=readyhead;
readyhead->
flag=3;
over=newPCB;
if(!
over)return1;
over->
while(begin=='
y'
)
{
if(!
processproc())break;
element=hasElement(readyhead);
while(element)
{
exe=getp(readyhead,readytail);
printf("
进程%d申请运行,它是一个"
exe->
numlabel);
exe->
flag==1?
printf("
生产者\n"
):
printf("
消费者\n"
);
if(exe->
flag==1)
producerrun();
else
comsuerrun();
element=hasElement(readyhead);
}
cout<
就绪队列中无进程"
endl;
if(hasElement(consumerhead))
{
cout<
消费者等待队列中有进程:
display(consumerhead);
}
else
消费者等待队列中已无进程!
if(hasElement(producerhead))
生产者等待队列中有进程:
display(producerhead);
生产者等待队列中已无进程!
\n你想继续吗?
(press/'
y/'
for/'
n/'
)"
cin>
}
cout<
\n进程模拟完成."
freelink(over);
over=0;
freelink(readyhead);
readyhead=0;
readytail=0;
freelink(consumerhead);
consumerhead=0;
consumertail=0;
freelink(producerhead);
producerhead=0;
producertail=0;
return0;
}
voidlinklist(PCB*p,PCB*listhead)
{
PCB*cursor=listhead;
while(cursor->
processlink)
{
cursor=cursor->
processlink;
}
cursor->
processlink=p;
voidfreelink(PCB*linkhead)
PCB*p;
while(linkhead)
p=linkhead;
linkhead=linkhead->
delete(p);
tail)
if(tail)
tail->
processlink=process;
tail=process;
else
队列未初始化!
exit
(1);
}
PCB*getp(PCB*head,PCB*&
tail)
p=head->
if(p)
head->
processlink=p->
p->
if(!
head->
processlink)
tail=head;
else
return0;
returnp;
intprocessproc()
intflag,num;
charch;
PCB*p=0;
\n请输入希望产生的进程个数:
num;
for(inti=0;
i<
i++)
\n请输入您要产生的进程:
输入1为生产者进程;
输入2为消费者进程\n"
cin>
flag;
p=newPCB;
p)
内存分配失败!
return0;
}
flag=flag;
processnum++;
if(p->
flag==1)
{
printf("
您要产生的是生产者进程,它是第%d个进程,请输入您要该进程产生的字符:
\n"
processnum);
ch;
p->
product=ch;
productnum++;
printf("
您要该进程产生的字符是%c\n"
p->
product);
else
您要产生的是消费者进程,它是第%d个进程。
linkqueue(p,readytail);
return1;
intwaitempty()
if(empty<
=0)
进程%d:
缓冲区存数,缓冲区满,该进程进入生产者等待队列\n"
linkqueue(exe,producertail);
{
empty--;
intwaitfull()
if(full<
=0)
缓冲区取数,缓冲区空,该进程进入消费者等待队列\n"
linkqueue(exe,consumertail);
full--;
return1;
voidsignalempty()
if(hasElement(producerhead))
p=getp(producerhead,producertail);
linkqueue(p,readytail);
等待中的生产者进程进入就绪队列,它的进程号是%d\n"
empty++;
voidsignalfull()
voidproducerrun()
if(!
waitempty())
return;
进程%d开始向缓冲区存数%c\n"
numlabel,exe->
buffer[bufferpoint]=exe->
product;
bufferpoint++;
进程%d向缓冲区存数操作结束\n"
signalfull();
linklist(exe,over);
voidcomsuerrun()
voiddisplay(PCB*p)
p=p->
while(p)
生产者进程"
消费者进程"
%d\n"
p=p->
inthasElement(PCB*pro)
pro->
三.实验结果
四.实验讨论
收获体会及对该题解的改进意见和见解。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验一 进程同步 实验 进程 同步