2.3 操作系统 李航 IPC_第1页
2.3 操作系统 李航 IPC_第2页
2.3 操作系统 李航 IPC_第3页
2.3 操作系统 李航 IPC_第4页
2.3 操作系统 李航 IPC_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

Process-IPC-part2李航funfind@Contents•Semaphore•Mutexes•Monitors•MessagePassing•BarriersSemaphore#defineN1OO/*numberofslotsinthebuffer*/typedefintsemaphore;semaphoremutex=1;semaphoreempty=N;、semaphorefull=O;voidproducer(void)intitem;while(TRUE)(item=produce—item();down(&empty);down(&mutex);insert—item(item);up(&mutex);up(&full);voidconsumer(void)intitem;/*semaphoresareaspecialkindofint*//*controlsaccesstocriticalregion*//*countsemptybufferslots*//*countsfullbufferslots*/、/*TRUEistheconstant1*/generatesomethingtoputinbuffer*/r^decrementemptycount*/</*e^tercriticalregion*//*purmewiteminbuffer*/>/*ieavebqticalregion*/ncremeoffullslots*/KeyIdea:仓库没有空槽位不能再生产,仓库没有产品不能再消费;向仓库中虹放置产品或取产品是原子操作’while(TRUE)(down(&full);down(&mutex);item=remove_item();up(&mutex);up(&empty);consume_item(item);}/*infiniteloop*//*decrementfullcount*//*entercriticalregion*//*takeitemfrombuffer*//*leavecriticalregion*//*incrementcountofemptyslots*./*dosomethingwiththeitem*/Mutex•SimplifiedSemaphore◦whereonlytwostatesexist:•lock&unlockThreadcallDescriptionPthreadmutexinitCreateamutexPthreadmutexdestroyDestroyanexistingmutexPthreadmutexlockAcquirealockorblockPthreadmutextrylockAcquirealockorfailPthreadmutexunlockReleasealockMutex•PossibleImplementationmutex_lock:TSLREGISTER,MUTEXCMPREGISTER,#。JZEokCALLthread-yieldJMPmutex_lockok:RETcopymutextoregisterandsetmutexto1wasmutexzero?ifitwaszero,mutexwasunlocked,soreturnmutexisbusy;scheduleanotherthreadtryagainIreturntocaller;criticalregionenteredmutex_unlock:MOVEMUTEX,#0RETstorea0inmutexreturntocallerMutex•ConditionalVariablesBuffer大小为1。值为正数,0表示现在buffer为空若没有cond_wait.那么producer发现buffer不为空,就得释放锁,然后又加锁进入忙等待状态,直至buffer为空。Mutex•What'sthedifferenceofmutex&semaphore?◦Onlythedifferenceindefinition?◦ImplementationDifferencesMonitor•Semaphoreproblem。easytodeadlock•Solution。Highlevelabstraction•Monitor:Amonitorisacollectionofprocedures,variables,anddatastructuresthatareallgroupedtogetherinaspecialkindofmoduleorpackage.Processesmaycalltheproceduresinamonitorwhenevertheywantto,buttheycannotdirectlyaccessthemonitor'sinternaldatastructuresfromproceduresdeclaredoutsidethemonitor.•ImportantFeature:Onlyoneprocesscanbeactiveinamonitoratanyinstant.Monitormonitorexampleintegerz;conditionc\procedureproducer^);end;procedureconsumer^);••・end;endmonitor;MutualExclusionwithBusywaitingmonitorProducerConsumercondition/mZ/,empty,integercount',procedureinsert(item:integer)-,beginifcount=Nthenyvait(full);insert_item(item);count:=count+1;ifcount=1thensignalfempty)end;functionremove',integer,beginifcount=0thenwait(e“?p邛丿;remove=removecount:=count

一1;ifcount=N-Ithensignal的〃)end;count:=0;endmonitor;procedureproducer,beginwhiletruedobeginitem=produceProducerConsumer.insert(item)endend;procedureconsumer,beginwhiletruedobeginitem=ProducerConsumer.remove;consume_item(item)endend;MonitorExample—PseudoPascalpublicclassProducerconsumer{staticfinalintN=100;//constantgivingthebuffersizestaticproducerp=newproducer();Hinstantiateanewproducerthreadstaticconsumerc=newconsumer();Hinstantiateanewconsumerthreadstaticourmonitormon=newourmonitor();//instantiateanewmonitorpublicstaticvoidmain(Stringargs[])(р.start();//starttheproducerthreadс.start();//starttheconsumerthreadstaticclassproducerextendsThread(publicvoidrun()(//runmethodcontainsthethreadcodeintitem;while(true)(〃

producerloopitem=produceitem();mon.insert(item);privateintproduceitem()(...}//actuallyproducestaticclassconsumerextendsThread(publicvoidrun()(runmethodcontainsthethreadcodeintitem;while(true){//consumerloop|item=mon.remove();consumeitem(item);privatevoidconsumeitem(intitem)(...}//actuallyconsumestaticclassourmonitor(Hthisisamonitorprivateintbuffer[]=newint[N];privateintcount=0,Io=0,hi=0;〃

countersandindicespublicsynchronizedvoidinsert(intval)(if(count==N)gotosleep();//ifthebufferisfull,gotosleepbuffer[hi]=val;Hinsertanitemintothebufferhi=(hi+1)%N;//slottoplacenextitemincount=count+1;//onemoreiteminthebuffernowif(count=1)notify();//ifconsumerwassleeping,wakeituppublicsynchronizedintremove(){intval;if(count=0)gotosleep();IIifthebufferisempty,gotosleepval=buffer[Io];II

fetchanitemfromthebufferlo=(lo+1)%N;II

slottofetchnextitemfromcount=count-1;II

onefewitemsinthebufferif(count=N-1)notify();//ifproducerwassleeping,wakeitupreturnval;privatevoidgotosleep()(try{wait();}catch(InterruptedExceptionexc){);MonitorJavaExampleMessagePassing#defineN100voidproducer(void){intitem;messagem;while(TRUE)(item=produce_item();receive(consumer,&m);build_message(&m,item);send(consumer,&m);}}voidconsumer(void){intitem,i;messagem;for(i=0;ivN;i++)send(producer,while(TRUE)(receive(producer,&m);item=extract_item(&

温馨提示

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

评论

0/150

提交评论