




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、銀行家算法法 銀行家家算法是一一種最有代代表性的避避免死鎖的的算法。 要解釋釋銀行家算算法,必須須先解釋操操作系統(tǒng)安安全狀態(tài)和和不安全狀狀態(tài)。 安全狀狀態(tài):如果果存在一個(gè)個(gè)由系統(tǒng)中中所有進(jìn)程程構(gòu)成的安安全序列PP1,PPn,則系系統(tǒng)處于安安全狀態(tài)。安全狀態(tài)態(tài)一定是沒沒有死鎖發(fā)發(fā)生。 不安全全狀態(tài):不不存在一個(gè)個(gè)安全序列列。不安全全狀態(tài)不一一定導(dǎo)致死死鎖。 那么什什么是安全全序列呢? 安全序序列:一個(gè)個(gè)進(jìn)程序列列P1,Pn是安全的的,如果對(duì)對(duì)于每一個(gè)個(gè)進(jìn)程Pii(1iin),它它以后尚需需要的資源源量不超過過系統(tǒng)當(dāng)前前剩余資源源量與所有有進(jìn)程Pjj (j i )當(dāng)前占占有資源量量之和。 銀行家家
2、算法: 我們可可以把操作作系統(tǒng)看作作是銀行家家,操作系系統(tǒng)管理的的資源相當(dāng)當(dāng)于銀行家家管理的資資金,進(jìn)程程向操作系系統(tǒng)請(qǐng)求分分配資源相相當(dāng)于用戶戶向銀行家家貸款。操操作系統(tǒng)按按照銀行家家制定的規(guī)規(guī)則為進(jìn)程程分配資源源,當(dāng)進(jìn)程程首次申請(qǐng)請(qǐng)資源時(shí),要要測(cè)試該進(jìn)進(jìn)程對(duì)資源源的最大需需求量,如如果系統(tǒng)現(xiàn)現(xiàn)存的資源源可以滿足足它的最大大需求量則則按當(dāng)前的的申請(qǐng)量分分配資源,否否則就推遲遲分配。當(dāng)當(dāng)進(jìn)程在執(zhí)執(zhí)行中繼續(xù)續(xù)申請(qǐng)資源源時(shí),先測(cè)測(cè)試該進(jìn)程程已占用的的資源數(shù)與與本次申請(qǐng)請(qǐng)的資源數(shù)數(shù)之和是否否超過了該該進(jìn)程對(duì)資資源的最大大需求量。若超過則則拒絕分配配資源,若若沒有超過過則再測(cè)試試系統(tǒng)現(xiàn)存存的資源能能否
3、滿足該該進(jìn)程尚需需的最大資資源量,若若能滿足則則按當(dāng)前的的申請(qǐng)量分分配資源,否否則也要推推遲分配。 算法: n:系系統(tǒng)中進(jìn)程程的總數(shù) m:資資源類總數(shù)數(shù) Avaailabble: ARRAAY1.m of iintegger; Maxx: ARRRAY1.nn,1.m oof inntegeer; Alllocattion: ARRRAY11.n,1.mm off inttegerr; Neeed: AARRAYY1.n,1.m of iintegger; Reqquestt: ARRRAY1.nn,1.m oof inntegeer; 符號(hào)說說明: Avaailabble 可可用剩余資資源
4、Maxx 最大需需求 Alllocattion 已分配資資源 Neeed 需求求資源 Reqquestt 請(qǐng)求資資源 當(dāng)進(jìn)程程pi提出出資源申請(qǐng)請(qǐng)時(shí),系統(tǒng)統(tǒng)執(zhí)行下列列 步驟:(“=”為賦值符符號(hào),“=”為等等號(hào)) steep(1)若若Requuest=Neeed, ggoto stepp(2);否則錯(cuò)誤誤返回 steep(2)若若Requuest=Avaailabble, gotoo steep(3);否則進(jìn)程程等待 steep(3)假假設(shè)系統(tǒng)分分配了資源源,則有: Avaailabble=AAvaillablee-Reqquestt; Alllocattion=Alloocatiion+RR
5、equeest; Neeed=Neeed-RRequeest 若系統(tǒng)統(tǒng)新狀態(tài)是是安全的,則則分配完成成 若系統(tǒng)統(tǒng)新狀態(tài)是是不安全的的,則恢復(fù)復(fù)原狀態(tài),進(jìn)進(jìn)程等待 為進(jìn)行行安全性檢檢查,定義義數(shù)據(jù)結(jié)構(gòu)構(gòu): Worrk:ARRRAY1.mm off inttegerr; Finnish:ARRAAY1.n of BBooleean; 安全性性檢查的步步驟: steep (11): Worrk=Avvailaable; Finnish=falsse; steep (22) 尋找找滿足條件件的i: a.FFinissh=ffalsee; b.NNeed=Worrk; 如果不不存在,ggoto step
6、p(4) steep(3) Worrk=Woork+AAlloccatioon; Finnish=truee; gotto sttep(22) steep (44) 若對(duì)對(duì)所有i,Finiish=ttrue,則系統(tǒng)處處于安全狀狀態(tài),否則則處于不安安全狀態(tài) /* 銀行家算算法,操作作系統(tǒng)概念念(OS concceptss Sixx Ediitionn) reeedit by JJohnnny haagen,SSCAU,rrun aat vcc6.0 */ #inncludde mmallooc.h #inncludde sstdioo.h #inncludde sstdliib.h #deefi
7、nee alllocleen siizeoff(strruct alloocatiion) #deefinee maxxlen sizeeof(sstrucct maax) #deefinee avaalen sizeeof(sstrucct avvailaable) #deefinee neeedlenn sizzeof(struuct nneed) #deefinee finnilenn sizzeof(struuct ffinissh) #deefinee patthlenn sizzeof(struuct ppath) strruct alloocatiion intt vallue;
8、 strruct alloocatiion *nextt; ; strruct max intt vallue; strruct max *nexxt; ; strruct avaiilablle /*可用資源源數(shù)*/ intt vallue; strruct avaiilablle *nnext; ; strruct needd /*需需求資源數(shù)數(shù)*/ intt vallue; strruct needd *neext; ; strruct pathh intt vallue; strruct pathh *neext; ; strruct finiish intt staat; strru
9、ct finiish *nextt; ; intt maiin() intt roww,collum,sstatuus=0,i,j,t,teemp,pproceessteest; strruct alloocatiion *alloocheaad,*aallocc1,*aallocc2,*aallocctempp; strruct max *maxxheadd,*maaxiumm1,*mmaxiuum2,*maxttemp; strruct avaiilablle *aavaheead,*avaiilablle1,*avaiilablle2,*workkheadd,*woork1,*worrk
10、2,*workktempp,*woorkteemp1; strruct needd *neeedheead,*needd1,*nneed22,*neeedteemp; strruct finiish *finiiheadd,*fiinishh1,*ffinissh2,*finiishteemp; strruct pathh *paathheead,*pathh1,*ppath22; priintf(n請(qǐng)請(qǐng)輸入系統(tǒng)統(tǒng)資源的種種類數(shù):); scaanf(%d,&collum); priintf(請(qǐng)輸入入現(xiàn)時(shí)內(nèi)存存中的進(jìn)程程數(shù):); scaanf(%d,&roww); priintf(請(qǐng)輸入入已分配
11、資資源矩陣:n); forr(i=00;irrow;ii+) forr (j=0;jnexxt=allloc22-neext=NNULL; scaanf(%d,&alllocheead-valuue); staatus+; elsse allloc2=(strruct alloocatiion *)mallloc(allooclenn); scaanf(%d,%d,&allooc2-valuue); if(stattus=1) alllocheead-nextt=allloc2; staatus+; allloc1-nexxt=allloc22; allloc1=allooc2; allloc
12、2-nexxt=NUULL; staatus=0; priintf(請(qǐng)輸入入最大需求求矩陣:n); forr(i=00;irrow;ii+) forr (j=0;jneext=mmaxiuum2-nextt=NULLL; scaanf(%d,&maxxium11-vaalue); staatus+; elsse maxxium22=(sttructt maxx *)mmallooc(maaxlenn); scaanf(%d,%d,&maxiium2-vallue); if(stattus=1) maxxheadd-neext=mmaxiuum2; staatus+; maxxium11-nee
13、xt=mmaxiuum2; maxxium11=maxxium22; maxxium22-neext=NNULL; staatus=0; priintf(請(qǐng)輸入入現(xiàn)時(shí)系統(tǒng)統(tǒng)剩余的資資源矩陣:n); forr (j=0;jnexxt=avvailaable22-neext=NNULL; worrk1-nextt=worrk2-nextt=NULLL; scaanf(%d,&avaailabble1-vallue); worrk1-valuue=avvailaable11-vaalue; staatus+; elsse avaailabble2=(strruct avaiilablle*)mmal
14、looc(avvalenn); worrk2=(struuct aavaillablee*)maallocc(avaalen); scaanf(%d,%d,&avaiilablle2-valuue); worrk2-valuue=avvailaable22-vaalue; if(stattus=1) avaaheadd-neext=aavaillablee2; worrkheaad-nnext=workk2; staatus+; avaailabble1-nexxt=avvailaable22; avaailabble1=avaiilablle2; worrk1-nextt=worrk2; w
15、orrk1=wwork22; avaailabble2-nexxt=NUULL; worrk2-nextt=NULLL; staatus=0; alllocteemp=aalloccheadd; maxxtempp=maxxheadd; forr(i=00;irrow;ii+) forr (j=0;jnextt=neeed2-nextt=NULLL; neeed1-valuue=maaxtemmp-vvaluee-alllocteemp-valuue; staatus+; elsse neeed2=(struuct nneed *)maallocc(neeedlenn); neeed2-val
16、uue=(mmaxteemp-valuue)-(allooctemmp-vvaluee); if(stattus=1) neeedheaad-nnext=needd2; staatus+; neeed1-nextt=neeed2; neeed1=nneed22; maxxtempp=maxxtempp-neext; alllocteemp=aallocctempp-neext; neeed2-nextt=NULLL; staatus=0; forr(i=00;ineext=ffinissh2-nextt=NULLL; finnish11-sttat=00; staatus+; elsse fi
17、nnish22=(sttructt finnish*)mallloc(finiilen); finnish22-sttat=00; if(stattus=1) finniheaad-nnext=finiish2; staatus+; finnish11-neext=ffinissh2; finnish11=finnish22; finnish22-neext=NNULL; /*IInitiializzatioon coompleeatedd*/ staatus=0; proocessstestt=0; forr(temmp=0;tempproww;temmp+) alllocteemp=aal
18、loccheadd; neeedtemmp=neeedheead; finnishttemp=finiiheadd; worrktemmp=woorkheead; forr(i=00;istatt=0) forr(j=00;jnextt,worrktemmp=woorkteemp-nextt) if(needdtempp-vaaluevvaluee) proocessstestt+; if(proccessttest=collum) forr(j=00;jvaluue+=aallocctempp-vaalue; worrktemmp1=wworkttemp11-neext; alllocteemp=aallocctempp-neext; if(stattus=0) patthheaad=paath1=pathh2=(sstrucct paath*)mallloc(ppathllen); patth1-nextt=patth2-nextt=NULLL; patth1-valuue=i; staatus+; elsse patth2=(
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度企業(yè)研發(fā)成果知識(shí)產(chǎn)權(quán)轉(zhuǎn)讓合同范本
- 2025年度醫(yī)療設(shè)備改造與售后服務(wù)協(xié)議
- 2025年度出租車公司車輛租賃合同修訂版
- 2025抖音主播品牌代言合作框架協(xié)議書
- 2025年C301-I型低壓甲醇合成催化劑合作協(xié)議書
- 2025年度咖啡廳店鋪轉(zhuǎn)讓合同范本
- 2025年創(chuàng)客中心項(xiàng)目發(fā)展計(jì)劃
- 科研部研究項(xiàng)目進(jìn)展與未來規(guī)劃計(jì)劃
- 教學(xué)資源的有效整合與利用計(jì)劃
- 度假村行業(yè)保安工作總結(jié)計(jì)劃
- 國(guó)家基本藥物使用培訓(xùn)課件
- 室內(nèi)設(shè)計(jì)現(xiàn)場(chǎng)測(cè)量總結(jié)
- copd護(hù)理查房的課件
- 信息安全與網(wǎng)絡(luò)安全的重要性與意義
- 《避孕藥具知識(shí)培訓(xùn)》課件
- 特教教師的教育科研
- 員工調(diào)崗調(diào)薪申請(qǐng)表
- 中心靜脈壓測(cè)量技術(shù)-中華護(hù)理學(xué)會(huì)團(tuán)體標(biāo)準(zhǔn)2023
- 項(xiàng)目考勤表(模板)
- 《鍋爐原理》試題庫及參考答案(學(xué)習(xí)資料)
- 防呆防錯(cuò)十大原理及案例分析
評(píng)論
0/150
提交評(píng)論