2.3 操作系統(tǒng) 李航 IPC_第1頁
2.3 操作系統(tǒng) 李航 IPC_第2頁
2.3 操作系統(tǒng) 李航 IPC_第3頁
2.3 操作系統(tǒng) 李航 IPC_第4頁
2.3 操作系統(tǒng) 李航 IPC_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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。值為正數(shù),0表示現(xiàn)在buffer為空若沒有cond_wait.那么producer發(fā)現(xiàn)buffer不為空,就得釋放鎖,然后又加鎖進入忙等待狀態(tài),直至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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論