資源分配和管理的銀行家算法-銀行家算法實驗報告(共25頁)_第1頁
資源分配和管理的銀行家算法-銀行家算法實驗報告(共25頁)_第2頁
資源分配和管理的銀行家算法-銀行家算法實驗報告(共25頁)_第3頁
資源分配和管理的銀行家算法-銀行家算法實驗報告(共25頁)_第4頁
資源分配和管理的銀行家算法-銀行家算法實驗報告(共25頁)_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上操作系統(tǒng)實驗報告年級、專業(yè)、班級姓名實驗題目資源分配和管理的銀行家算法實驗時間 2013.05.14實驗地點主教0416實驗成績 實驗性質(zhì)驗證性 設(shè)計性 綜合性教師評價:算法/實驗過程正確; 源程序/實驗內(nèi)容提交 程序結(jié)構(gòu)/實驗步驟合理;實驗結(jié)果正確; 語法、語義正確; 報告規(guī)范; 其他: 評價教師簽名:一、實驗目的學習分配和管理資源的銀行家算法,了解死鎖避免方法。二、實驗項目內(nèi)容編寫程序?qū)崿F(xiàn)教材6.3.2節(jié)的銀行家算法程序功能:1. 程序隨機生成進程數(shù)量(>10)、資源種類(>3)、每類資源總數(shù)量(>3)、進程的申請資源的數(shù)量(>0)、已分配

2、資源的數(shù)量、可用資源數(shù)量等;2. 輸出每一個進程的資源分配情況;3. 輸出每一步的資源分配情況和進程執(zhí)行序列(安全序列)。4. 指出每一次資源分配后系統(tǒng)是否處于安全狀態(tài)。三、實驗過程或算法(源程序)31算法思路: 先對用戶提出的請求進行合法性檢查,即檢查請求是否大于需要的,是否大于可利用的。若請求合法,則進行預分配,對分配后的狀態(tài)調(diào)用安全性算法進行檢查。若安全,則分配;若不安全,則拒絕申請,恢復到原來的狀態(tài),拒絕申請。32銀行家算法步驟(1)如果Requesti=Need,則轉(zhuǎn)向步驟(2);否則,認為出錯,因為它所需要的資源數(shù)已超過它所宣布的最大值。(2)如果Requesti=Availabl

3、e,則轉(zhuǎn)向步驟(3);否則,表示系統(tǒng)中尚無足夠的資源,進程必須等待。(3)系統(tǒng)試探把要求的資源分配給進程Pi,并修改下面數(shù)據(jù)結(jié)構(gòu)中的數(shù)值:     Available=Available-Requesti;     Allocation=Allocation+Request;     Need=Need-Request;(4)系統(tǒng)執(zhí)行安全性算法,檢查此次資源分配后,系統(tǒng)是否處于安全狀態(tài)。33安全性算法步驟(1)設(shè)置工作向量工作向量Work。它表示系統(tǒng)可提供進程繼續(xù)運行所需要的

4、各類資源數(shù)目,執(zhí)行安全算法開始時,Work=Available;定義判斷一個進程是否執(zhí)行完畢的方法:boolean isFinished()。(2)從進程集合中找到一個能滿足下述條件的進程:process.isFinished()返回值為true.Need<=Work如找到,執(zhí)行步驟(3);否則,執(zhí)行步驟(4)。(3)當進程P獲得資源后,可順利執(zhí)行,直至完成,并釋放出分配給它的資源,故應執(zhí)行:Work=Work+Allocation;Allocation += Need; 轉(zhuǎn)向步驟(2)。(4)如果所有進程的均執(zhí)行完畢即isAllFinished()返回值為true,則表示系統(tǒng)處于安全狀

5、態(tài);否則,系統(tǒng)處于不安全狀態(tài)。34數(shù)據(jù)結(jié)構(gòu):34. 1主要用到的數(shù)據(jù)結(jié)構(gòu):(1) 保存進程最大資源需求量的矩陣: int _maxNeed(2) 保存進程已分配資源量的矩陣: int _allocated(3) 保存進程標識符的字符串:String _id(4) 保存系統(tǒng)中各資源總數(shù)的矩陣:int _totalResource(5) 表示申請資源的進程隊列:ArrayList<Process> _processes(6) 表示系統(tǒng)資源種類數(shù)的整數(shù):int _resourceClassCount(7) 存儲執(zhí)行信息:StringBuffer _executeInfo34. 2程序模塊

