东北大学 操作系统实验三报告.doc_第1页
东北大学 操作系统实验三报告.doc_第2页
东北大学 操作系统实验三报告.doc_第3页
东北大学 操作系统实验三报告.doc_第4页
东北大学 操作系统实验三报告.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

操作系统实验报告 班 级物联网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#define PIPESIZE 8enum Status RUN, WAIT, READY ;#define NORMAL 0#define SLEEP 1#define AWAKE 2struct PCBchar name3;enum Status status;int time;struct waitqueuestruct PCB pcb;struct waitqueue *next;struct PCB *producer; /* write wait point */struct PCB *consumer;/* read wait point */struct waitqueue *headerqueue, *tailqueue;int writeptr;int readptr;int writein, readout;int bufferPIPESIZE;int empty, full;void runp(char in3), runc(char in3), print();int main()char in3;writeptr = 0;readptr = 0;writein = 0;empty = PIPESIZE;full = 0;producer = (struct PCB *)malloc(sizeof(struct PCB);consumer = (struct PCB *)malloc(sizeof(struct PCB);headerqueue = (struct waitqueue *)malloc(sizeof(struct waitqueue);headerqueue-next = NULL;tailqueue = headerqueue;producer-status = READY;consumer-status = WAIT;producer-time = consumer-time = 0;printf(Now starting the program!n);printf(Press p1 to run PRODUCER1,Press p1 to run PRODUCER2 n);printf(Press c1 to run CONSUMER1,Press c2 to run CONSUMER2 n);printf(Press e to exit from the program.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,please input again!n);strcpy(in, N);if (p = in0)runp(in);producer-time+;else if (c = in0)runc(in);consumer-time+;else/printf(PRODUCER product %d timesn, producer-time);/printf(CONSUMER consumer %d timesn, consumer-time);exit(0);print();printf(n);void runp(char in3)if (full = 8)struct waitqueue *search;search = headerqueue-next;while (search != NULL)if (!strcmp(in, )printf(error!n);exit(1);search = search-next;producer-status = WAIT;printf(PRODUCER %s process is waiting, cant be scheduled.n, in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(, in);p-pcb.status = WAIT;p-pcb.time = producer-time + 1;p-next = NULL;tailqueue-next = p;tailqueue = p;full+;elsewritein = (writein + 1) % 100;producer-status = RUN;printf(run PRODUCER %s process . product %d , in, writein);bufferwriteptr = writein;if (empty8)struct waitqueue *p;p = headerqueue-next;printf(run CONSUMER %s process. use %d, , bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;headerqueue-next = p-next;free(p);consumer-status = WAIT;empty-;elseif (writeptr readptr)writeptr+;if (writeptr = PIPESIZE)writeptr = 0;if (readptr = 0)producer-status = WAIT;else producer-status = READY;elsewriteptr+;if (writeptr = readptr)producer-status = WAIT;else producer-status = READY;consumer-status = READY;empty-;full+;void runc(char in3)if (empty = 8)struct waitqueue *search;search = headerqueue-next;while (search != NULL)if (!strcmp(in, )printf(error!n);exit(1);search = search-next;consumer-status = WAIT;printf(CONSUMER %s is waiting, cant be scheduled.n,in);struct waitqueue *p = (struct waitqueue *)malloc(sizeof(struct waitqueue);strcpy(, in);p-pcb.status = WAIT;p-pcb.time = consumer-time + 1;p-next = NULL;tailqueue-next = p;tailqueue = p;empty+;elseconsumer-status = RUN;readout = bufferreadptr;printf(run CONSUMER %s process. use %d , in, readout);if (full8)writein = (writein + 1) % 100;bufferwriteptr = writein;struct waitqueue *p;p = headerqueue-next;printf(run PRODUCER %s process. product %d , , bufferwriteptr);if (tailqueue = p)tailqueue = headerqueue;headerqueue-next = p-next;free(p);producer-status = WAIT;full-;writeptr+;readptr+;elseif (readptr writeptr)readptr+;if (readptr = PIPESIZE)readptr = 0;if (writeptr = 0)consumer-status = WAIT;elseconsumer-status = READY;else consumer-status = READY;elsereadptr+;if (readptr = writeptr)consumer-status = WAIT;writeptr = readptr = 0;else consumer-status = READY;producer-status = READY;full-;empty+;void print()int i = 0;int j = 0;int low = 0;int high = 0;printf(n);for (i; i PIPESIZE; i+)printf(-);printf(n);if (readptr writeptr)for (low = 0; low readptr; low+)printf(|);for (low = readptr; low writeptr; low+)printf(| %2d|, bufferlow);for (low = writeptr; low writeptr)for (low = 0; low writeptr; low+)printf(| %2d|, bufferlow);for (low = writeptr; low readptr; low+)printf(|);for (low = readptr; low status = WAIT)for (low = 0; low PIPESIZE; low+)printf(| %2d |, bufferlow);elsefor (low = 0; low PIPESIZE; low+)printf(|);printf(n);for (i = 0; i status = WAIT & NULL !=headerqueue-next)struct waitqueue *p = headerqueue-next;while (NULL != p)printf( PRODUCER

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论