

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、<p><b> 一、實驗名稱:</b></p><p><b> 模擬實現(xiàn)銀行家算法</b></p><p><b> 二、實驗目的:</b></p><p> 用程序語言模擬實現(xiàn)銀行家算法。銀行家算法是避免死鎖的一種重要方法,通過編寫一個簡單的銀行家算法程序,加深了解有關資源申請
2、、避免死鎖等概念,并體會和了解死鎖和避免死鎖的具體實施方法。</p><p><b> 三、問題分析與設計</b></p><p> 1、算法思路:先對用戶提出的請求進行合法性檢查,即檢查請求是否大于需要的,是否大于可利用的。若請求合法,則進行預分配,對分配后的狀態(tài)調用安全性算法進行檢查。若安全,則分配;若不安全,則拒絕申請,恢復到原來的狀態(tài),拒絕申請。</
3、p><p> 2、銀行家算法步驟:</p><p> ?。?)如果Requesti<or =Need,則轉向步驟(2);否則,認為出錯,因為它所需要的資源數(shù)已超過它所宣布的最大值。</p><p> ?。?)如果Request<or=Available,則轉向步驟(3);否則,表示系統(tǒng)中尚無足夠的資源,進程必須等待。</p><p> ?。?)
4、系統(tǒng)試探把要求的資源分配給進程Pi,并修改下面數(shù)據(jù)結構中的數(shù)值:</p><p> Available=Available-Request[i];</p><p> Allocation=Allocation+Request;</p><p> Need=Need-Request;</p><p> (4)系統(tǒng)執(zhí)行安全性算法,檢查此次
5、資源分配后,系統(tǒng)是否處于安全狀態(tài)。</p><p> 3、安全性算法步驟:</p><p><b> (1)設置兩個向量</b></p><p> ?、俟ぷ飨蛄縒ork。它表示系統(tǒng)可提供進程繼續(xù)運行所需要的各類資源數(shù)目,執(zhí)行安全算法開始時,Work=Allocation;</p><p> ②布爾向量Finish。
6、它表示系統(tǒng)是否有足夠的資源分配給進程,使之運行完成,開始時先做Finish[i]=false,當有足夠資源分配給進程時,令Finish[i]=true。</p><p> (2)從進程集合中找到一個能滿足下述條件的進程:</p><p> ?、貴inish[i]=false</p><p> ?、贜eed<or=Work</p><p&g
7、t; 如找到,執(zhí)行步驟(3);否則,執(zhí)行步驟(4)。</p><p> ?。?)當進程P獲得資源后,可順利執(zhí)行,直至完成,并釋放出分配給它的資源,故應執(zhí)行:</p><p> Work=Work+Allocation;</p><p> Finish[i]=true; </p><p><b> 轉向步驟(2)。</b
8、></p><p> ?。?)如果所有進程的Finish[i]=true,則表示系統(tǒng)處于安全狀態(tài);否則,系統(tǒng)處于不安全狀態(tài)。</p><p><b> 4、流程圖:</b></p><p> ①系統(tǒng)主要過程流程圖:</p><p> ?、阢y行家算法流程圖:</p><p> ③安全性算
9、法流程圖:</p><p><b> 5、實現(xiàn)過程:</b></p><p><b> 一、開發(fā)環(huán)境</b></p><p><b> 編程語言:java</b></p><p> 開發(fā)環(huán)境:windows & eclipse</p><p&
10、gt;<b> 二、開發(fā)過程:</b></p><p> 1、display()用于初始化資源矩陣和初始化進程,available[][]、allocation[]、need[][]、resource[]、process[]。</p><p> 2、print(int)用于打印T時刻的資源矩陣。</p><p> 3、cal()用于進行
11、安全性算法。</p><p> 4、changeStatu()通過request向量改變資源使用情況并進行安全性檢測。</p><p> 5、main()用于初始化程序。</p><p> 三 、 關鍵代碼</p><p><b> 1.銀行家算法</b></p><p> whil
12、e(flag){</p><p> int[] req=new int[5];</p><p> System.out.print("請輸入您要請求資源的 編號:");</p><p> int choose=scan.nextInt();</p><p> System.out.print("請輸入該進
13、程的請求向量:");</p><p> for (int i = 0; i < resource; i++) {</p><p> req[i]=scan.nextInt();</p><p> }//將請求向量存入數(shù)組</p><p> boolean flag1 = (req[0]<=need[choose]
14、[0]&&req[1]<=need[choose][1]&&req[2]<=need[choose][2]&&req[3]<=need[choose][3]&&req[4]<=need[choose][4]);</p><p> boolean flag2 = (req[0]<=available[0]&&am
15、p;req[1]<=available[1]&&req[2]<=available[2]&&req[3]<=available[3]&&req[4]<=available[4]);</p><p> System.out.println(flag1);</p><p> System.out.println(fla
16、g2);</p><p> if(flag1){</p><p> if(flag2){</p><p> //判斷請求向量是否小于可用資源數(shù) </p><p> for (int i = 0; i < resource; i++) {</p><p> available[i]=available[i
17、]-req[i]; //改變可用資源數(shù) </p><p> allocation[choose][i]=allocation[choose][i]+req[i]; </p><p> need[choose][i]=need[choose][i]-req[i]; //改變所需資源數(shù)3</p><p><b> }</b></p>
18、;<p> int[] work=new int[5];//資源種類數(shù)量大小的數(shù)組 </p><p> boolean[] finish1=new boolean[process]; //設置finish標志數(shù)組 </p><p> for (int i = 0; i < process; i++) {</p><p> finish1[
19、i]=false;</p><p><b> }</b></p><p> int[] array1=new int[process];</p><p> int num1=1;</p><p> int count11=0; </p><p> while(num1<proces
20、s){</p><p> for (int i = 0; i < process; i++) {</p><p> if(finish1[i]==false){</p><p> if(need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[0
21、]&&need[i][3]<= work[3]&&need[i][4]<=work[4]){</p><p> for (int j2 = 0; j2 < resource; j2++) {</p><p> available[j2]=available[j2]+allocation[i][j2
22、];</p><p> available[j2] = work[j2];</p><p><b> }</b></p><p> finish1[i]=true;</p><p> array1[count11]=i;</p><p> count11++;</p>&l
23、t;p><b> }</b></p><p><b> }</b></p><p><b> }num1++;</b></p><p> } //安全性算法3</p><p> int count2=0;</p><p> for (i
24、nt i = 0; i < array1.length; i++) {</p><p> if(finish1[i]==true){</p><p><b> count2++;</b></p><p><b> }</b></p><p> } //判斷有多少個進程已經(jīng)成功得到所需進
25、程并結束</p><p> if(count2==process){</p><p> System.out.println("存在一個安全序列:");</p><p> for (int i = 0; i < array1.length; i++) {</p><p> System.out.print(&
26、quot;P"+array1[i]+"→");</p><p><b> }</b></p><p> } //判斷整個進程序列書否全部完成 并打印輸出安全序列</p><p><b> else{</b></p><p> System.out.printl
27、n("系統(tǒng)處于不安全狀態(tài)!");</p><p><b> }</b></p><p> System.out.println();</p><p><b> }</b></p><p><b> else{</b></p><p
28、> System.out.println("資源不夠清等待!");</p><p><b> }</b></p><p><b> }//if結束</b></p><p><b> else{</b></p><p> System.out.
29、println("請求資源已超過所需資源!");</p><p><b> }</b></p><p> }//while結束</p><p> }//end of method changeStatu</p><p> public static void main(String args[
30、]) throws IOException{</p><p> work w = new work();</p><p> int count2 = 1; </p><p> w.display();</p><p> w.print(count2++);</p><p><b> w.cal();
31、</b></p><p> w.changeStatu();</p><p> } //end of method main</p><p><b> 2.安全性算法</b></p><p> boolean[] finish = new boolean[process];</p>&l
32、t;p> for (int i = 0; i < process; i++) {</p><p> finish[i]=false;//初始化數(shù)組finish</p><p><b> }</b></p><p> int[] array = new int[process];//定義一個數(shù)組保存安全序列</p>
33、<p> int num=1;</p><p> int count1=1; </p><p> while(num<process){</p><p> for (int i = 0; i < process; i++) {</p><p> if(finish[i]==false){</p>
34、<p> if(need[i][0]<=available[0]&&need[i][1]<=available[1]&&need[i][2]<=available[0]&&need[i][3]<=available[3]&&need[i][4]<=available[4]){</p><p> for
35、(int j2 = 0; j2 < resource; j2++) {</p><p> available[j2]=available[j2]+allocation[i][j2];</p><p><b> }</b></p><p> finish[i]=true;</p><p> array[cou
36、nt1-1]=i;</p><p><b> count1++;</b></p><p><b> }</b></p><p><b> }</b></p><p><b> }num++;</b></p><p><
37、b> } //安全性算法</b></p><p><b> 3、實驗結果</b></p><p><b> 6、程序清單</b></p><p> package banker;</p><p> import java.io.IOException;</p>
38、<p> import java.util.Scanner;</p><p> public class work {</p><p><b> public</b></p><p> int resource;</p><p> int process;</p><p>
39、 int available[];</p><p> int max[][];</p><p> int allocation[][];</p><p> int need[][];//定義基本的數(shù)據(jù)</p><p> boolean flag1;</p><p> boolean flag2;</p
40、><p> Scanner scan = new Scanner(System.in);</p><p> public void display(){</p><p> System.out.println("\t\t"+"操作系統(tǒng)課程設計--銀行家算法");</p><p> System.ou
41、t.print("請輸入進程數(shù):");</p><p> process = scan.nextInt();</p><p> System.out.print("請輸入資源種類數(shù):");</p><p> resource = scan.nextInt();</p><p> System.o
42、ut.print("請輸入可用資源數(shù):");</p><p> available = new int[5];</p><p> for(int i = 0;i<resource;i++){</p><p> available[i] = scan.nextInt();</p><p><b> }
43、</b></p><p> System.out.println("請輸入資源分配矩陣");</p><p> allocation = new int [5][5];</p><p> for(int i = 0;i<process;i++){</p><p> System.out.print
44、("請輸入"+ i + "號進程已分配資源數(shù):");</p><p> for(int j =0;j<resource;j++){</p><p> allocation[i][j] = scan.nextInt();</p><p> }//end of resource</p><p>
45、 } //end of process</p><p> need = new int[5][5];</p><p> for(int i = 0;i<process;i++){</p><p> System.out.print("請輸入"+ i + "號進程還需要資源數(shù):");</p><
46、p> for(int j =0;j<resource;j++){</p><p> need[i][j] = scan.nextInt();</p><p> }//end of resource</p><p> } //end of process</p><p> max = new int[5][5];</
47、p><p> for(int i = 0;i<process;i++){</p><p> System.out.print("請輸入"+ i + "號進程最大需求資源數(shù):");</p><p> for(int j = 0;j<resource;j++){</p><p> max[i
48、][j] = scan.nextInt();</p><p> } // end of resource</p><p> }// end of for process</p><p> } //end of method display</p><p> public void print(int g){</p>&l
49、t;p> System.out.println();</p><p><b> /*</b></p><p><b> * 打印資源分配表</b></p><p><b> * */</b></p><p> System.out.println("T
50、"+g+"時刻的資源分配矩陣");</p><p> System.out.println("進程\tmax\t\tallocation\tneed\t\tavailable");</p><p> System.out.print("P0 ");</p><p> for (int i
51、= 0; i <resource; i++) {</p><p> System.out.print(max[0][i]+" ");</p><p><b> }</b></p><p> System.out.print(" ");</p><p> for
52、(int i = 0; i <resource; i++) {</p><p> System.out.print(allocation[0][i]+" ");</p><p><b> }</b></p><p> System.out.print(" ");</p>&
53、lt;p> for (int i = 0; i <resource; i++) {</p><p> System.out.print(need[0][i]+" ");</p><p><b> }</b></p><p> System.out.print(" ");</
54、p><p> for (int i = 0; i <resource; i++) {</p><p> System.out.print(available[i]+" ");</p><p><b> }</b></p><p> System.out.println();</p&
55、gt;<p> for (int i = 1; i < process; i++) {</p><p> System.out.print("P"+i+" ");</p><p> for (int j = 0; j < resource; j++) {</p><p> System.ou
56、t.print(max[i][j]+" ");</p><p><b> }</b></p><p> System.out.print(" ");</p><p> for (int j = 0; j < resource; j++) {</p><p> S
57、ystem.out.print(allocation[i][j]+" ");</p><p><b> }</b></p><p> System.out.print(" ");</p><p> for (int j = 0; j < resource; j++) {</p>
58、;<p> System.out.print(need[i][j]+" ");</p><p><b> }</b></p><p> System.out.println();</p><p> } //此段程序輸出已輸入數(shù)據(jù)s </p><p>
59、 }//end of method print</p><p> public void cal(){</p><p><b> /**</b></p><p><b> * 檢查安全序列</b></p><p><b> * */</b></p><
60、;p> boolean[] finish = new boolean[process];</p><p> for (int i = 0; i < process; i++) {</p><p> finish[i]=false;//初始化數(shù)組finish</p><p><b> }</b></p><
61、p> int[] array = new int[process];//定義一個數(shù)組保存安全序列</p><p> int num=1;</p><p> int count1=1; </p><p> while(num<process){</p><p> for (int i = 0; i < process
62、; i++) {</p><p> if(finish[i]==false){</p><p> if(need[i][0]<=available[0]&&need[i][1]<=available[1]&&need[i][2]<=available[0]&&need[i][3]<=available[3]&
63、;&need[i][4]<=available[4]){</p><p> for (int j2 = 0; j2 < resource; j2++) {</p><p> available[j2]=available[j2]+allocation[i][j2];</p><p><b> }</b></p&g
64、t;<p> finish[i]=true;</p><p> array[count1-1]=i;</p><p><b> count1++;</b></p><p><b> }</b></p><p><b> }</b></p>
65、<p><b> }num++;</b></p><p><b> } //安全性算法</b></p><p> int count=0;</p><p> for (int i = 0; i < array.length; i++) {</p><p> if(finis
66、h[i]==true){</p><p><b> count++;</b></p><p><b> }</b></p><p> }//檢查已完成的進程數(shù)目</p><p> if(count==process){</p><p> System.out.pri
67、ntln("存在一個安全序列:");</p><p> for (int i = 0; i < array.length; i++) {</p><p> System.out.print("P"+array[i]+"→");</p><p><b> }</b></
68、p><p><b> }//輸出安全序列</b></p><p><b> else{</b></p><p> System.out.println("系統(tǒng)處于不安全狀態(tài)!");</p><p><b> }</b></p><p&
69、gt; System.out.println();</p><p> }//end of cal</p><p> public void prinSecurityList(){</p><p> }//end of method prinSecurityList</p><p> public void changeStatu()
70、{</p><p> boolean flag=true;</p><p> while(flag){</p><p> int[] req=new int[5];</p><p> System.out.print("請輸入您要請求資源的 編號:");</p><p> int choo
71、se=scan.nextInt();</p><p> System.out.print("請輸入該進程的請求向量:");</p><p> for (int i = 0; i < resource; i++) {</p><p> req[i]=scan.nextInt();</p><p> }//將請求
72、向量存入數(shù)組</p><p> boolean flag1 = (req[0]<=need[choose][0]&&req[1]<=need[choose][1]&&req[2]<=need[choose][2]&&req[3]<=need[choose][3]&&req[4]<=need[choose][4]);&l
73、t;/p><p> boolean flag2 = (req[0]<=available[0]&&req[1]<=available[1]&&req[2]<=available[2]&&req[3]<=available[3]&&req[4]<=available[4]);</p><p> Sy
74、stem.out.println(flag1);</p><p> System.out.println(flag2);</p><p> if(flag1){</p><p> if(flag2){</p><p> //判斷請求向量是否小于可用資源數(shù) </p><p> for (int i = 0; i
75、 < resource; i++) {</p><p> available[i]=available[i]-req[i]; //改變可用資源數(shù) 分配資源</p><p> allocation[choose][i]=allocation[choose][i]+req[i]; //由于已分配資源 </p><p> need[choose][i]=ne
76、ed[choose][i]-req[i]; //改變所需資源數(shù)3</p><p><b> }</b></p><p> int[] work=new int[5];//資源種類數(shù)量大小的數(shù)組 用來構建available的數(shù)</p><p> boolean[] finish1=new boolean[process]; //設置fini
77、sh標志數(shù)組 </p><p> for (int i = 0; i < process; i++) {</p><p> finish1[i]=false;</p><p><b> }</b></p><p> int[] array1=new int[process];</p><
78、p> int num1=1;</p><p> int count11=0; </p><p> while(num1<process){</p><p> for (int i = 0; i < process; i++) {</p><p> if(finish1[i]==false){</p>
79、<p> if(need[i][0]<=work[0]&&need[i][1]<=work[1]&&need[i][2]<=work[0]&&need[i][3]<=work[3]&&need[i][4]<=work[4]){</p><p> for (int j2 = 0; j2 < resour
80、ce; j2++) {</p><p> available[j2]=available[j2]+allocation[i][j2];</p><p> available[j2] = work[j2];</p><p><b> }</b></p><p> finish1[i]=true;</p>
81、<p> array1[count11]=i;</p><p> count11++;</p><p><b> }</b></p><p><b> }</b></p><p><b> }num1++;</b></p><p>
82、; } //安全性算法3</p><p> int count2=0;</p><p> for (int i = 0; i < array1.length; i++) {</p><p> if(finish1[i]==true){</p><p><b> count2++;</b></p>
83、;<p><b> }</b></p><p> } //判斷有多少個進程已經(jīng)成功得到所需進程并結束</p><p> if(count2==process){</p><p> System.out.println("存在一個安全序列:");</p><p> for (in
84、t i = 0; i < array1.length; i++) {</p><p> System.out.print("P"+array1[i]+"→");</p><p><b> }</b></p><p> } //判斷整個進程序列書否全部完成 并打印輸出安全序列</p>
85、;<p><b> else{</b></p><p> System.out.println("系統(tǒng)處于不安全狀態(tài)!");</p><p><b> }</b></p><p> System.out.println();</p><p><b>
86、; }</b></p><p><b> else{</b></p><p> System.out.println("資源不夠清等待!");</p><p><b> }</b></p><p><b> }//if結束</b><
87、;/p><p><b> else{</b></p><p> System.out.println("請求資源已超過所需資源!");</p><p><b> }</b></p><p> }//while結束</p><p> }//end of
88、 method changeStatu</p><p> public static void main(String args[]) throws IOException{</p><p> work w = new work();</p><p> int count2 = 1; </p><p> w.display();<
89、;/p><p> w.print(count2++);</p><p><b> w.cal();</b></p><p> w.changeStatu();</p><p> } //end of method main</p><p> }// end of class work<
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 課程設計--銀行家算法
- 銀行家算法—課程設計
- 銀行家算法-課程設計
- 銀行家算法課程設計
- 銀行家算法課程設計
- 銀行家算法課程設計報告
- 銀行家算法課程設計報告
- 銀行家算法課程設計2
- 銀行家算法課程設計報告 (2)
- 銀行家算法模擬實現(xiàn)課程設計
- 操作系統(tǒng)課程設計--銀行家算法
- 銀行家算法的實現(xiàn)課程設計報告
- 課程設計---銀行家算法實驗報告
- 操作系統(tǒng)課程設計---銀行家算法
- 課程設計--銀行家算法的模擬實現(xiàn)
- 操作系統(tǒng)課程設計銀行家算法
- 操作系統(tǒng)課程設計--銀行家算法
- 操作系統(tǒng)課程設計(銀行家算法)
- 操作系統(tǒng)課程設計-銀行家算法
- 操作系統(tǒng)課程設計--銀行家算法
評論
0/150
提交評論