




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、編號: 實驗一二三四五六七八九十總評教師簽名成績武漢大學計算機學院課程實驗(設計)報告專業(yè)(班): 計算機科學與技術 計科6班 學 號: 2013301500217 姓 名: 張偉 課程名稱: 操作系統(tǒng)設計 任課教師: 宋偉 2015年12 月22日銀行家算法實現(xiàn)一、 實習內(nèi)容編寫實現(xiàn)銀行家算法,實現(xiàn)資源的安全分配。通過本實驗熟悉銀行家算法,對預防死鎖有更深刻的認識。二、 實習題目初始狀態(tài)下,設置數(shù)據(jù)結(jié)構(gòu)存儲可利用資源向量(Available),最大需求矩陣(MAX),分配矩陣(Allocation),需求矩陣(Need),輸入待分配進程隊列和所需資源。設計安全性算法,設置工作向量表示系統(tǒng)可提
2、供進程繼續(xù)運行的可利用資源數(shù)目。如果進程隊列可以順利執(zhí)行打印輸出資源分配情況,如果進程隊列不能順利執(zhí)行打印輸出分配過程,提示出現(xiàn)死鎖位置。三、 設計思想數(shù)據(jù)結(jié)構(gòu)class process /定義 進程 public : bool finish = false; /完成狀態(tài) int needmax_resources; /還需要分配的資源 int allocationmax_resources; /已經(jīng)分配的資源 int max_needmax_resources; /最大需求量 int requestmax_resources; /本次需求量public:process(int _needma
3、x_resources, int _allocationmax_resources, int _max_needmax_resources)for (int i = 0; i < max_resources; i+)needi = _needi;allocationi = _allocationi;max_needi = _max_needi; /構(gòu)造函數(shù)void set(int _needmax_resources, int _max_needmax_resources)for (int i = 0; i < max_resources; i+)needi = _needi;al
4、locationi = 0;max_needi = _max_needi; /賦值函數(shù)process(); 主要函數(shù)(1)bool check_safe(int workmax_process, process my_processmax_process) /安全性算法(2)bool destribute(int availablemax_resources, process the_process, process my_processmax_process) /是否分配空間成功的算法 (3) void init(int availablemax_resources, process my_
5、processmax_process) /初始化函數(shù)Main函數(shù)int main()int _needmax_resources;int _allocationmax_resources;int availablemax_resources;process my_processmax_process;int i,j;int choice=1;init( available, my_process);while (true)cout << " 選項n 1:繼續(xù)分配n 2:查看當前available資源數(shù)n其他字符:退出n"scanf_s("%d"
6、;, &choice);switch (choice) case 1:cout << "請輸入本次請求分配給第i個進程的資源,格式:進程號 xx xx xx xx,空格隔開" << endl; scanf_s("%d", &i);for (j = 0; j < max_resources; j+)scanf_s("%d", &my_processi.requestj);if (destribute(available, my_processi, my_process) = true
7、)cout << "此次destribute成功" << endl;else cout << "此次destribute不成功" << endl; break;case 2: for (i = 0; i < max_resources; i+)cout << "第" << i << "個資源還剩" << availablei << "個n"break;default: break;c
8、in.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();return 0;銀行家算法操作部分銀行家算法的基本思想是分配資源之前,判斷系統(tǒng)是否是安全的;若是,才分配。它是最具有代表性的避免死鎖的算法。 設進程cusneed提出請求REQUEST i,則銀行家算法按如下規(guī)則進行判斷。 (1)如果REQUEST cusneed i<= NEEDcusneedi,則轉(zhuǎn)(2);否則,出錯。 (2)如果REQU
9、EST cusneed i<= AVAILABLEcusneedi,則轉(zhuǎn)(3);否則,出錯。 (3)系統(tǒng)試探分配資源,修改相關數(shù)據(jù): AVAILABLEi-=REQUESTcusneedi; ALLOCATIONcusneedi+=REQUESTcusneedi;
10、60; NEEDcusneedi-=REQUESTcusneedi; (4)系統(tǒng)執(zhí)行安全性檢查,如安全,則分配成立;否則試探險性分配作廢,系統(tǒng)恢復原狀,進程等待。安全性算法檢驗部分1)設置兩個工作向量Work=AVAILABLE;FINISH (2)從進程集合中找到一個滿足下述條件的進程, FINISH=false; NEED<=Work; 如找到,執(zhí)行(3);否則,執(zhí)行(4) (3)設進程獲得資源,可順利執(zhí)行,直至完成,從而釋放資源。 Work+=ALLOCA
11、TION; Finish=true; GOTO 2 (4)如所有的進程Finish= true,則表示安全;否則系統(tǒng)不安全。 結(jié)果顯示部分 在屏幕上面打印本次分配資源是否成功或者失敗 或者打印當前available資源狀態(tài)四、 源代碼 /*C+ Source File*/*開發(fā)環(huán)境為Microsoft Visual Studio 2015*/#include<iostream>using namespace std;#define max_process 5#define max_resources 4class process p
12、ublic : bool finish = false; /完成狀態(tài) int needmax_resources; /還需要分配的資源 int allocationmax_resources; /已經(jīng)分配的資源 int max_needmax_resources; /最大需求量 int requestmax_resources; /本次需求量public:process(int _needmax_resources, int _allocationmax_resources, int _max_needmax_resources)for (int i = 0; i < max_resou
13、rces; i+)needi = _needi;allocationi = _allocationi;max_needi = _max_needi; /構(gòu)造函數(shù)void set(bool _finish, int _needmax_resources, int _allocationmax_resources, int _max_needmax_resources,int _requestmax_resources)for (int i = 0; i < max_resources; i+)finish = _finish;needi = _needi;allocationi = _al
14、locationi;max_needi = _max_needi;requesti = _requesti; /賦值函數(shù)process();bool check_safe(int workmax_process, process my_processmax_process) /安全性算法int temp_workmax_process;process temp_processmax_process;for (int no = 0; no < max_process; no+)temp_workno = workno;temp_processno.set(my_processno.fini
15、sh, my_processno.need, my_processno.allocation, my_processno.max_need, my_processno.request); /先把每個進程的狀態(tài)存儲在臨時數(shù)組,最后在拷貝回去int i = 0;int x = 0;bool check_everyonemax_process = true ,true,true,true,true;bool check = false;int num = 0;while (check = false&&num<max_process*max_process/2)num+;for
16、 (i = 0; i < max_process; i+) /找到一個可以完成的資源for ( x = 0; x < max_resources; x+) /對于每個進程,檢查資源是否夠if (my_processi.finish = false)check_everyonei = workx >= my_processi.needx;else break;if (check_everyonei = false)break;if (check_everyonei = true)/*先把資源分配給i進程,然后運行完后釋放掉*/for (x = 0; x < max_res
17、ources; x+)workx = workx + my_processi.needx;break;/*檢查是否所有的進程都為true,如果是,那么check置為true*/for (int temp = 0; temp < max_process; temp+)if (check_everyonetemp = false)check = false; break;else check = true;/*cout << "check" << endl;*/for (int no = 0; no < max_process; no+)wo
18、rkno = temp_workno;my_processno.set(temp_processno.finish, temp_processno.need, temp_processno.allocation, temp_processno.max_need, temp_processno.request); /安全性算法檢測完畢,把數(shù)據(jù)拷貝回來return check;bool destribute(int availablemax_resources, process the_process, process my_processmax_process) /是否分配成功的算法int i
19、= 0;int enough = 1;for (i = 0; i < max_resources; i+)if (the_process.requesti <= the_process.needi && the_process.requesti < availablei)enough = enough * 1;else enough = 0; /檢查request的值是不是小于need和availableif (enough > 0)for (i = 0; i < max_resources; i+)availablei = availablei
20、- the_process.requesti;the_process.allocationi = the_process.allocationi + the_process.requesti;the_process.needi = the_process.needi - the_process.requesti;elsecout << "請求資源超過宣布最大值或者資源不足,無法分配" << endl;return false;if (check_safe(available, my_process) = true)cout << &quo
21、t;此次分配成功" << endl;return true;elsecout << "此次尋找失敗" << endl;for (i = 0; i < max_resources; i+)availablei = availablei + the_process.requesti;the_process.allocationi = the_process.allocationi - the_process.requesti;the_process.needi = the_process.needi + the_process
22、.requesti;the_process.finish = false; /安全性算法檢測錯誤,則回檔return false;void init(int availablemax_resources, process my_processmax_process) /初始化函數(shù)int _max_needmax_resources;int i;int tempmax_resources = 0,0,0,0 ;cout << "初始化available數(shù)組值,請輸入" << max_resources << "個值代表每個資源初始
23、數(shù)目,空格隔開" << endl;for ( i = 0; i < max_resources; i+)scanf_s("%d", &availablei);for (i = 0; i < max_process; i+)cout << "進程初始化" << endl;cout << "請輸入第" << i << "個進程的最大所需每個資源的值,共計" << max_resources <<
24、 "個資源,用空格隔開" << endl;for (int j = 0; j < max_resources; j+)scanf_s("%d", &_max_needj);my_processi.set(false, _max_need, temp, _max_need,temp);int main()int _needmax_resources;int _allocationmax_resources;int availablemax_resources;process my_processmax_process;int i,
25、j;int choice=1;init( available, my_process);while (true)cout << " 選項n 1:繼續(xù)分配n 2:查看當前available資源數(shù)n其他字符:退出n"scanf_s("%d", &choice);switch (choice) case 1:cout << "請輸入本次請求分配給第i個進程的資源,格式:進程號 xx xx xx xx,空格隔開" << endl; scanf_s("%d", &i);for (j = 0; j < max_resources; j+)scanf_s("%d", &
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度旅游休閑場地經(jīng)營權轉(zhuǎn)讓合同
- 二零二五年度木門行業(yè)知識產(chǎn)權保護合作協(xié)議
- 2025年度魚塘養(yǎng)殖水域污染賠償協(xié)議
- 2025年度綠色建筑企業(yè)設計師聘用合同
- 2025年度高校畢業(yè)生就業(yè)實習與培訓服務合同
- 2025年度網(wǎng)絡安全防護與招防范合同法律風險全面解決方案
- 二零二五年度租賃住宅安全事故責任認定與處理合同
- 二零二五年度劇組特聘演員服務協(xié)議書
- 二零二五年度煤炭行業(yè)國際市場開拓合作協(xié)議
- 二零二五年度農(nóng)村自建房買賣與農(nóng)村電子商務平臺合作協(xié)議
- 人教PEP版六年級下冊英語全冊課件(2024年2月修訂)
- 城市智慧交通管理系統(tǒng)
- 飛行中鳥擊的危害與防范
- 青少年人工智能技術水平測試一級04
- 核安全與核安全文化課件
- 《“健康中國2030”規(guī)劃綱要》全文健康中國2030規(guī)劃綱要全文
- 有限公司章程范本
- 培訓山地光伏電站設計
- 第4課 視覺中的紅屋頂 課件 2022-2023學年湘美版初中美術八年級下冊
- 蛇的介紹課件
- 國外藥典介紹
評論
0/150
提交評論