操作系统实验二Mutex_第1页
操作系统实验二Mutex_第2页
操作系统实验二Mutex_第3页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

1、操作系统实验一:废鞍冬学号:09012431日期:15/4/29实验容:在Windows和Linux操作系统上,利用各自操作系统提供的Mutex和信号呈:机制(Win32 API 或Pthreads)实现生产者/消费者问题。具体要求见Operating System Concepts(Seventh Edition) Chapter 6 后的 Project (P236-241 )。实验目的:通过实验,理解Win32 APL Pthreads中mutex lockssemaphores等使用方法,并掌握如何 利用它们实现进程(线程)间的同步和互斥。设计思路和流程图生产者/消费者主要考虑两个同步

2、问题:1、产品队列被两者共同访问,因此需要保持产品变化的互斥性:2、产品有上限.因此生产者不能无限生产,多个生产者应该保持同步:消费者亦然。/生产者Do/produce an itemWait(empty);/队列有空位才能将产品放入Wait(mutex);/互斥访问队列/add productSignal(mutex);Signal(full);/告诉消费者有产品可以取用消费者DoWait(full);/队列有产品才能消费Wait(mutex);/互斥访间队列/consume productSignal(mutex);Signal(full);/告诉生产者队列有空位主要数据结构及其说明int

3、 BuffM:有限缓冲队列HANDLE/sem_t Mutex:互斥锁HANDLE/sem_t Full/Empty:队列有物品/空位的信号源程序并附上注释/WIN32include #include#include #include 系统调用#include#includeusing namespace std;#define M 10#define RandomJVIax 50插入随机数最大值intbuffIM=0;/ 冲区大小为 10int in=0;生产者下标int out=0;消费者下标int prod_id=0,proc_id=0;/初始化 idbool con=true;HAND

