




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、編號(hào): 實(shí)驗(yàn)一二三四五六七八九十總評(píng)教師簽名成績(jī)武漢大學(xué)計(jì)算機(jī)學(xué)院課程實(shí)驗(yàn)(設(shè)計(jì))報(bào)告專業(yè)(班): 計(jì)算機(jī)科學(xué)與技術(shù) 計(jì)科6班 學(xué) 號(hào): 2013301500217 姓 名: 張偉 課程名稱: 操作系統(tǒng)設(shè)計(jì) 任課教師: 宋偉 2015年12 月22日銀行家算法實(shí)現(xiàn)一、 實(shí)習(xí)內(nèi)容編寫實(shí)現(xiàn)銀行家算法,實(shí)現(xiàn)資源的安全分配。通過本實(shí)驗(yàn)熟悉銀行家算法,對(duì)預(yù)防死鎖有更深刻的認(rèn)識(shí)。二、 實(shí)習(xí)題目初始狀態(tài)下,設(shè)置數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)可利用資源向量(Available),最大需求矩陣(MAX),分配矩陣(Allocation),需求矩陣(Need),輸入待分配進(jìn)程隊(duì)列和所需資源。設(shè)計(jì)安全性算法,設(shè)置工作向量表示系統(tǒng)可提
2、供進(jìn)程繼續(xù)運(yùn)行的可利用資源數(shù)目。如果進(jìn)程隊(duì)列可以順利執(zhí)行打印輸出資源分配情況,如果進(jìn)程隊(duì)列不能順利執(zhí)行打印輸出分配過程,提示出現(xiàn)死鎖位置。三、 設(shè)計(jì)思想數(shù)據(jù)結(jié)構(gòu)class process /定義 進(jìn)程 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 << " 選項(xiàng)n 1:繼續(xù)分配n 2:查看當(dāng)前available資源數(shù)n其他字符:退出n"scanf_s("%d"
6、;, &choice);switch (choice) case 1:cout << "請(qǐng)輸入本次請(qǐng)求分配給第i個(gè)進(jìn)程的資源,格式:進(jìn)程號(hào) 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 << "個(gè)資源還剩" << availablei << "個(gè)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)是否是安全的;若是,才分配。它是最具有代表性的避免死鎖的算法。 設(shè)進(jìn)程cusneed提出請(qǐng)求REQUEST i,則銀行家算法按如下規(guī)則進(jìn)行判斷。 (1)如果REQUEST cusneed i<= NEEDcusneedi,則轉(zhuǎn)(2);否則,出錯(cuò)。 (2)如果REQU
9、EST cusneed i<= AVAILABLEcusneedi,則轉(zhuǎn)(3);否則,出錯(cuò)。 (3)系統(tǒng)試探分配資源,修改相關(guān)數(shù)據(jù): AVAILABLEi-=REQUESTcusneedi; ALLOCATIONcusneedi+=REQUESTcusneedi;
10、60; NEEDcusneedi-=REQUESTcusneedi; (4)系統(tǒng)執(zhí)行安全性檢查,如安全,則分配成立;否則試探險(xiǎn)性分配作廢,系統(tǒng)恢復(fù)原狀,進(jìn)程等待。安全性算法檢驗(yàn)部分1)設(shè)置兩個(gè)工作向量Work=AVAILABLE;FINISH (2)從進(jìn)程集合中找到一個(gè)滿足下述條件的進(jìn)程, FINISH=false; NEED<=Work; 如找到,執(zhí)行(3);否則,執(zhí)行(4) (3)設(shè)進(jìn)程獲得資源,可順利執(zhí)行,直至完成,從而釋放資源。 Work+=ALLOCA
11、TION; Finish=true; GOTO 2 (4)如所有的進(jìn)程Finish= true,則表示安全;否則系統(tǒng)不安全。 結(jié)果顯示部分 在屏幕上面打印本次分配資源是否成功或者失敗 或者打印當(dāng)前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); /先把每個(gè)進(jìn)程的狀態(tài)存儲(chǔ)在臨時(shí)數(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+) /找到一個(gè)可以完成的資源for ( x = 0; x < max_resources; x+) /對(duì)于每個(gè)進(jìn)程,檢查資源是否夠if (my_processi.finish = false)check_everyonei = workx >= my_processi.needx;else break;if (check_everyonei = false)break;if (check_everyonei = true)/*先把資源分配給i進(jìn)程,然后運(yùn)行完后釋放掉*/for (x = 0; x < max_res
17、ources; x+)workx = workx + my_processi.needx;break;/*檢查是否所有的進(jìn)程都為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); /安全性算法檢測(cè)完畢,把數(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 << "請(qǐng)求資源超過宣布最大值或者資源不足,無(wú)法分配" << 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; /安全性算法檢測(cè)錯(cuò)誤,則回檔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ù)組值,請(qǐng)輸入" << max_resources << "個(gè)值代表每個(gè)資源初始
23、數(shù)目,空格隔開" << endl;for ( i = 0; i < max_resources; i+)scanf_s("%d", &availablei);for (i = 0; i < max_process; i+)cout << "進(jìn)程初始化" << endl;cout << "請(qǐng)輸入第" << i << "個(gè)進(jìn)程的最大所需每個(gè)資源的值,共計(jì)" << max_resources <<
24、 "個(gè)資源,用空格隔開" << 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 << " 選項(xiàng)n 1:繼續(xù)分配n 2:查看當(dāng)前available資源數(shù)n其他字符:退出n"scanf_s("%d", &choice);switch (choice) case 1:cout << "請(qǐng)輸入本次請(qǐng)求分配給第i個(gè)進(jìn)程的資源,格式:進(jìn)程號(hào) xx xx xx xx,空格隔開" << endl; scanf_s("%d", &i);for (j = 0; j < max_resources; j+)scanf_s("%d", &
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 公司組團(tuán)采摘活動(dòng)方案
- 公司故居活動(dòng)方案
- 公司每天小禮物活動(dòng)方案
- 公司旅游策劃活動(dòng)方案
- 公司新業(yè)務(wù)拓展策劃方案
- 短視頻制作師職業(yè)資格考試的測(cè)試題及答案
- 2025年信息系統(tǒng)項(xiàng)目管理師職業(yè)資格考試試卷及答案
- 2025年醫(yī)院管理師考試試題及答案
- 2025年心理咨詢與治療理論基礎(chǔ)考試試題及答案
- 2025年心理健康和社會(huì)支持服務(wù)考試試卷及答案
- 求職心理調(diào)適專家講座
- GB/T 6344-2008軟質(zhì)泡沫聚合材料拉伸強(qiáng)度和斷裂伸長(zhǎng)率的測(cè)定
- GB/T 3532-1995日用瓷器
- 學(xué)術(shù)論文寫作規(guī)范與技巧課件
- 生物高中-基于大數(shù)據(jù)分析的精準(zhǔn)教學(xué)課件
- 工程結(jié)算審計(jì)實(shí)施方案(共8篇)
- 樂東221氣田投產(chǎn)專家驗(yàn)收匯報(bào)
- 信任五環(huán)(用友營(yíng)銷技巧)課件
- 2022年廣東省深圳市中考化學(xué)真題試卷
- 危險(xiǎn)貨物道路運(yùn)輸安全生產(chǎn)管理制度
- GB∕T 8110-2020 熔化極氣體保護(hù)電弧焊用非合金鋼及細(xì)晶粒鋼實(shí)心焊絲
評(píng)論
0/150
提交評(píng)論