操作系統(tǒng):銀行家算法的實(shí)現(xiàn).docx_第1頁
操作系統(tǒng):銀行家算法的實(shí)現(xiàn).docx_第2頁
操作系統(tǒng):銀行家算法的實(shí)現(xiàn).docx_第3頁
操作系統(tǒng):銀行家算法的實(shí)現(xiàn).docx_第4頁
操作系統(tǒng):銀行家算法的實(shí)現(xiàn).docx_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

計(jì)科112康巖巖201100814220操作系統(tǒng)實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)四:銀行家算法的實(shí)現(xiàn)計(jì)科112 康巖巖 2011008142202013/4/29實(shí)驗(yàn)四:銀行家算法的實(shí)現(xiàn)一實(shí)驗(yàn)?zāi)康模?) 加深了解有關(guān)資源申請、避免死鎖等概念。(2) 體會和了解死鎖和避免死鎖的具體實(shí)施方法。二實(shí)驗(yàn)屬性該實(shí)驗(yàn)為設(shè)計(jì)性實(shí)驗(yàn)。三實(shí)驗(yàn)儀器設(shè)備及器材普通PC386以上微機(jī)四實(shí)驗(yàn)要求本實(shí)驗(yàn)要求2學(xué)時完成。本實(shí)驗(yàn)要求完成如下任務(wù):(1)設(shè)計(jì)進(jìn)程對各類資源最大申請表示及初值的確定。(2)設(shè)定系統(tǒng)提供資源的初始狀況。(3)設(shè)定每次某個進(jìn)程對各類資源的申請表示。(4)編制程序,依據(jù)銀行家算法,決定其資源申請是否得到滿足。(5)顯示資源申請和分配時的變化情況。五實(shí)驗(yàn)步驟(一)任務(wù)分析:實(shí)現(xiàn)銀行家算法,首先需要構(gòu)造四張鏈表表,如下:進(jìn)程最大需求數(shù)量表:Maxmn進(jìn)程以獲取資源量表 Allocationmn需求表 Needmn可用資資源 Availablen其中,m表示進(jìn)程數(shù)目。n表示資源數(shù)目。 對于銀行家算的實(shí)現(xiàn),我們可以先初始化一部分?jǐn)?shù)據(jù),模擬出某一狀態(tài)下的資源分配情況。并發(fā)出資源請求,然后判斷請求是否可行,并尋找安全序列。 可以看出,本次試驗(yàn)會設(shè)計(jì)到大量的數(shù)據(jù),所以為了簡化步驟,并且能直觀的得到算法運(yùn)行結(jié)果,需要用到窗口來呈現(xiàn)數(shù)據(jù)變化情況。(二)程序設(shè)計(jì):(1) 總體設(shè)計(jì):本次試驗(yàn)語言為java。程序分兩大部分,一部分是核心的銀行家算法,用來處理資源請求。另一部分是界面,用來發(fā)出資源請求并顯示處理結(jié)果。利用java的swing編程和相關(guān)IDE,很容易初始化資源分布情況,下面是其截圖:(2) 具體實(shí)現(xiàn):核心部分,銀行家算法: 算法的實(shí)現(xiàn)完全按照教材中的步驟來進(jìn)行,具體實(shí)現(xiàn)如下:/* * * param processID 進(jìn)程號 * param ra 請求A類資源數(shù)量 * param rb 請求A類資源數(shù)量 * param rc 請求C類資源數(shù)量 */public static List checkEnable(int processID, int ra, int rb, int rc) /得等到該進(jìn)程對各類資源的需求量數(shù)組Integer need = Need.get(processID);/得等到該進(jìn)程的各類資源的就緒量數(shù)組Integer allocation = Allocation.get(processID);/檢測請求數(shù)量是否大于需求量if (ra need0 | rb need1 | rc need2) return null;/檢測請求量是否大于可用量if (ra Available0 | rb Available1 | rc Available2) return null;/先根據(jù)需求修改各類數(shù)據(jù),如果后來檢測到不存在安全序列,這些數(shù)據(jù)還會復(fù)原Available0 -= ra;Available1 -= rb;Available2 -= rc;need0 -= ra;need1 -= rb;need2 -= rc;allocation0 += ra;allocation1 += rb;allocation2 += rc;/獲取安全序列List list = findSaftyLine(processID);/如果安全序列為空,則恢復(fù)剛才修改的數(shù)據(jù)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) /得到進(jìn)程數(shù)目int count = Max.size();/標(biāo)示進(jìn)程是否安全的數(shù)組boolean finish = new booleancount;for (int i = 0; i count; i+) finishi = false;/得到現(xiàn)有各類資源數(shù)目Integer work = new IntegerAvailable0, Available1, Available2;int curr = proceeeId;/安全序列List saft = new ArrayList();/尋找 滿足條件 ”finishi=false And Needijworkj“(摘自課本) 的進(jìn)程while (curr count) if (finishcurr = false) Integer need = Need.get(curr);/檢測需求量是否符合要求,如果符合,需要修改相應(yīng)數(shù)值if (need0 = work0) & (need1 = work1) & (need2 = work2) Integer allocation = Allocation.get(curr);work0 += allocation0;work1 += allocation1;work2 += allocation2;saft.add(curr); /線程號加入安全序列finishcurr = true; /標(biāo)示完成curr = 0; /從頭搜索數(shù)據(jù) else curr+; else curr+;/檢測是否有進(jìn)程不安全for (int i = 0; i count; i+) if (finishi = false) return null;return saft;界面部分:雖然界面不是實(shí)驗(yàn)的關(guān)鍵,但在本次實(shí)驗(yàn)中,界面擔(dān)當(dāng)了很重要作用,銀行家算法有可能涉及大量數(shù)據(jù),所以這需要一個良好的界面來呈現(xiàn)數(shù)據(jù)。在該程序中,只需要在表格中輸入各類請求資源的數(shù)目,并指定相關(guān)進(jìn)程,點(diǎn)擊按鈕就可的出計(jì)算結(jié)果,具體實(shí)現(xiàn)如下:/* * 初始化數(shù)據(jù) */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(); /獲取請求參數(shù),分別為三類資源的數(shù)目int ra_ = Integer.parseInt(this.ra.getText();int rb_ = Integer.parseInt(this.rb.getText();int rc_ = Integer.parseInt(this.rc.getText();/得到請求數(shù)據(jù)的進(jìn)程號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();/顯示各進(jìn)程的資源情況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+);/請求一次資源后,就禁止修改現(xiàn)有資源數(shù)目this.a.setEnabled(false);this.b.setEnabled(false);this.c.setEnabled(false);/根據(jù)安全序列顯示數(shù)據(jù)/得到可用資源的一個副本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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論