6、: 代表進程的類:Process.java代表銀行家算法的類:BankerAlgorithm.java算法的主界面:BankerUI.java34. 3各模塊間的調(diào)用關(guān)系:BankerUI是程序執(zhí)行的主界面,輸入系統(tǒng)資源種類數(shù)之后,其通過程序隨機生成進程數(shù)量(>10)、資源種類(>3)、每類資源總數(shù)量(>3)、進程的申請資源的數(shù)量(>0)、已分配資源的數(shù)量、可用資源數(shù)量等。其中所用針對系統(tǒng)進程和資源的操作均需要調(diào)用類BankerAlgorithm的方法。3.5主要函數(shù)的核心代碼:1. 進行初始化輸入的函數(shù)2. 打印輸出的函數(shù)3. 利用安全性算法進行檢測的函數(shù)4. 進行資

7、源分配的函數(shù)5. 利用行家算法進行判定的函數(shù) 注:具體代碼請見附錄源程序清單。程序流程圖:1、 系統(tǒng)主要過程流程圖: 2、進程請求資源序列圖 3、安全性算法序列圖四、實驗結(jié)果及分析和(或)源程序調(diào)試過程 4.1 主界面: 4.2點擊“隨機生成”按鈕,隨機生成進程數(shù)量(>10)、資源種類(>3)、每類資源總數(shù)量(>3)、進程的申請資源的數(shù)量(>0)、已分配資源的數(shù)量、可用資源數(shù)量,并向系統(tǒng)中添加進程,顯示進程的資源分配情況。 4.3點擊“分配資源”按鈕,檢查系統(tǒng)是否安全,如果當前系統(tǒng)安全,則輸出安全隊列,并給第一個安全進程分配資源。若安全:若不安全,則“執(zhí)行結(jié)果”提示框會

8、提示:4.4點擊“執(zhí)行進程”按鈕,執(zhí)行已經(jīng)分配資源的進程,并將其從隊列中移除。4.5點擊“分配資源”按鈕,重新檢測當前系統(tǒng)的安全,如果當前系統(tǒng)安全,則輸出安全隊列,并給第一個安全進程分配資源。4.6 此后重復4、5步,直至系統(tǒng)中的進程執(zhí)行完畢:4.7 系統(tǒng)中此時已沒有等待執(zhí)行的進程,若再點擊“分配資源”按鈕,則“執(zhí)行結(jié)果”提示框中會提示:4.8 如果需要再進行模擬,則點擊“重新生成”按鈕,會重新生成進程,再重復前7步即可。4.9 如果模擬結(jié)束,可點擊“退出”按鈕,即可退出系統(tǒng)。 模擬結(jié)束。五、心得體會通過本次實驗,我們對銀行家算法有了更深的了解,理解了操作系統(tǒng)關(guān)于進程調(diào)度的一些方法,并通過編程

9、實現(xiàn)了該算法。也進一步提高了我們的編程能力,從中學會了很多。附錄:源程序清單1.主界面類BankerUI.javapublic class BankerUI extends JFrame implements ActionListener private static final long serialVersionUID = -L;private JPanel panel;private JButton model;private JButton alloc;private JButton next;private JButton exit;private JTextField proces

10、sNum;/ centerprivate JEditorPane resourcesInfo;private JEditorPane processesInfo; / 用html格式顯示進程需要資源個數(shù).private JTextArea result;private JSplitPane splitCenter;private JPanel east;private int resourceClassesCount;/ 表示資源的個數(shù)private BankerAlgorithm banker;/private Process pro;private int Pronum = 0;priva

11、te int Sournum = 0;static int available = null;/ 可利用的資源 static int max = null;/ 最大的需求矩陣 static int allocation = null;/ 分配矩陣 static int need = null;/ 需求矩陣static int totalSour = null; static int Max = null; static int Allocation = null;public BankerUI() super("銀行家算法");try UIManager.setLookAn

12、dFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); catch (ClassNotFoundException e) e.printStackTrace(); catch (InstantiationException e) e.printStackTrace(); catch (IllegalAccessException e) e.printStackTrace(); catch (UnsupportedLookAndFeelException e) e.printStackTrace();setBo

