Nachos 实验3:Synchronization Using Semaphores.doc_第1页
Nachos 实验3:Synchronization Using Semaphores.doc_第2页
Nachos 实验3:Synchronization Using Semaphores.doc_第3页
Nachos 实验3:Synchronization Using Semaphores.doc_第4页
Nachos 实验3:Synchronization Using Semaphores.doc_第5页
全文预览已结束

下载本文档

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

文档简介

实验目标在本次实验中,使用信号量,编写一段程序解决生产者/消费者同步问题。完成本次试验,理解Nachos的信号量是如何实现的生产者/消费者问题是如何用信号量实现的在Nachos中是如何创建并发线程的在Nachos下是如何测试和debug的实验环境Linux关键源代码注释Ringring与slot的关系如图所示,在prodcons+中,定义ring的BUFF_SIZE大小为3。生产者nempty-P();查看buffer是否有空,if nempty0,nempty=nempty -1(用nempty代替nempty.value,下nfull同).mutex-P();加锁.向ring中放入message信息.mutex-V();解锁.nfull-V();通知消费者buffer有新信息,nfull=nfull+1.关键代码如下:voidProducer(_int which) int num;slot *message = new slot(0,0);/建立slot实例,此信息message将被放到ring buffer里。每个信息message有一个id和一个value。/每个生产者产生的商品的个数必须使用N_MESSG限定,否则程序没有结束 for (num = 0; num thread_id=which; /producer的id message-value=num; /产生的message的编号 nempty-P(); mutex-P(); ring-Put(message); mutex-V(); nfull-V(); 消费者nfull-P();查看buffer中是否有信息,if nfull0,nfull-1.mutex-P();加锁.从ring buffer中取出信息.mutex-V();解锁.nempty-V();通知生产者bufferr有空nempty=nempty+1.关键代码如下:voidConsumer(_int which) char strMAXLEN; char fnameLINELEN; int fd; slot *message = new slot(0,0); / to form a output file name for this consumer thread. / all the messages received by this consumer will be recorded in / this file. sprintf(fname, tmp_%d, which); / create a file. Note that this is a UNIX system call. if ( (fd = creat(fname, 0600) ) = -1) perror(creat: file create failed); Exit(1); /不需要规定每个消费者消费的商品的个数 for (; ; ) nfull-P(); mutex-P(); ring-Get(message); mutex-V(); nempty-V(); / form a string to record the message sprintf(str,producer id - %d; Message number - %d;n, message-thread_id, message-value); / write this string into the output file of this consumer. / note that this is another UNIX system call. if ( write(fd, str, strlen(str) = -1 ) perror(write: write failed); Exit(1); 生产者/消费者问题的解决mutex = new Semaphore(mutux,1);信号量初始化为1,才能起到加锁功能nfull = new Semaphore(full,0);nfull的大小在生产者没生产前为0nempty = new Semaphore(empty,BUFF_SIZE);nempty的大小应该为buffer的大小创建线程生成一个生产者的代码:producersi = new Thread(prod_namesi);producersi - Fork(Producer,i);关键代码如下:voidProdCons() int i;/建立信号量mutex = new Semaphore(mutux,1); nfull = new Semaphore(full,0); nempty = new Semaphore(empty,BUFF_SIZE); / 建立ring buffer,大小为BUFF_SIZE ring = new Ring(BUFF_SIZE); / 建立并唤醒 N_PROD个生产者线程 for (i=0; i Fork(Producer,i); ; / 建立并唤醒N_CONS 个消费者线程 for (i=0; i Fork(Consumer,i); ;调试记录源代码中exit(0)没有大写,增加了许多无谓的劳动。调试过程中因为消费者消费记录写入文件部分一直报错,曾打算将写入文件改为终端显示,但这最终将会看到生产者产生的商品按产生顺序被顺序打印,根本无法看出是那个被消费者获取。后来终于注意到Exit,顺利通过调试。运行分析输出文件:The contents of tmp_0producer_id-0; Message number -0;producer_id-0; Message number -1;the contents of tmp_1producer_id-0; Message number -2;producer_id-0; Message number -3;producer_id-1; Message number -0;producer_id-1; Message nu

温馨提示

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

评论

0/150

提交评论