版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
進程同進程同步的基本概 2.3進程同
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進程同
VARtop:integer=-1ProgramAwhileProgramAwhile(TRUEtop:=(top+1)modstack[top]:=xend 如何實現(xiàn)臨界資源的互
ProgramBwhile(TRUEtop:=(top+1)modstack[top]:=yendwhile201arend2.3進程同 臨界資 互臨界 互
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進程同criticalsectioncriticalsectionremaindersectionuntil
entrycriticalsection;exitsection;remaindersection;untilfalse空閑讓進:當無進程處于臨界區(qū)忙則等待:當已有進程進入臨界有限等待:對要 臨界資源進程,保證能在有限時間內(nèi)進入臨界讓權(quán)等待:當進程不能進入臨界區(qū)時,應釋放處理機 2.3進程同
VARfree:boolean:=false實現(xiàn)進程同步的早期軟件解法1資源空閑標志:free=false:Free=true:
Programwhile(while(free)free:=truefree:=falsefree:=falseuntilProgramwhile(while(free)free:=truefree:=falsefree:=false
remaindersectionuntilfalse82.3進程同
VARturn:integer:=12.3.2實現(xiàn)進程同步的早期
Programwhile(while(turn=2)turn:=2turn:=2remaindersectionuntil嚴格限制資 順
Programwhile(while(turn=1)turn:=1turn:=1
remaindersectionuntilfalse92.3進程同
VARpturn,qturn:boolean:=false,false2.3.2實現(xiàn)進程同步的早期方Programpturn:=truepturn:=truewhile(qturn)
criticalsection;remaindersection;pturn:=falsepturn:=falseProgramqturnqturn:=truewhile(pturn)criticalsectionqturnqturn:=falseremaindersection;untilfalse
12.3進程同2.3.2實現(xiàn)進程同步的早期方#defineFALSE#define
Programenter_region(0enter_region(0criticalsection intint
進程的個 數(shù)組,初始值均為
remaindersectionuntilvoidenter_regionintprocessprocess0或{int //另外一個進程的進程other=1- interested[process]=TRUE表明本進程turn= 設置標志while(turn==process&&interested[other]==}voidleave_region(int{interested[process]= //本進程已離開
Programenter_region(1);criticalsection;remaindersection;untilfalseVARlock:boolean:=VARlock:boolean:=falseProgramcriticalsectionremaindersectionuntilfalseProgramcriticalsection;remaindersection;untilenter_region(lock2.3.2實現(xiàn)進程同步的早期方//交換鎖lock//交換鎖lock和key的functionSWAP(lock,key){vartmp:boolean:=lock;lock:=key;key:=tmp}functionfunctionenter_region(varlockbooleanVarkey:boolean;//局部變量key:=functionleave_region(varlock:booleanlock:=false;進程同實現(xiàn)進程同步的早期方 信號量機制的提出荷蘭計算機科學家EdsgerWybeDijkstra; 第一個Algol60編譯器的設計者 VARS:integer:=1VARS:integer:=1;ProgramP1:criticalsectionremaindersection;untilfalseProgramcriticalsectionremaindersectionuntil信號量機整數(shù)信號量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兩個原子操作 P/Vtypesemaphore=value:integerL:listofprocess
//阻塞進程隊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進程同(2)資源,因此描述為S.value:=S.value-1;當S.value<0時,表源,故S.value:=s.value+1操作表示資源數(shù)目加1。若加1后仍S.value:S.value>=0:資源數(shù)量;S.value<0: |S.value|合肥工業(yè)大學操作系統(tǒng)課程組 2.3進程同信號量的應例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進程同(1)河河河河 ProcessN2SProcessN2Sgoacrossthebridgeuntilsignal(mutex)wait(mutex)ProcessS2Ngoacrossthebridge;untilfalsesignal(mutex)wait(mutex)VARmutex:semaphore=1ProcessN2Sgoacrossthebridge;untilfalseProcessS2Ngoacrossthebridge 2.3進程同(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組組 進程同(2)B3:IEBI負責產(chǎn)生隨機數(shù)B中,當B中數(shù)據(jù)為奇數(shù)時由O負責打印,當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)典進程同步問生產(chǎn)者-消費者問循環(huán)緩沖
出隊操 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è)大學操作
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è)大學操作
(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è)大學操作系統(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è)大學操
(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è)大學
(2)
合肥工業(yè)大學
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è)大學
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è)大學
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è)大學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è)大學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è)大學2.4經(jīng)典進程同步問讀者-寫者問
讀寫讀寫 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)典進程同步問哲學家進餐問 哲學家進程:互斥(筷子 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)典進程同步問理發(fā)師問題(課后思考題(1) 2.4經(jīng)典進程同步問信號量集機 2.4.5信號量集機typesemaphore=value:integerL:listofprocess;
//阻塞進程隊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等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年江蘇省中等職業(yè)學校學生學業(yè)水平考試機械CAD繪圖試卷(含5張圖)
- 2024屆襄樊市重點中學高三下學期第一次診斷性考試數(shù)學試題試卷
- 蝴蝶舞會小班課件
- 輪胎行業(yè)可行性報告
- 2024年蚌埠大客車從業(yè)資格證考試
- 2024年客運資格證考試實際操作試題答案
- 2024年安徽駕駛員客運從業(yè)資格證模擬考試題答案
- 2024年湖南駕駛員客運從業(yè)資格證考試題庫
- 2024年呼倫貝爾道路客運輸從業(yè)資格證培訓資料
- 2025屆湖南省湘西土家族苗族自治州高三生物第一學期期末達標檢測模擬試題含解析
- 函數(shù)的單調(diào)性與最值-完整課件
- s和m關(guān)系協(xié)議書
- 空分裝備安全檢查表
- 安全風險隱患排查表國家隱患排查導則版
- 停車場施工方案及技術(shù)措施范本
- 分居聲明告知書范本
- 【基于杜邦分析的企業(yè)財務現(xiàn)狀探究國內(nèi)外文獻綜述5200字】
- 工程圖學(天津大學)智慧樹知到答案章節(jié)測試2023年
- 江蘇省民用建筑施工圖綠色設計專篇參考樣式2021年版
- GB/T 17853-2018不銹鋼藥芯焊絲
- 初中生離父母有多遠
評論
0/150
提交評論