操作系统实验三_第1页
操作系统实验三_第2页
操作系统实验三_第3页
操作系统实验三_第4页
操作系统实验三_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

1、实验报告 专业 软件工程 班级 学号 094、 094、 101、 103 姓名 陈璐、吴艺萍、崔旻、刘超群 实验日期: 2013 年 10月 17 日 课程 实验名称 报告退发(订正 生产者与消费者问题、 、重做) 读者一写者问题 一、实验目的 1. 实现生产者消费者问题模拟 2. 进一步掌握P, V如何解决同步和互斥问题 实验环境 1. 2. Windows 或 Linux 平台 Eclipse、Visual Studio 2005 或 GCC 实验内容、步骤和结果分析 实验内容: 实现生产者消费者问题模拟,显示每次添加和读取数据时缓冲区的状态, 生产者和消费者可用线程模拟。 1. 一个大

2、小为10的缓冲区,初始为空。 ID), 随机等待一段时间。 若缓冲区已满,等待消费者取走数据后再添加。 若缓冲区可以读取数据, 则示意进入消费过程 (打印出消费者ID ), 随机等待一段时间; 若缓冲区为空,等待生产者添加数据后再读取。 2. 五个生产者:若缓冲区可以加入数据,则示意进入生产过程(打印出生产者 往缓冲区添加数据, 3. 五个消费者: 从缓冲区读取数据, 四、讨论 (说明实验过程中遇到的问题及解决办法;未解决/需进一步研讨的问题或建议新实验方 法等) (请利用实验二中所给的各个版本信号量类来完成实验三。若选用Windows平台,要求 一定要选用这三个文件夹中的某个信号量类Sema

3、 phore来完成实验,否则实验报告视为 缺交;若选用Linux平台,也要求参照已给出的三个版本的Semaphore类的接口,先定 义一个Linux版本的C+类class Semaphore,并在该类基础上完成实验,提交实验报告 时请附上自定义的 Sema phore类。 读者一写 wmutex.VO; F F:M i c rosoft Vi sual Stu diol M yProj ec t 貞接作票统 I a b 30e b u g操作萦统 I a b - 752勺the students tAhlc 24G6urLtt ing the student S table 752峙readi

4、ng tlic student e table ing the student 3 tabla tlis student s tAhls 24G6ui*Lt t ing the student s tabls tbe students tab1b 246SuiLtt ing the student v tabla 24 9 七able ing tbe student 5 table 24GRuri.tC ing the student * s table the etudentF table ZGSwrittinff the studenttable 7524peat1 ing 752read

5、ing 7524peat1insf tbe tbe the tbe students student s student s student s table table table table 2468writtinsf the studenttable 7524readinsf tbe student s table 2468wpittinsf the studenttable 2468writtinsf the studenttable 752readinsf tbe student s table 752readinsf the student s table #inelude #ine

6、lude semaphore.h #inelude thread.h #inelude using namespacestd; Sema phore rmutex(1); Sema phore wmutex(1); int cou nt=0; un sig nedi nt WINA PI Reader( void *p) while(TRUE) rmutex. P(); cou nt+; rmutex.V(); if(eou nt = 1) wmutex. P(); cout GetCurre ntThreadIdO readi ng the stude nts tablee ndl; rmu

7、tex. P(); cou nt-; rmutex.V(); if(cou nt = 0) Slee p(ra ndO%1000); unsignedint WINAPI Writer( void *p) while(TRUE) wmutex .P(); cout GetCurre ntThreadld() writt ing the stude nts table e ndl; wmutex.V(); Slee p(ra ndO%1000); int mai n() HANDLE hThread2; hThread0 = startThread(Reader,NULL); hThread1

8、= startThread(Writer,NULL); :WaitForMult ip leObjects(2,hThread,TRUE,INFINITE); CloseHa ndle(hThread0); CloseHa ndle(hThread1); return 0; 生产者与消费者问题: F F:Microsoft Visual StuclioiMyProjects3作累统bb 3TDebug作票统- 5264 4956 523 495 G 52 495 5264 495 495 B 2 4?5t 5跖崎 4956 526 4956 5264 4956 4956 5264 4956 5

9、2fi4 ConSunT produce! Co nsuRit producef cansunt producef cansunt producel consunif ppoducet consunit produce? consul* produce* consult produce? consun* ptoduce? consunt pLoduce* consunt pi?oduce* cansurnt #inelude #include semaphore.h #include thread.h #inelude using namespacestd; Sema phore full(5

10、); Sema phore emp ty(5); Sema phore mutex(1); int cou nt=O; un sig nedi nt WINA PI P roducer( void *p) while(TRUE) emp ty. P(); mutex. P(); cout GetCurre ntThreadld() p roduce!e ndl; mutex.V(); full.V(); Slee p(ra nd()%1000); un sig nedi nt WINA PI Co nsumer(void *p) while(TRUE) full. P(); mutex. P(); cout GetCurre ntThreadld() con sum!e ndl; mutex.V(); emp ty.V(); Slee p(ra nd()%1000); int mai n() HANDLE hThread2; hThread0 = startThread(

温馨提示

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

评论

0/150

提交评论