东北大学 操作系统实验三报告.docx

上传人:b****5 文档编号:6619088 上传时间:2023-01-08 格式:DOCX 页数:16 大小:404.88KB
下载 相关 举报
东北大学 操作系统实验三报告.docx_第1页
第1页 / 共16页
东北大学 操作系统实验三报告.docx_第2页
第2页 / 共16页
东北大学 操作系统实验三报告.docx_第3页
第3页 / 共16页
东北大学 操作系统实验三报告.docx_第4页
第4页 / 共16页
东北大学 操作系统实验三报告.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

东北大学 操作系统实验三报告.docx

《东北大学 操作系统实验三报告.docx》由会员分享,可在线阅读,更多相关《东北大学 操作系统实验三报告.docx(16页珍藏版)》请在冰豆网上搜索。

东北大学 操作系统实验三报告.docx

东北大学操作系统实验三报告

操作系统实验报告

班级

物联网1302班

学号

姓名

实验3进程同步和通信-生产者和消费者问题模拟

1.目的:

调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。

通过补充新功能,使学生能灵活运用相关知识,培养创新能力。

2.内容及要求:

1)调试、运行模拟程序。

2)发现并修改程序中不完善的地方。

3)修改程序,使用随机数控制创建生产者和消费者的过程。

4)在原来程序的基础上,加入缓冲区的写互斥控制功能,模拟多个进程存取一个公共缓冲区,当有进程正在写缓冲区时,其他要访问该缓冲区的进程必须等待,当有进程正在读取缓冲区时,其他要求读取的进程可以访问,而要求写的进程应该等待。

5)完成1)、2)、3)功能的,得基本分,完成4)功能的加2分,有其它功能改进的再加2分

3.程序说明:

  本程序是模拟两个进程,生产者(producer)和消费者(Consumer)工作。

生产者每次产生一个数据,送入缓冲区中。

消费者每次从缓冲区中取走一个数据。

缓冲区可以容纳8个数据。

因为缓冲区是有限的,因此当其满了时生产者进程应该等待,而空时,消费者进程应该等待;当生产者向缓冲区放入了一个数据,应唤醒正在等待的消费者进程,同样,当消费者取走一个数据后,应唤醒正在等待的生产者进程。

就是生产者和消费者之间的同步。

  每次写入和读出数据时,都将读和写指针加一。

当读写指针同样时,又一起退回起点。

当写指针指向最后时,生产者就等待。

当读指针为零时,再次要读取的消费者也应该等待。

为简单起见,每次产生的数据为0-99的整数,从0开始,顺序递增。

两个进程的调度是通过运行者使用键盘来实现的。

4.程序使用的数据结构

进程控制块:

包括进程名,进程状态和执行次数。

缓冲区:

一个整数数组。

缓冲区说明块:

包括类型,读指针,写指针,读等待指针和写等待指针。

5.程序使用说明

  启动程序后,如果使用'p'键则运行一次生产者进程,使用'c'键则运行一次消费者进程。

通过屏幕可以观察到两个进程的状态和缓冲区变化的情况。

6.实验流程图

 

源程序

#include

#include

#include

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

producer->time=consumer->time=0;