4、LE Empty=NULL; 空缓冲信号捲HANDLE Full=NULL:满缓冲信号量HANDLE Mutcx=NULL;/缓冲区互斥锁void produce();void consumeO;UINT _stdcall ThreadProd(LPVOID);UINT _stdcall ThreadCons(LPVOID);生产void produce()随机产生“产品”放入队列srand(time(0);int j = rand() % Random_Max;buffin=j;/打印生产信息coutH produce HjH into bufferHendl;指针移动,下一个空项in =

5、(in+l)%M;)消费void consume()coutM consume Hbuffoutn from bufferHendI;指针移动,下一个满项out = (out+l)%M;生产者入口函数UINT _stdcall ThreadProd(LPVOID)int id=+prod_id;while(l) 初始化srand(time(0);int i = rand() % 1OOO;Sleep(i+3000);等待队列空WaitForSingleObject(EmptyJNFINITE); 等待互斥锁WaitForSingleObject(Mutex,INFINITE); 打印生产者信息

6、coutHProducer: Hid;生产 produceQ;释放 semaphoreReleaseSemaphore(Full, 1 ,NULL);ReleaseSemaphore(Mutex ,1 ,NULL); return 0;消费者入口函数UINT_stdcall ThreadCons(LPVOID)int id=+procjd;while(l)(初始化 srand(time(O);int i = rand() % 1000;Sleep(i);等待队列 WaitForSingleObject(FulIJNHNITE);等待互斥锁WaitForSingleObject(MutexJNF

7、INITE); coutHConsumer:,id;消费 consumeO;释放ReleaseSemaphore(Empty, 1 .NULL);ReleaseSemaphore(Mutex ,1 ,NULL);return 0:)void main()标识主进程UINT uld;int prod_numxons_num.nintime; cout请输入生产者个数和消费者个数:”; cinprod_numcons_num;cout请输入运行时间;cinnmtime;线程标识HANDLE *h_producer = new HANDLEprod_num;HANDLE *h_consumer =

8、new HANDLEcons_num;/初始化Empty = CreateSemaphore(NULL,M,M,NULL);/ 缓冲区满,无法生产Full = CreatcScmaphore(NULLQM,NULL); 缓冲区空,无法消费Mutex = CreateSemaphore(NULL, 1,1 ,NULL)y/ 线程互斥 /创建N个生产者int i;for(i=0;iprod_num;i+) h_produceri=(HANDLE):_beginthreadex(NULL.O,ThreadProd,NULUO,&uId);创建N个消费者 for(i=0;icons_num;i+) h

9、_consumcri=(HANDLE):_bcgimhrcadcx(NULL、O.ThrcadCons,NULLQ&uId);Sleep(runtime);/LINUX(Pthread) include include #include include include unistd #include #include using namespace std;#define M 10#define Random_Max 50 int buffM=0;int prodjd=0;semj Empty; semj Full;sem_t Mutex;void produce();void consume

10、O;void *producer(void *);void *consumer(void *);void produce() srand(time(O); int j = rand() % Random_Max; buffin=j; coutH produce Hj into bufferHendl; in = (in+l)%M;void consume()coutH consume ,buffout from bufTerHendl; out = (out+l)%M;void *producer(void *arg)int id=+prodjd: while(l)srand(time(O);

11、int i = rand() % 5;sleep(i);sem_wait(&Empty);sem_wait(&Mutcx);coutH Producer” v vid: produce();scm_post(&Full);scm_post(&Mutcx);coutHendHendl;return 0:void *consumer(void *arg)int id=+prodd;while(l)srand(time(O);int i = rand() % 5;sleep(i); sem_wait(&Fu!l);sem_wait(&Mutex); coutHConsumer Hid;consume

12、O;sem_post(&Emply);sem_post(& Mutex);return 0:int niain()int prod_num,cons_num,runtimc;coutHinput producer and consumer number:0; cinprod_numcons_nunKcoutHinput how long main sleep:; cinnmtime;pthreadj *h_producer = new pthreadjprod_num;pthreadj *h_consumer = new pthread_tcons_num; sem_init(&Empty.0

13、,M);semJnit(&FullAO);semjnit(&Mutex,0,1);int i;for(i=0;iprod_num;i+)pthread_create(&h_produccri, NULUproducer, NULL);for(i=0:icons_num;i+)pthread_create(&h_consumcri, NULLxonsumer. NULL);sleep( runtime);程序运行时的初值和运行结果消费者:10生产者:10运行时间:60000ms*C:UsersAdministratorDesktopi 作亲统信号建 Win32Debugsemaphoeexe”(

14、=4 回,- into buffoi*Cunsuiuer:8consume15f ronbufferProducer:4produce15intobufferConsumer:?consume15f ronbufferProducei: 1produce15intobufferConsumer: 6cun s uric15f ronbufferConsuner:5consume15f ronbufferProducer:3produce24intobufferConsumev:lconsume24Fvonhuf Fei*:-/VxtktopSOBSapro.cQo.cpppro cono* e

15、meu 23Ne 皿 opg% Bi/res xob 3 M 三条入匚匸QiiubuntvllubuntvU-viftiMl*Adchtne:-$ cd Desktop ubuntvliubuntvllvrtul nochtne;-/Desktops /po_cxo input producer nd consuner nuwber: 1016 input Ift run tg:6ee*4141414141Consumer 6 consume 41 fron bufferConsurwr 9 conste 1 froc buffer PrcdurlO pro4ve 1 int buffer C

16、onsunerConsunerPr(xlu*rl Producers Produer4 pr(xJuer5 ProducerG PrcducerTer2 prodveprxJye prodae prcd*j produce prodM ( ptfwZ.craiHl忙asmeflR Kticonsumif. mjuxj*wTir/R&:I5 9*1 心2 UQ(ri wrod.W:ward(tiiH:Hnii:QM|*Scpfsrucpp X3 圧 A: 11 T -: gikotMO GMU Cfliaa on*of tM GNAIMuk opinttag mm.2MS* i hnlWMous

17、e i on tKmovcw Htol weftCT To 5,机 rnrEEmrd. weCowo吃11coosvnefreebuffer12coosne41freebuffer13coAttno41freAbufferccxisw*1freebuffer17coflivne41freebuffer18consume4ttreebufferConsunerConsuner Produrl9 prodyo 41 Sto buffer (onsuntr u coasutw 1 r心 bufferpcdu20 pU irpeUS *CNU &nM Tpu 何-h tfi 0 他久 olubunt

18、ikcop1pro.coa.cpppro_cono* n cmfrOJubuntuU vtelu machkic Ne cdK opowTools rwtrp&rvKHa rmm “么 州Topu |知舛wggiTUojucLH.pjipcod 心.wg妙忙snamafld 血 uasum”. mxixa:*aiOmw*prod W: 如屮“火BH:gikor6iGXU Mm, cne covpwwnt of tM GNAUtuKOpirMafl sml to Wbir i linkWMxiie i g! VpcftHTo qiaceihgmr*l. we Co wo吃ProOucer4 or

19、educ 14 Into buffer 次ne12 von,”e M fron buffer Producerl9 produce 14 into buffer Co*&iner 14 consune 14 frow buffer Preuer 17 conuRe W from buffer PrcKerl produce 14 into buffer Co&iner id consume 14 fron buffer PreucarS preduca 14 IgS buffer C8$u、” io consuwe M froR buffer PrgC20 produce 14 Into bv

20、fr Cocuner IS consuwe 14 fron buffer Produce produce 14 Into buffer11 on$une 14 froR buffer rBvcer) produce 10 Wt。buffer rcducer4 produce 16 into buffer oauner 9 consune 16 froR buffer xs5or a16 fron buffercccXKerz produce 10 Into buffer oewner 11 consuwe 14 frow buffer PrcduceriO produce ,6 into buffer CoAMnar 12 con&uRa “ froR buffer roducen produce 16 into buffer ewncr 14 conuRc 14 fo“ buffer re

温馨提示

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

评论

0/150

提交评论