银行家算法C++代码实现.doc





《银行家算法C++代码实现.doc》由会员分享,可在线阅读,更多相关《银行家算法C++代码实现.doc(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流银行家算法C+代码实现【精品文档】第 11 页编号: 实验一二三四五六七八九十总评教师签名成绩武汉大学计算机学院课程实验(设计)报告专业(班): 计算机科学与技术 计科6班 学 号: 2013301500217 姓 名: 张伟 课程名称: 操作系统设计 任课教师: 宋伟 2015年12 月22日银行家算法实现一、 实习内容编写实现银行家算法,实现资源的安全分配。通过本实验熟悉银行家算法,对预防死锁有更深刻的认识。二、 实习题目初始状态下,设置数据结构存储可利用资源向量(Available),最大需求矩阵(MAX),分配矩阵(Allocation),需求矩
2、阵(Need),输入待分配进程队列和所需资源。设计安全性算法,设置工作向量表示系统可提供进程继续运行的可利用资源数目。如果进程队列可以顺利执行打印输出资源分配情况,如果进程队列不能顺利执行打印输出分配过程,提示出现死锁位置。三、 设计思想数据结构class process /定义 进程 public : bool finish = false; /完成状态 int needmax_resources; /还需要分配的资源 int allocationmax_resources; /已经分配的资源 int max_needmax_resources; /最大需求量 int requestmax_
3、resources; /本次需求量public:process(int _needmax_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 ma
4、x_resources; i+)needi = _needi;allocationi = 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 avai
5、lablemax_resources, process my_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)cout 此次destribute成功 endl;else cout 此次destribute不成功 endl; break;case 2: f
7、or (i = 0; i max_resources; i+)cout 第 i 个资源还剩 availablei 个n;break;default: break;cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();cin.get();return 0;银行家算法操作部分银行家算法的基本思想是分配资源之前,判断系统是否是安全的;若是,才分配。它是最具有代表性的避免死锁的算法。设进程cusneed提出请求REQUESTi,则银行家算法按如下规则进行判断。(1)如果REQUESTcusneed
8、i=NEEDcusneedi,则转(2);否则,出错。(2)如果REQUESTcusneedi=AVAILABLEcusneedi,则转(3);否则,出错。(3)系统试探分配资源,修改相关数据:AVAILABLEi-=REQUESTcusneedi;ALLOCATIONcusneedi+=REQUESTcusneedi;NEEDcusneedi-=REQUESTcusneedi;(4)系统执行安全性检查,如安全,则分配成立;否则试探险性分配作废,系统恢复原状,进程等待。安全性算法检验部分1)设置两个工作向量Work=AVAILABLE;FINISH(2)从进程集合中找到一个满足下述条件的进程,
9、FINISH=false;NEED=Work;如找到,执行(3);否则,执行(4)(3)设进程获得资源,可顺利执行,直至完成,从而释放资源。Work+=ALLOCATION;Finish=true;GOTO2(4)如所有的进程Finish=true,则表示安全;否则系统不安全。 结果显示部分 在屏幕上面打印本次分配资源是否成功或者失败 或者打印当前available资源状态四、 源代码 /*C+ Source File*/*开发环境为Microsoft Visual Studio 2015*/#includeusing namespace std;#define max_process 5#d
10、efine max_resources 4class process public : 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_resour
11、ces)for (int i = 0; i max_resources; 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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 银行家 算法 C+ 代码 实现

限制150内