




版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 武裝直升機(jī)購(gòu)買合同協(xié)議
- 商務(wù)用酒合同協(xié)議
- 正規(guī)地?cái)偨?jīng)濟(jì)合同協(xié)議
- 含稅銷售合同協(xié)議
- 商標(biāo)代理公司合同協(xié)議
- 商業(yè)秘密協(xié)議英文合同
- 商場(chǎng)店面轉(zhuǎn)兌合同協(xié)議
- 商場(chǎng)和餐飲商家合同協(xié)議
- 德國(guó)租車位合同協(xié)議
- 商業(yè)產(chǎn)品買賣合同協(xié)議
- 中華人民共和國(guó)民營(yíng)經(jīng)濟(jì)促進(jìn)法
- 2025-2030中國(guó)船用導(dǎo)航雷達(dá)行業(yè)市場(chǎng)發(fā)展分析及發(fā)展趨勢(shì)與投資前景研究報(bào)告
- 礦山探礦證轉(zhuǎn)讓合同協(xié)議
- 離散數(shù)學(xué)中的網(wǎng)絡(luò)科學(xué)研究-全面剖析
- 外包免責(zé)協(xié)議書模板
- 廣東省廣州市2025屆普通高中畢業(yè)班綜合測(cè)試(二)物理試題(含答案)
- 護(hù)士執(zhí)業(yè)資格考試資料2024
- 貴州省考試院2025年4月高三年級(jí)適應(yīng)性考試歷史試題及答案
- 五一節(jié)后復(fù)工復(fù)產(chǎn)培訓(xùn)
- 《休閑農(nóng)業(yè)》課件 項(xiàng)目六 休閑農(nóng)業(yè)經(jīng)營(yíng)管理
- T-CWEC 40-2023 防汛排澇抗旱一體化泵車
評(píng)論
0/150
提交評(píng)論