银行家算法C++代码实现_第1页
银行家算法C++代码实现_第2页
银行家算法C++代码实现_第3页
银行家算法C++代码实现_第4页
银行家算法C++代码实现_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、编号: 实验一二三四五六七八九十总评教师签名成绩武汉大学计算机学院课程实验(设计)报告专业(班): 计算机科学与技术 计科6班 学 号: 2013301500217 姓 名: 张伟 课程名称: 操作系统设计 任课教师: 宋伟 2015年12 月22日银行家算法实现一、 实习内容编写实现银行家算法,实现资源的安全分配。通过本实验熟悉银行家算法,对预防死锁有更深刻的认识。二、 实习题目初始状态下,设置数据结构存储可利用资源向量(Available),最大需求矩阵(MAX),分配矩阵(Allocation),需求矩阵(Need),输入待分配进程队列和所需资源。设计安全性算法,设置工作向量表示系统可提

2、供进程继续运行的可利用资源数目。如果进程队列可以顺利执行打印输出资源分配情况,如果进程队列不能顺利执行打印输出分配过程,提示出现死锁位置。三、 设计思想数据结构class process /定义 进程 public : bool finish = false; /完成状态 int needmax_resources; /还需要分配的资源 int allocationmax_resources; /已经分配的资源 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; /构造函数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; /赋值函数process(); 主要函数(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) /初始化函数Main函数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:继续分配n 2:查看当前available资源数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;银行家算法操作部分银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。 设进程cusneed提出请求REQUEST i,则银行家算法按如下规则进行判断。 (1)如果REQUEST cusneed i<= NEEDcusneedi,则转(2);否则,出错。 (2)如果REQU

9、EST cusneed i<= AVAILABLEcusneedi,则转(3);否则,出错。 (3)系统试探分配资源,修改相关数据:          AVAILABLEi-=REQUESTcusneedi;          ALLOCATIONcusneedi+=REQUESTcusneedi;    

10、60;     NEEDcusneedi-=REQUESTcusneedi; (4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。安全性算法检验部分1)设置两个工作向量Work=AVAILABLE;FINISH (2)从进程集合中找到一个满足下述条件的进程, FINISH=false; NEED<=Work; 如找到,执行(3);否则,执行(4) (3)设进程获得资源,可顺利执行,直至完成,从而释放资源。 Work+=ALLOCA

11、TION; Finish=true; GOTO 2 (4)如所有的进程Finish= true,则表示安全;否则系统不安全。 结果显示部分 在屏幕上面打印本次分配资源是否成功或者失败 或者打印当前available资源状态四、 源代码 /*C+ Source File*/*开发环境为Microsoft Visual Studio 2015*/#include<iostream>using namespace std;#define max_process 5#define max_resources 4class process p

12、ublic : bool finish = false; /完成状态 int needmax_resources; /还需要分配的资源 int allocationmax_resources; /已经分配的资源 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; /构造函数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; /赋值函数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); /先把每个进程的状态存储在临时数组,最后在拷贝回去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); /安全性算法检测完毕,把数据拷贝回来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) /初始化函数int _max_needmax_resources;int i;int tempmax_resources = 0,0,0,0 ;cout << "初始化available数组值,请输入" << max_resources << "个值代表每个资源初始

23、数目,空格隔开" << 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:继续分配n 2:查看当前available资源数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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论