版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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:倉(cāng)庫(kù)沒有空槽位不能再生產(chǎn),倉(cāng)庫(kù)沒有產(chǎn)品不能再消費(fèi);向倉(cāng)庫(kù)中虹放置產(chǎn)品或取產(chǎn)品是原子操作’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不為空,就得釋放鎖,然后又加鎖進(jìn)入忙等待狀態(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 新生家長(zhǎng)會(huì)發(fā)言稿
- 托班學(xué)期工作計(jì)劃范文匯編九篇
- 推門聽課項(xiàng)目方案范文(6篇)
- 瑜伽系統(tǒng)提升課程設(shè)計(jì)
- 消防安全小班課程設(shè)計(jì)
- 2025年山東濱州醫(yī)學(xué)院公開招聘工作人員24人管理單位筆試遴選500模擬題附帶答案詳解
- 2025年山東淄博張店區(qū)招聘首批城鄉(xiāng)公益性崗位人員700人歷年管理單位筆試遴選500模擬題附帶答案詳解
- 2025年山東淄博臨淄區(qū)事業(yè)單位招聘工作人員75人歷年管理單位筆試遴選500模擬題附帶答案詳解
- 2025年山東濟(jì)南平陰縣教體事業(yè)單位招聘110人歷年管理單位筆試遴選500模擬題附帶答案詳解
- 2024年海鮮運(yùn)輸保溫協(xié)議-確保鮮活產(chǎn)品品質(zhì)
- (正式版)JBT 14587-2024 膠體鉛酸蓄電池 技術(shù)規(guī)范
- 小學(xué)生作文方格紙
- 小區(qū)內(nèi)命案防控應(yīng)急預(yù)案
- 2024年內(nèi)蒙古交通集團(tuán)興安分公司招聘筆試參考題庫(kù)附帶答案詳解
- 臨電施工方案與施工組織設(shè)計(jì)
- “牢固樹立法紀(jì)意識(shí),強(qiáng)化責(zé)任擔(dān)當(dāng)”心得體會(huì)模板(3篇)
- (2024年)質(zhì)量管理體系
- 大學(xué)生職業(yè)生涯規(guī)劃大賽醫(yī)學(xué)檢驗(yàn)技術(shù)專業(yè)成長(zhǎng)賽道
- 高中數(shù)學(xué)家長(zhǎng)會(huì)課件:夯實(shí)數(shù)學(xué)基礎(chǔ)培養(yǎng)數(shù)學(xué)思維
- 四川省成都市天府新區(qū)2023-2024學(xué)年七年級(jí)上學(xué)期期末數(shù)學(xué)試題(含答案)
- 青浦朱家角古鎮(zhèn)分析報(bào)告
評(píng)論
0/150
提交評(píng)論