版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
進(jìn)程同進(jìn)程同步的基本概 2.3進(jìn)程同
VARtop:integer=-1stack:array[0..n-1]ofProgramAwhile(TRUEtop:=(top+1)mod stack[top]:=x endProgramBwhile(TRUEtop:=(top+1)mod stack[top]:=y endwhile 2.3進(jìn)程同
VARtop:integer=-1ProgramAwhileProgramAwhile(TRUEtop:=(top+1)modstack[top]:=xend 如何實(shí)現(xiàn)臨界資源的互
ProgramBwhile(TRUEtop:=(top+1)modstack[top]:=yendwhile201arend2.3進(jìn)程同 臨界資 互臨界 互
VARtop:integer=-1stack:array[0..n-1]ofitem;ProgramA:while(TRUEtoptop:=(top+1)modn;stack[top]:=x;endwhileProgramB:while(TRUEtoptop:=(top+1)modn;stack[top]:=y;
end201arend2.3進(jìn)程同criticalsectioncriticalsectionremaindersectionuntil
entrycriticalsection;exitsection;remaindersection;untilfalse空閑讓進(jìn):當(dāng)無進(jìn)程處于臨界區(qū)忙則等待:當(dāng)已有進(jìn)程進(jìn)入臨界有限等待:對(duì)要 臨界資源進(jìn)程,保證能在有限時(shí)間內(nèi)進(jìn)入臨界讓權(quán)等待:當(dāng)進(jìn)程不能進(jìn)入臨界區(qū)時(shí),應(yīng)釋放處理機(jī) 2.3進(jìn)程同
VARfree:boolean:=false實(shí)現(xiàn)進(jìn)程同步的早期軟件解法1資源空閑標(biāo)志:free=false:Free=true:
Programwhile(while(free)free:=truefree:=falsefree:=falseuntilProgramwhile(while(free)free:=truefree:=falsefree:=false
remaindersectionuntilfalse82.3進(jìn)程同
VARturn:integer:=12.3.2實(shí)現(xiàn)進(jìn)程同步的早期
Programwhile(while(turn=2)turn:=2turn:=2remaindersectionuntil嚴(yán)格限制資 順
Programwhile(while(turn=1)turn:=1turn:=1
remaindersectionuntilfalse92.3進(jìn)程同
VARpturn,qturn:boolean:=false,false2.3.2實(shí)現(xiàn)進(jìn)程同步的早期方Programpturn:=truepturn:=truewhile(qturn)
criticalsection;remaindersection;pturn:=falsepturn:=falseProgramqturnqturn:=truewhile(pturn)criticalsectionqturnqturn:=falseremaindersection;untilfalse
12.3進(jìn)程同2.3.2實(shí)現(xiàn)進(jìn)程同步的早期方#defineFALSE#define
Programenter_region(0enter_region(0criticalsection intint
進(jìn)程的個(gè) 數(shù)組,初始值均為
remaindersectionuntilvoidenter_regionintprocessprocess0或{int //另外一個(gè)進(jìn)程的進(jìn)程other=1- interested[process]=TRUE表明本進(jìn)程turn= 設(shè)置標(biāo)志while(turn==process&&interested[other]==}voidleave_region(int{interested[process]= //本進(jìn)程已離開
Programenter_region(1);criticalsection;remaindersection;untilfalseVARlock:boolean:=VARlock:boolean:=falseProgramcriticalsectionremaindersectionuntilfalseProgramcriticalsection;remaindersection;untilenter_region(lock2.3.2實(shí)現(xiàn)進(jìn)程同步的早期方//交換鎖lock//交換鎖lock和key的functionSWAP(lock,key){vartmp:boolean:=lock;lock:=key;key:=tmp}functionfunctionenter_region(varlockbooleanVarkey:boolean;//局部變量key:=functionleave_region(varlock:booleanlock:=false;進(jìn)程同實(shí)現(xiàn)進(jìn)程同步的早期方 信號(hào)量機(jī)制的提出荷蘭計(jì)算機(jī)科學(xué)家EdsgerWybeDijkstra; 第一個(gè)Algol60編譯器的設(shè)計(jì)者 VARS:integer:=1VARS:integer:=1;ProgramP1:criticalsectionremaindersection;untilfalseProgramcriticalsectionremaindersectionuntil信號(hào)量機(jī)整數(shù)信號(hào)量S; P/VfunctionfunctionWait(varS:integerwhile(S<=0)dono_op();S:=S-1;functionSignal(varS:integer)S:=VARS:semaphore:=1;ProgramP1:VARS:semaphore:=1;ProgramP1:criticalsection;remaindersection;untilfalseProgramcriticalsection;remaindersection;untilwait(S兩個(gè)原子操作 P/Vtypesemaphore=value:integerL:listofprocess
//阻塞進(jìn)程隊(duì)functionwait(varS:Semaphore)S.value:=S.value-if(S.value<0)thenblock(S.L);functionsignal(varS:SemaphoreS.value:=if(S.value<=0)thenwakeup(S.L);2.3進(jìn)程同(2)資源,因此描述為S.value:=S.value-1;當(dāng)S.value<0時(shí),表源,故S.value:=s.value+1操作表示資源數(shù)目加1。若加1后仍S.value:S.value>=0:資源數(shù)量;S.value<0: |S.value|合肥工業(yè)大學(xué)操作系統(tǒng)課程組 2.3進(jìn)程同信號(hào)量的應(yīng)例1:2
VARtop:integer=-1stack:array[0..n-1]ofmutex:semaphore:=1ProcessProcessP1while(TRUEtop:=(top+1)modstack[top]:=xendsignal(mutex)wait(mutex)ProcessProcessP2while(TRUEtop:=(top+1)modn;stack[top]:=y;endwhilesignal(mutex)wait(mutex)
2.3進(jìn)程同(1)河河河河 ProcessN2SProcessN2Sgoacrossthebridgeuntilsignal(mutex)wait(mutex)ProcessS2Ngoacrossthebridge;untilfalsesignal(mutex)wait(mutex)VARmutex:semaphore=1ProcessN2Sgoacrossthebridge;untilfalseProcessS2Ngoacrossthebridge 2.3進(jìn)程同(2)B VARS1,S2:VARS1,S2:semaphore=1,0ProcessPIreadxfromI/OB:=xuntilProcessPOy:=Bprint(y);untilfalsesignal(S1)wait(S2)signal(S2)wait(S1)ProcessPOy:=Bprint(y)untilProcessPIreadxfromI/O;B:=x;until組組 進(jìn)程同(2)B3:IEBI負(fù)責(zé)產(chǎn)生隨機(jī)數(shù)B中,當(dāng)B中數(shù)據(jù)為奇數(shù)時(shí)由O負(fù)責(zé)打印,當(dāng)B中數(shù)B ProcessProcessPEy:=Bprint(y);untilfalseProcessPOz:=Bprint(z);untilfalseProcessPIgeneratexB:=xuntilvarS,SO,SE:semaphoreProcessPEy:=B;signal(S);print(y);ProcessPOz:=Bsignal(S);print(z);untilProcessPIgeneratexB:=xif(xisodd)signal(SO); signal(SE)until組組 經(jīng)典進(jìn)程同步問生產(chǎn)者-消費(fèi)者問循環(huán)緩沖
出隊(duì)操 VARempty,full:semaphore:=n,0;in,out:integer:=0,0;Buffer:array[0..n-1]ofitemproduceaniteminnextp;wait(empty);Buffer(in):=nextp;in:=(in+1)modn;signal(full)untilwait(full)nextc=out:=(out+1)modn課程組課程組
signal(empty)consumetheitemnextc;untilflasein,out:integerin,out:integer:=0,0Buffer:array[0..n-1]ofitem produceaniteminnextpBuffer(in):=nextp;in:=(in+1)modn;untilnextc=out:=(out+1)modnconsumetheitemnextc;untilflase
VARmutex,VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0Buffer:array[0..n-1]ofitem;produceaniteminnextp;wait(empty);wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilwait(full);wait(mutex);nextc=out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextc;untilflase (2)
合肥工業(yè)大學(xué)操作
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextcuntilflase(2)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex)nextc=out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextcuntil合肥工業(yè)大學(xué)操作
(2)
VARmutex,empty,full:semaphore:=1,n,0;in,out:integer:=0,0;Buffer:array[0..n-1]ofitemproduceaniteminnextp;wait(empty);Buffer(in):=nextp;in:=(in+1)modn;
signal(full)untilwait(full)nextc=out:=(out+1)modn合肥工業(yè)大學(xué)操作系統(tǒng)
signal(empty);consumetheitemnextc;程組 程組 (2)
同例VARempty,full:VARempty,full:semaphore:=1,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextp;wait(empty);Buffer(in):=nextp;in:=(in+1)modn;signal(full);untilwait(full)nextc=out:=(out+1)modn;signal(empty);consumetheitemnextc;until(2)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextc系統(tǒng)課程組 系統(tǒng)課程組 合肥工業(yè)大學(xué)操
(2)
VARmutexC,mutexP,empty,full:semaphore:=1,1,n,0;in,out:integer:=0,0;Buffer:array[0..n-1]ofitemproduceaniteminnextp;wait(empty);wait(mutexP);Buffer(in):=nextp;in:=(in+1)modn;signal(mutexP);signal(full);untilwait(full)wait(mutexC);nextc=Buffer(out);out:=(out+1)modnsignal(mutexC);signal(empty);consumetheitemnextc;操作系統(tǒng)課程組 操作系統(tǒng)課程組 合肥工業(yè)大
(2)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextc作系統(tǒng)課程組 作系統(tǒng)課程組 合肥工業(yè)大學(xué)
(2)
合肥工業(yè)大學(xué)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex)consumetheitemnextc;untilflase作系統(tǒng)課程組作系統(tǒng)課程組 (2)
合肥工業(yè)大學(xué)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(mutex)wait(empty);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextcuntil作系統(tǒng)課程組作系統(tǒng)課程組 (2)
合肥工業(yè)大學(xué)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(mutex)wait(full)nextc=out:=(out+1)modnsignal(mutex)signal(empty);consumetheitemnextc;until作系統(tǒng)課程組作系統(tǒng)課程組 (2)
VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(mutex)wait(empty);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(mutex)wait(full)nextc=out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextcuntil合肥工業(yè)大學(xué)4操作系統(tǒng)end組(2)VARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty);wait(mutex)Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(empty);signal(mutex)consumetheitemnextcuntil合肥工業(yè)大學(xué)4操作系統(tǒng)end組(2)
VARVARmutex,empty,full:semaphore:=1,n,0in,out:integer:=0,0;Buffer:array[0..n-1]ofitem;produceaniteminnextpwait(empty)wait(mutex);Buffer(in):=nextp;in:=(in+1)modn;signal(mutex);signal(full);untilflasewait(full)wait(mutex);nextc=Buffer(out);out:=(out+1)modnsignal(mutex);signal(empty)consumetheitemnextcuntil 合肥工業(yè)大學(xué)2.4經(jīng)典進(jìn)程同步問讀者-寫者問
讀寫讀寫 VARVARwmutex:semaphore:=1wait(wmutex);performreadoperation;signal(wmutex);untilflasewait(wmutex);performwriteoperation;signal(wmutex);untilflase VARVARwmutex:semaphore:=1readcount:integer:=0if(readcount=0)wait(wmutex);readcount:=readcount+1;performreadoperation;readcount:=readcount-1if(readcount=0)signal(wmutex);untilflasewait(wmutex);performwriteoperation;signal(wmutex);until
VARVARwmutex:semaphore:=1;readcount:integer:=0;if(readcount=0)wait(wmutex);readcount:=readcount+1;performreadoperation;readcount:=readcount-1if(readcount=0)signal(wmutex);untilflasewait(wmutex);performwriteoperation;signal(wmutex);untilVARrmutex,wmutex:semaphore:=1,1;readcount:integer:=0;wait(rmutex)VARrmutex,wmutex:semaphore:=1,1;readcount:integer:=0;wait(rmutex)if(readcount=0)wait(wmutex);readcount:=readcount+1;signal(rmutex);performreadoperationwait(rmutex)readcount:=readcount-1if(readcount=0)signal(wmutex)signal(rmutex)untilwait(wmutex);performwriteoperation;signal(wmutex);untilflase VARrmutex,wmutex:VARrmutex,wmutex:semaphore:=1,1readcount:integer:=0;wait(rmutex)if(readcount=0)wait(wmutex);readcount:=readcount+1;signal(rmutex);performreadoperationwait(rmutex)readcount:=readcount-1if(readcount=0)signal(wmutex)signal(rmutex)untilwait(wmutex);performwriteoperation;signal(wmutex);untilflase 2.4經(jīng)典進(jìn)程同步問哲學(xué)家進(jìn)餐問 哲學(xué)家進(jìn)程:互斥(筷子 VARVARchopsticks:array[0..4]ofsemaphore:=1,1,1,1,1wait(chopsticks[i])wait(chopsticks[(i+1)mod5]eatsignal(chopsticks[i])signal(chopsticks[(i+1)mod5]untilflase 0 2
VARVARchopsticks:array[0..4]ofsemaphore:=1,1,1,1,1philosopher0-wait(chopsticks[i])wait(chopsticks[(i+1)mod5]);eat;signal(chopsticks[i])signal(chopsticks[(i+1)mod5]untilflasewait(chopsticks[(i+1)mod5]wait(chopsticks[i]);eat;signal(chopsticks[i])signal(chopsticks[(i+1)mod5]untilflase4 0 2 經(jīng)典進(jìn)程同步問理發(fā)師問題(課后思考題(1) 2.4經(jīng)典進(jìn)程同步問信號(hào)量集機(jī) 2.4.5信號(hào)量集機(jī)typesemaphore=value:integerL:listofprocess;
//阻塞進(jìn)程隊(duì)functionSwait(varS1,S2,..,Sn:Semaphoreif(S1>=1and…andSn>=1)fori:=1tondoSi:=Si-1PlacetheprocessinthequeueassociatedwiththefirstSifoundwithSi<1,gotothebeginningoffunctionSsignal(varS1,S2,..,Sn:Semaphorefo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 員工勞動(dòng)合同協(xié)議書格式
- 廠房租賃合同范本版(18篇)
- 農(nóng)業(yè)機(jī)械購買補(bǔ)貼合同
- 技術(shù)開發(fā)服務(wù)合同案例
- 企業(yè)培訓(xùn)就業(yè)協(xié)議書編寫技巧
- 2第二章-血液一般檢驗(yàn)-02-血栓與止血、血型
- 室內(nèi)清潔合作合同格式
- 員工個(gè)人合同書范本
- 投資合作協(xié)議范本:2024投資合作協(xié)議范本
- 場地游戲安全協(xié)議書
- 軟件項(xiàng)目提成方案
- 裝配式建筑設(shè)計(jì)研究與總結(jié)課件
- 一年級(jí)上冊(cè)全冊(cè)道德與法治教案全
- 中班健康《身體上的洞洞》課件
- GB/T 9452-2023熱處理爐有效加熱區(qū)測定方法
- 停車場施工方案及技術(shù)措施范本
- 高考地理一輪復(fù)習(xí)課件【知識(shí)精講+高效課堂】美食與地理環(huán)境關(guān)系
- 分居聲明告知書范本
- 2023年04月山東濟(jì)南市槐蔭區(qū)殘聯(lián)公開招聘殘疾人工作“一專兩員”公開招聘筆試參考題庫+答案解析
- 消失的13級(jí)臺(tái)階
- 營銷管理知識(shí)點(diǎn)
評(píng)論
0/150
提交評(píng)論