13、unds(100, 100, 800, 600);panel = new JPanel(new BorderLayout();/ centerresourcesInfo = new JEditorPane("text/html", "<html></html>");resourcesInfo.setEditable(false);processesInfo = new JEditorPane("text/html", "<html></html>"); / 以html

14、格式顯示進程信息.processesInfo.setEditable(false);JSplitPane splitInfo = new JSplitPane(JSplitPane.VERTICAL_SPLIT);splitInfo.add(new JScrollPane(resourcesInfo), JSplitPane.TOP);splitInfo.add(new JScrollPane(processesInfo), JSplitPane.BOTTOM);splitInfo.setBorder(BorderFactory.createTitledBorder("系統(tǒng)信息&qu

15、ot;);splitInfo.setOneTouchExpandable(true);result = new JTextArea(5, 30);result.setEditable(false);result.setWrapStyleWord(true); / 按單詞換行,即所有單詞都不會打斷.result.setLineWrap(true); / 換行.JScrollPane textScroll = new JScrollPane(result);textScroll.setBorder(BorderFactory.createTitledBorder("執(zhí)行結(jié)果")

16、;splitCenter = new JSplitPane(JSplitPane.VERTICAL_SPLIT);splitCenter.setResizeWeight(1.0);splitCenter.add(splitInfo, JSplitPane.TOP);splitCenter.add(textScroll, JSplitPane.BOTTOM);splitCenter.setOneTouchExpandable(true); / 點擊一下就可以擴展分割開來的控件.panel.add(splitCenter, BorderLayout.CENTER);panel.setSize(80

17、0, 700);/ easteast = new JPanel();/east.setSize(60, 100);model = new JButton("隨機生成");model.setSize(50, 20);model.setLocation(10, 10);model.addActionListener(this);alloc = new JButton("分配資源");alloc.addActionListener(this);next = new JButton("執(zhí)行進程");next.addActionListener

18、(this);exit = new JButton("退出");exit.addActionListener(this);JLabel label = new JLabel("當前進程個數(shù):");label.setSize(50,20);label.setLocation(10, 60);processNum = new JTextField();processNum.setSize(50, 20);processNum.setLocation(10,90);processNum.setEditable(false);processNum.setFont

19、(new Font("宋體", Font.BOLD, 20);processNum.setForeground(Color.RED);processNum.setHorizontalAlignment(JTextField.CENTER);east.setLayout(new GridLayout(10,1,10,10);east.setSize(80, 100);east.add(label);east.add(processNum);east.add(model);east.add(alloc);east.add(next);east.add(exit);panel.a

20、dd(east, BorderLayout.EAST);setEastButtonEnabled(false);/this.getContentPane().add(new JScrollPane(panel);this.getContentPane().add(panel);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void setEastButtonEnabled(boolean b) / eastalloc.setEnabled(b);next.setEnabled(b);public BankerAlgorithm ge

21、tBanker() return banker;/ 一個數(shù)組小于另一個數(shù)組,兩個數(shù)組大小相等.public boolean aLowerB(int a, int b) for (int i = 0; i < a.length; i+) if (ai > bi)return false;return true;/ 在resourceInfoz中顯示系統(tǒng)資源的信息.private void updateTotalResourcesInfo() StringBuffer html = new StringBuffer(100);html.append("<html>

22、<body>");html.append("<table width = "100%" border = "1" bgcolor="#C0C0C0" bordercolor="#">n");StringBuffer resourceNames = new StringBuffer("<tr><td>資源名</td>");StringBuffer resourceCounts = new StringBuff

23、er("<tr><td>資源個數(shù)</td>");/int totalResource = banker.getTotalResource();for (int i = 0; i < Sournum; i+) resourceNames.append("<td>");resourceNames.append("R" + String.valueOf(i);resourceNames.append("</td>");resourceCounts.appe

24、nd("<td>");resourceCounts.append(String.valueOf(totalSouri);resourceCounts.append("</td>");resourceNames.append("</tr>");resourceCounts.append("</tr>");html.append(resourceNames);html.append(resourceCounts);html.append("</table&

25、gt;n</body>n</html>");resourcesInfo.setText(html.toString();private void updateProcessInfo() StringBuffer content = new StringBuffer("<html>n");content.append("<body>n");content.append("<table width = "100%" border = "1" bg

26、color="#C0C0C0" bordercolor="#">n");content.append("<tr><td>資源情況</td><td align = "center" colspan = "+ Sournum+ ">Max</td><td align = "center" colspan = "+ Sournum+ ">Allocated</td><

27、td align = "center" colspan = "+ Sournum+ ">Need</td><td align = "center" colspan = "+ Sournum + ">Avilable</td></tr>");content.append("<tr>");content.append("<td>進程名</td>");StringBuffer pro

28、cessNames = new StringBuffer(40);for (int i = 0; i < Sournum; i+) processNames.append("<td>R" + i + "</td>");content.append(processNames); / Maxcontent.append(processNames); / Allocatedcontent.append(processNames); / Needcontent.append(processNames); / Avilablecont

29、ent.append("</tr>");ArrayList<Process> processes = banker.getProcesses();/System.out.println("pppp"+processes.size();for (int i = 0; i < processes.size(); i+) Process p = processes.get(i);content.append("<tr>" + p.makeHtml();if (i = 0) int avilable

30、= banker.getAvilable();for (int j = 0; j < avilable.length; j+)content.append("<td>" + avilablej + "</td>");if (i = 1)content.append("<td rowspan ="+ String.valueOf(processes.size() - 1)+ " colspan = "3"></td>");content.app

31、end("</tr>");content.append("</table>n");content.append("</body>n");content.append("</html>");processesInfo.setText(content.toString();processNum.setText(""+Pronum);Overridepublic void actionPerformed(ActionEvent e) if (e.getS

32、ource() = model) RandomMake();banker = new BankerAlgorithm(totalSour,Sournum, new ArrayList<Process>();for (int i = 0; i < Pronum; i+) Max = new intSournum;Allocation = new intSournum;for (int j = 0; j < Sournum; j+) Maxj = maxij;Allocationj = allocationij;Process pro = new Process(Strin

33、g.valueOf(i),Max,Allocation,Sournum);if (banker.addProcess(pro) result.setText(result.getText() + "成功添加進程: "+ banker.getExecuteInfo(); else result.setText(result.getText() + "添加進程失敗: "+ banker.getExecuteInfo();return;/updateProcessInfo();updateTotalResourcesInfo(); / 更新系統(tǒng)資源信息.upd

34、ateProcessInfo(); / 更新系統(tǒng)進程資源信息.alloc.setEnabled(true);next.setEnabled(false);model.setText("重新生成");if (e.getSource() = alloc) String names = banker.getProcessNames();if (names.length = 0) result.setText(result.getText() + "當前系統(tǒng)中沒有進程,無法分配資源!n");next.setEnabled(false);return;if (ba

35、nker.isSecured() String id = banker.AllocationResourse();result.setText(result.getText() + "進程P"+id+"資源分配成功!n" +"當前系統(tǒng)是安全的.n安全序列: "+ banker.getExecuteInfo();updateProcessInfo(); / 更新系統(tǒng)進程資源信息.next.setEnabled(true); else result.setText(result.getText() +"資源分配失?。"

36、;+ "當前系統(tǒng)是不安全的.n詳細信息如下: "+ banker.getExecuteInfo();next.setEnabled(false);return;if (e.getSource() = next) String pid = banker.ExecuteProcess();if(Pronum > 0)Pronum-;updateProcessInfo();result.append("進程P"+pid+"執(zhí)行完畢!n");else JOptionPane.showMessageDialog(this, "系

37、統(tǒng)中所有進程已執(zhí)行完畢!", "提示:", JOptionPane.ERROR_MESSAGE); next.setEnabled(false);next.setEnabled(false);if (e.getSource() = exit) if (JOptionPane.showConfirmDialog(this, "退出系統(tǒng)?", "確定退出",JOptionPane.OK_CANCEL_OPTION) = JOptionPane.OK_OPTION)System.exit(0);return;public void

38、 RandomMake()Random rnd = new Random();Pronum = rnd.nextInt(10)+10;Sournum = rnd.nextInt(5)+3;available = new intSournum;max = new intPronumSournum;allocation = new intPronumSournum;need = new intPronumSournum;for (int i = 0; i < Sournum; i+) availablei = rnd.nextInt(5) + 3;for (int i = 0; i <

39、 Pronum; i+) for (int j = 0; j < Sournum; j+) allocationij = rnd.nextInt(5);/ 分配矩陣獲得隨機數(shù)for (int i = 0; i < Pronum; i+) for (int j = 0; j < Sournum; j+) do maxij = rnd.nextInt(10);/ 最大的需求矩陣獲得隨機數(shù),且要比分配矩陣相同位置的數(shù)大 while (maxij < allocationij);need = new intPronumSournum;for (int i = 0; i <

40、 Pronum; i+) for (int j = 0; j < Sournum; j+) needij = maxij - allocationij;totalSour = new intSournum;for (int i = 0; i < Sournum; i+) totalSouri=availablei;for (int i = 0; i < Pronum; i+) for (int j = 0; j < Sournum; j+) totalSourj+= allocationij;public static void main(String args) th

41、rows ClassNotFoundException,InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException BankerUI banker = new BankerUI(); banker.setVisible(true); 2.銀行家算法實現(xiàn)類 BankerAlgorithm.javapublic class BankerAlgorithm / 表示系統(tǒng)中資源種類數(shù)/private int saveProcess;private int savePNum = 0;private fin

42、al int _resourceClassesCount;private final int _totalResource;private ArrayList<Process> _processes = new ArrayList<Process>();private ArrayList<Process> saveProcesses = new ArrayList<Process>();private StringBuffer _executeInfo = new StringBuffer(50);public BankerAlgorithm(i

43、nt totalResource, int resourceClassesCount,ArrayList<Process> processes) _resourceClassesCount = resourceClassesCount;_totalResource = totalResource;_processes = processes;private ArrayList<Process> newProcesses() ArrayList<Process> pList = new ArrayList<Process>();for (Proce

44、ss p : _processes) pList.add(p.newProcess();return pList;public int getAvilable() int avilable = new int_resourceClassesCount;for (int i = 0; i < _resourceClassesCount; +i) avilablei = _totalResourcei - getResourceAllocated(i);return avilable;/ index代表某個資源的索引,結(jié)果為某個資源的已分配量.private int getResourceA

45、llocated(int index) int totalAllocated = 0;for (Process p : _processes) int allocated = p.getAllocated();totalAllocated += allocatedindex;return totalAllocated;public boolean addProcess(Process p) if (isUniqueProcessId(p.getId() _executeInfo.append(p.getId() + "=" + p.toString();return _pr

46、ocesses.add(p); else _executeInfo.append(p.getId() + "與已有進程重名.");return false;public void removeProcess(String processId) removeProcess(getProcessById(_processes, processId);public void removeProcess(Process p) _processes.remove(p);/_executeInfo.append(p.getId();String id;public String Exe

47、cuteProcess()if(savePNum < saveProcesses.size()Process p = saveProcesses.get(savePNum);id = p.getId();removeProcess(id);savePNum +;else_executeInfo.append("警告:所有進程已執(zhí)行完畢!");return id;/saveProcesses.remove(p);String idd;public String AllocationResourse()if(savePNum < saveProcesses.size

48、()Process p = saveProcesses.get(savePNum);idd = p.getId();int pmax = p.getMaxNeed();p.setAllocated(pmax);saveProcesses.set(savePNum, p);int no = 0;for (Process pp : _processes) if(idd = pp.getId()_processes.set(no, p);no+;return idd;public String getExecuteInfo() / 若存在安全序列,則要刪除最后一個"->".

49、int startIndex = _executeInfo.lastIndexOf("->");if (startIndex != -1) _executeInfo.delete(startIndex, startIndex + 2);_executeInfo.append("nn");String info = _executeInfo.toString();_executeInfo.delete(0, _executeInfo.length();return info;public ArrayList<Process> getPro

50、cesses() return _processes;public String getProcessNames() String names = new String_processes.size();for (int i = 0; i < _processes.size(); i+)namesi = _processes.get(i).getId();return names;/ 判斷當前系統(tǒng)是否安全public boolean isSecured() return isSecured(newProcesses(), getAvilable();private boolean isS

51、ecured(ArrayList<Process> pList, int avilable) if (!isAllMaxNeedLowerTotalResource(pList)return false;while (!isAllFinished(pList) Process p = searchProcessLowerAvilable(pList, avilable);if (p != null) int need = p.getNeed();p.allocate(need);/ System.out.println(p.getId();_executeInfo.append(&

52、quot;P" + p.getId() + "->");saveProcesses.add(p);sub(avilable, need);add(avilable, p.getAllocated(); else _executeInfo.append("系統(tǒng)中剩余進程的資源需求量均大于系統(tǒng)資源可用量.");return false;return true;private Process getProcessById(ArrayList<Process> pList, String id) for (Process p : pL

53、ist) if (p.equals(id)return p;return null;private boolean isAllFinished(ArrayList<Process> pList) for (Process p : pList) if (!p.isFinished()return false;return true;public boolean isAllMaxNeedLowerTotalResource(ArrayList<Process> processes) for (Process p : processes) if (!p.isMaxNeedLowerTotalResource(_totalResource, _executeInfo)return false;r

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論