printf("Nowstartingtheprogram!

\n");

printf("Press'p1'torunPRODUCER1,Press'p1'torunPRODUCER2\n");

printf("Press'c1'torunCONSUMER1,Press'c2'torunCONSUMER2\n");

printf("Press'e'toexitfromtheprogram.\n");

while

(1)

{

strcpy(in,"N");

while(!

strcmp(in,"N"))

{

printf("\n");

scanf("%s",in);

if(strcmp(in,"e")&&strcmp(in,"p1")&&strcmp(in,"p2")&&strcmp(in,"c1")&&strcmp(in,"c2"))

{

printf("error,pleaseinputagain!

!

!

\n");

strcpy(in,"N");

}

}

if('p'==in[0])

{

runp(in);

producer->time++;

}

elseif('c'==in[0])

{

runc(in);

consumer->time++;

}

else

{

//printf("PRODUCERproduct%dtimes\n",producer->time);

//printf("CONSUMERconsumer%dtimes\n",consumer->time);

exit(0);

}

print();

printf("\n");

}

}

voidrunp(charin[3])

{

if(full>=8)

{

structwaitqueue*search;

search=headerqueue->next;

while(search!

=NULL)

{

if(!

strcmp(in,search->pcb.name))

{

printf("error!

!

!

\n");

exit

(1);

}

search=search->next;

}

producer->status=WAIT;

printf("PRODUCER%sprocessiswaiting,can'tbescheduled.\n",in);

structwaitqueue*p=(structwaitqueue*)malloc(sizeof(structwaitqueue));

strcpy(p->pcb.name,in);

p->pcb.status=WAIT;

p->pcb.time=producer->time+1;

p->next=NULL;

tailqueue->next=p;

tailqueue=p;

full++;

}

else

{

writein=(writein+1)%100;

producer->status=RUN;

printf("runPRODUCER%sprocess.product%d",in,writein);

buffer[writeptr]=writein;

if(empty>8)

{

structwaitqueue*p;

p=headerqueue->next;

printf("runCONSUMER%sprocess.use%d",p->pcb.name,buffer[writeptr]);

if(tailqueue==p)

{

tailqueue=headerqueue;

}

headerqueue->next=p->next;

free(p);

consumer->status=WAIT;

empty--;

}

else

{

if(writeptr>readptr)

{

writeptr++;

if(writeptr>=PIPESIZE)

{

writeptr=0;

if(readptr==0)

producer->status=WAIT;

elseproducer->status=READY;

}

}

else

{

writeptr++;

if(writeptr==readptr)

producer->status=WAIT;

elseproducer->status=READY;

}

consumer->status=READY;

empty--;

full++;

}

}

}

voidrunc(charin[3])

{

if(empty>=8)

{

structwaitqueue*search;

search=headerqueue->next;

while(search!

=NULL)

{

if(!

strcmp(in,search->pcb.name))

{

printf("error!

!

!

\n");

exit

(1);

}

search=search->next;

}

consumer->status=WAIT;

printf("CONSUMER%siswaiting,can'tbescheduled.\n",in);

structwaitqueue*p=(structwaitqueue*)malloc(sizeof(structwaitqueue));

strcpy(p->pcb.name,in);

p->pcb.status=WAIT;

p->pcb.time=consumer->time+1;

p->next=NULL;

tailqueue->next=p;

tailqueue=p;

empty++;

}

else

{

consumer->status=RUN;

readout=buffer[readptr];

printf("runCONSUMER%sprocess.use%d",in,readout);

if(full>8)

{

writein=(writein+1)%100;

buffer[writeptr]=writein;

structwaitqueue*p;

p=headerqueue->next;

printf("runPRODUCER%sprocess.product%d",p->pcb.name,buffer[writeptr]);

if(tailqueue==p)

{

tailqueue=headerqueue;

}

headerqueue->next=p->next;

free(p);

producer->status=WAIT;

full--;

writeptr++;

readptr++;

}

else

{

if(readptr>writeptr)

{

readptr++;

if(readptr>=PIPESIZE)

{

readptr=0;

if(writeptr==0)

consumer->status=WAIT;

else

consumer->status=READY;

}

elseconsumer->status=READY;

}

else

{

readptr++;

if(readptr==writeptr)

{

consumer->status=WAIT;

writeptr=readptr=0;

}

elseconsumer->status=READY;

producer->status=READY;

}

full--;

empty++;

}

}

}

voidprint()

{

inti=0;

intj=0;

intlow=0;

inthigh=0;

printf("\n");

for(i;i

printf("--------");

printf("\n");

if(readptr

{

for(low=0;low

printf("||");

for(low=readptr;low

{

printf("|%2d|",buffer[low]);

}

for(low=writeptr;low

printf("||");

}

elseif(readptr>writeptr)

{

for(low=0;low

printf("|%2d|",buffer[low]);

for(low=writeptr;low

printf("||");

for(low=readptr;low

{

printf("|%2d|",buffer[low]);

}

}

elseif(producer->status==WAIT)

{

for(low=0;low

{

printf("|%2d|",buffer[low]);

}

}

else

{

for(low=0;low

{

printf("||");

}

}

printf("\n");

for(i=0;i

printf("--------");

printf("\n");

if(producer->status==WAIT&&NULL!

=headerqueue->next)

{

structwaitqueue*p=headerqueue->next;

while(NULL!

=p)

{

printf("PRODUCER%sprocessarewaitting\n",p->pcb.name);

p=p->next;

}

}

else

printf("PRODUCERready\n");

if(consumer->status==WAIT&&headerqueue->next)

{

structwaitqueue*p=headerqueue->next;

while(NULL!

=p)

{

printf("CONSUMER%sprocessarewaitting\n",p->pcb.name);

p=p->next;

}

}

else

printf("CONSUMERready\n");

}

实验结果:

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

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1