东北大学 操作系统实验三报告Word文档下载推荐.docx
- 文档编号:19632844
- 上传时间:2023-01-08
- 格式:DOCX
- 页数:16
- 大小:404.88KB
东北大学 操作系统实验三报告Word文档下载推荐.docx
《东北大学 操作系统实验三报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《东北大学 操作系统实验三报告Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
为简单起见,每次产生的数据为0-99的整数,从0开始,顺序递增。
两个进程的调度是通过运行者使用键盘来实现的。
4.程序使用的数据结构
进程控制块:
包括进程名,进程状态和执行次数。
缓冲区:
一个整数数组。
缓冲区说明块:
包括类型,读指针,写指针,读等待指针和写等待指针。
5.程序使用说明
启动程序后,如果使用'
p'
键则运行一次生产者进程,使用'
c'
键则运行一次消费者进程。
通过屏幕可以观察到两个进程的状态和缓冲区变化的情况。
6.实验流程图
源程序
#include<
stdio.h>
stdlib.h>
string.h>
#definePIPESIZE8
enumStatus{RUN,WAIT,READY};
#defineNORMAL0
#defineSLEEP1
#defineAWAKE2
structPCB
{
charname[3];
enumStatusstatus;
inttime;
};
structwaitqueue
structPCBpcb;
structwaitqueue*next;
structPCB*producer;
/*writewaitpoint*/
structPCB*consumer;
/*readwaitpoint*/
structwaitqueue*headerqueue,*tailqueue;
intwriteptr;
intreadptr;
intwritein,readout;
intbuffer[PIPESIZE];
intempty,full;
voidrunp(charin[3]),runc(charin[3]),print();
intmain()
charin[3];
writeptr=0;
readptr=0;
writein=0;
empty=PIPESIZE;
full=0;
producer=(structPCB*)malloc(sizeof(structPCB));
consumer=(structPCB*)malloc(sizeof(structPCB));
headerqueue=(structwaitqueue*)malloc(sizeof(structwaitqueue));
headerqueue->
next=NULL;
tailqueue=headerqueue;
producer->
status=READY;
consumer->
status=WAIT;
time=consumer->
time=0;
printf("
Nowstartingtheprogram!
\n"
);
Press'
p1'
torunPRODUCER1,Press'
torunPRODUCER2\n"
c1'
torunCONSUMER1,Press'
c2'
torunCONSUMER2\n"
e'
toexitfromtheprogram.\n"
while
(1)
{
strcpy(in,"
N"
while(!
strcmp(in,"
))
{
printf("
scanf("
%s"
in);
if(strcmp(in,"
e"
)&
&
strcmp(in,"
p1"
p2"
c1"
c2"
{
printf("
error,pleaseinputagain!
!
strcpy(in,"
}
}
if('
==in[0])
runp(in);
producer->
time++;
elseif('
runc(in);
consumer->
else
//printf("
PRODUCERproduct%dtimes\n"
producer->
time);
CONSUMERconsumer%dtimes\n"
consumer->
exit(0);
print();
printf("
}
}
voidrunp(charin[3])
if(full>
=8)
structwaitqueue*search;
search=headerqueue->
next;
while(search!
=NULL)
if(!
strcmp(in,search->
pcb.name))
error!
exit
(1);
search=search->
producer->
PRODUCER%sprocessiswaiting,can'
tbescheduled.\n"
structwaitqueue*p=(structwaitqueue*)malloc(sizeof(structwaitqueue));
strcpy(p->
pcb.name,in);
p->
pcb.status=WAIT;
pcb.time=producer->
time+1;
tailqueue->
next=p;
tailqueue=p;
full++;
else
writein=(writein+1)%100;
status=RUN;
runPRODUCER%sprocess.product%d"
in,writein);
buffer[writeptr]=writein;
if(empty>
8)
structwaitqueue*p;
p=headerqueue->
runCONSUMER%sprocess.use%d"
p->
pcb.name,buffer[writeptr]);
if(tailqueue==p)
tailqueue=headerqueue;
headerqueue->
next=p->
free(p);
empty--;
if(writeptr>
readptr)
writeptr++;
if(writeptr>
=PIPESIZE)
{
writeptr=0;
if(readptr==0)
producer->
elseproducer->
}
else
if(writeptr==readptr)
producer->
elseproducer->
full++;
voidrunc(charin[3])
if(empty>
consumer->
CONSUMER%siswaiting,can'
in);
pcb.time=consumer->
empty++;
readout=buffer[readptr];
runCONSUMER%sprocess.use%d"
in,readout);
if(full>
writein=(writein+1)%100;
buffer[writeptr]=writein;
runPRODUCER%sprocess.product%d"
full--;
writeptr++;
readptr++;
if(readptr>
writeptr)
readptr++;
if(readptr>
readptr=0;
if(writeptr==0)
consumer->
else
elseconsumer->
if(readptr==writeptr)
consumer->
writeptr=readptr=0;
producer->
empty++;
voidprint()
inti=0;
intj=0;
intlow=0;
inthigh=0;
for(i;
i<
PIPESIZE;
i++)
--------"
if(readptr<
for(low=0;
low<
readptr;
low++)
||"
for(low=readptr;
writeptr;
|%2d|"
buffer[low]);
for(low=writeptr;
PIPESIZE;
elseif(readptr>
elseif(producer->
status==WAIT)
for(i=0;
if(producer->
status==WAIT&
NULL!
=headerqueue->
next)
structwaitqueue*p=headerqueue->
while(NULL!
=p)
PRODUCER%sprocessarewaitting\n"
pcb.name);
p=p->
PRODUCERready\n"
if(consumer->
headerqueue->
CONSUMER%sprocessarewaitting\n"
CONSUMERready\n"
实验结果:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 东北大学 操作系统实验三报告 操作系统 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)