操作系統:銀行家算法的實現.docx_第1頁
操作系統:銀行家算法的實現.docx_第2頁
操作系統:銀行家算法的實現.docx_第3頁
操作系統:銀行家算法的實現.docx_第4頁
操作系統:銀行家算法的實現.docx_第5頁
已閱讀5頁,還剩8頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

計科112康巖巖201100814220操作系統實驗報告實驗四:銀行家算法的實現計科112 康巖巖 2011008142202013/4/29實驗四:銀行家算法的實現一實驗目的(1) 加深了解有關資源申請、避免死鎖等概念。(2) 體會和了解死鎖和避免死鎖的具體實施方法。二實驗屬性該實驗為設計性實驗。三實驗儀器設備及器材普通PC386以上微機四實驗要求本實驗要求2學時完成。本實驗要求完成如下任務:(1)設計進程對各類資源最大申請表示及初值的確定。(2)設定系統提供資源的初始狀況。(3)設定每次某個進程對各類資源的申請表示。(4)編制程序,依據銀行家算法,決定其資源申請是否得到滿足。(5)顯示資源申請和分配時的變化情況。五實驗步驟(一)任務分析:實現銀行家算法,首先需要構造四張鏈表表,如下:進程最大需求數量表:Maxmn進程以獲取資源量表 Allocationmn需求表 Needmn可用資資源 Availablen其中,m表示進程數目。n表示資源數目。 對于銀行家算的實現,我們可以先初始化一部分數據,模擬出某一狀態下的資源分配情況。并發出資源請求,然后判斷請求是否可行,并尋找安全序列。 可以看出,本次試驗會設計到大量的數據,所以為了簡化步驟,并且能直觀的得到算法運行結果,需要用到窗口來呈現數據變化情況。(二)程序設計:(1) 總體設計:本次試驗語言為java。程序分兩大部分,一部分是核心的銀行家算法,用來處理資源請求。另一部分是界面,用來發出資源請求并顯示處理結果。利用java的swing編程和相關IDE,很容易初始化資源分布情況,下面是其截圖:(2) 具體實現:核心部分,銀行家算法: 算法的實現完全按照教材中的步驟來進行,具體實現如下:/* * * param processID 進程號 * param ra 請求A類資源數量 * param rb 請求A類資源數量 * param rc 請求C類資源數量 */public static List checkEnable(int processID, int ra, int rb, int rc) /得等到該進程對各類資源的需求量數組Integer need = Need.get(processID);/得等到該進程的各類資源的就緒量數組Integer allocation = Allocation.get(processID);/檢測請求數量是否大于需求量if (ra need0 | rb need1 | rc need2) return null;/檢測請求量是否大于可用量if (ra Available0 | rb Available1 | rc Available2) return null;/先根據需求修改各類數據,如果后來檢測到不存在安全序列,這些數據還會復原Available0 -= ra;Available1 -= rb;Available2 -= rc;need0 -= ra;need1 -= rb;need2 -= rc;allocation0 += ra;allocation1 += rb;allocation2 += rc;/獲取安全序列List list = findSaftyLine(processID);/如果安全序列為空,則恢復剛才修改的數據if (list = null) Available0 += ra;Available1 += rb;Available2 += rc;need0 += ra;need1 += rb;need2 += rc;allocation0 -= ra;allocation1 -= rb;allocation2 -= rc;return list;/* * 尋找安全序列 * param proceeeId * return */private static List findSaftyLine(int proceeeId) /得到進程數目int count = Max.size();/標示進程是否安全的數組boolean finish = new booleancount;for (int i = 0; i count; i+) finishi = false;/得到現有各類資源數目Integer work = new IntegerAvailable0, Available1, Available2;int curr = proceeeId;/安全序列List saft = new ArrayList();/尋找 滿足條件 ”finishi=false And Needijworkj“(摘自課本) 的進程while (curr count) if (finishcurr = false) Integer need = Need.get(curr);/檢測需求量是否符合要求,如果符合,需要修改相應數值if (need0 = work0) & (need1 = work1) & (need2 = work2) Integer allocation = Allocation.get(curr);work0 += allocation0;work1 += allocation1;work2 += allocation2;saft.add(curr); /線程號加入安全序列finishcurr = true; /標示完成curr = 0; /從頭搜索數據 else curr+; else curr+;/檢測是否有進程不安全for (int i = 0; i count; i+) if (finishi = false) return null;return saft;界面部分:雖然界面不是實驗的關鍵,但在本次實驗中,界面擔當了很重要作用,銀行家算法有可能涉及大量數據,所以這需要一個良好的界面來呈現數據。在該程序中,只需要在表格中輸入各類請求資源的數目,并指定相關進程,點擊按鈕就可的出計算結果,具體實現如下:/* * 初始化數據 */private void initData() int rows = this.jTable1.getRowCount();this.jComboBox1.removeAllItems();for (int i = 0; i rows; i+) this.jComboBox1.addItem(this.jTable1.getValueAt(i, 0);for (int i = 0; i rows; i+) /最大需求表Integer max = Integer.parseInt(this.jTable1.getValueAt(i, 1).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 2).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 3).toString();DoMethod.Max.add(max);/已獲得資源表Integer allocation = Integer.parseInt(this.jTable1.getValueAt(i, 4).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 5).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 6).toString();DoMethod.Allocation.add(allocation);/需求表Integer need = Integer.parseInt(this.jTable1.getValueAt(i, 7).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 8).toString(), Integer.parseInt(this.jTable1.getValueAt(i, 9).toString();DoMethod.Need.add(need);/各類資源總量 DoMethod.Available = new IntegerInteger.parseInt(this.a.getText(), Integer.parseInt(this.b.getText(), Integer.parseInt(this.c.getText(); private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) DoMethod.Available = new IntegerInteger.parseInt(this.a.getText(), Integer.parseInt(this.b.getText(), Integer.parseInt(this.c.getText(); /獲取請求參數,分別為三類資源的數目int ra_ = Integer.parseInt(this.ra.getText();int rb_ = Integer.parseInt(this.rb.getText();int rc_ = Integer.parseInt(this.rc.getText();/得到請求數據的進程號int processId = this.jComboBox1.getSelectedIndex();/獲取安全序列List list = DoMethod.checkEnable(processId, ra_, rb_, rc_);if (list != null) /顯示處理情況showResult(list); else JOptionPane.showMessageDialog(null, 此請求無法獲得安全序列!);/ TODO add your handling code here: /* * 顯示安全序列 * param list */private void showResult(List list) DefaultTableModel newtable1 = (DefaultTableModel) this.jTable1.getModel();newtable1.setRowCount(0);/清空表格1int count = DoMethod.Max.size();/顯示各進程的資源情況for (int i = 0; i count; i+) Integer max = DoMethod.Max.get(i);Integer need = DoMethod.Need.get(i);Integer allocation = DoMethod.Allocation.get(i);Object o = new ObjectP + i, max0, max1, max2, allocation0, allocation1, allocation2, need0, need1, need2;newtable1.addRow(o);this.jTable1.updateUI();/顯示可用資源情況this.a.setText(DoMethod.Available0+);this.b.setText(DoMethod.Available1+);this.c.setText(DoMethod.Available2+);/請求一次資源后,就禁止修改現有資源數目this.a.setEnabled(false);this.b.setEnabled(false);this.c.setEnabled(false);/根據安全序列顯示數據/得到可用資源的一個副本Integer work = DoMethod.Available0, DoMethod.Available1, DoMethod.Available2;DefaultTableModel newtable2 = (DefaultTableModel) this.jTable2.getModel();newtable2.setRowCount(0);/清空表格for (int get : list) Integer need = DoMethod.Need.get(get);Integer allocation = DoMethod.Allocation.get(get);Object o = new ObjectP + get, work0, work1, work2, need0, need1, need2, allocation0, allocation1, allocation2, work0 + allocation0, work1 + alloc

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論