课程设计模拟银行家算法避免死锁.doc

上传人:laozhun 文档编号:4186800 上传时间:2023-04-09 格式:DOC 页数:14 大小:176KB
返回 下载 相关 举报
课程设计模拟银行家算法避免死锁.doc_第1页
第1页 / 共14页
课程设计模拟银行家算法避免死锁.doc_第2页
第2页 / 共14页
课程设计模拟银行家算法避免死锁.doc_第3页
第3页 / 共14页
课程设计模拟银行家算法避免死锁.doc_第4页
第4页 / 共14页
课程设计模拟银行家算法避免死锁.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

《课程设计模拟银行家算法避免死锁.doc》由会员分享,可在线阅读,更多相关《课程设计模拟银行家算法避免死锁.doc(14页珍藏版)》请在三一办公上搜索。

1、模拟通过银行家算法避免死锁一、 银行家算法产生的背景及目的 1:在多道程序系统中,虽然借助于多个进程的并发执行来改善系统的利用率,提高系统的吞吐量,但可能发生一种危险死锁。死锁就是多个进程在运行过程中因争夺资源而造成的一种僵局,当进程处于这种僵局状态时,如无外力作用,他们将无法再向前进行,如再把信号量作为同步工具时,多个Wait和Signal操作顺序不当,会产生进程死锁。然而产生死锁的必要条件有互斥条件,请求和保持条件,不剥夺条件和环路等待条件。在预防死锁的几种方法中,都施加了较强的限制条件,在避免死锁的方法中,所施加的条件较弱,有可能获得令人满意的系统性能。在该方法中把系统的状态分为安全状态

2、和不安全状态,只要能使系统都处于安全状态,便可避免死锁。2:实验目的:让学生独立的使用编程语言编写和调试一个系统分配资源的简单模拟程序,了解死锁产生的原因及条件。采用银行家算法及时避免死锁的产生,进一步理解课堂上老师讲的相关知识点。银行家算法是从当前状态出发,逐个按安全序列检查各客户中谁能完成其工作,然后假定其完成工作且归还全部贷款,再进而检查下一个能完成工作的客户。如果所有客户都能完成工作,则找到一个安全序列,银行家才是安全的。二:银行家算法中的数据结构 1:可利用资源向量Available。这是一个含有m个元素的数组,其中的每个元素代表一类可利用的资源数目,其初始值是系统中所配置的该类全部

3、可用资源的数目,其数值随该类资源的分配和回收而动态的改变。如果Availablej=k,z则表示系统中现有Rj类资源K 个。 2:最大需求矩阵Max。这是一个n*m的矩阵,它定义了系统中n个进程中的每一个进程对m类资源的最大需求。如果Maxi,j=k,表示第i个进程需要第Rj类资源的最大数目k个.3: 分配矩阵Allocation,也是n*m的矩阵,若Allocationi,j=k,表示第i个进程已分配Rj类资源的数目为k个。 4:需求矩阵Need。也是一个n*m的矩阵,Needi,j=k,表示第i个进程还需Rj类资源k个。三、银行家算法及安全性算法 1:银行家算法设Requesti是进程Pi

4、的请求向量,若Requestij=k;表示进程需要j类资源k个。当Pi发出资源请求时,系统按下属步骤进行检查;(1) 如果Requestij=Needij;便转向步骤(2),否则认为出错,因为它所需要的资源数已超过他所宣布的最大值。(2) 如果Requestij=Availableij,便转向步骤(3),否则认为尚无足够资源,进程需等待。(3) 系统试探着把资源分配给进程,并修改下面数据结构的数据Availableij=Availableij-Requestij;Allocationij=Allocationij+Requestij;Needij=Needij-Requestij;(4) 系统

5、执行安全性算法,检查此次资源分配后系统是否处于安全状态。若安全,才正式将资源分配给进程Pi,已完成此次分配。否则,将本次的试探分配作废,回复原来的资源分配状态,将进程Pi等待。2:安全性算法(1) 设置两个向量; 1:工作向量Work,表示系统可提供给进程运行所需的各类资源数目,它含有m个元素,初始时Work=Available 2:Finish ,表示系统是否有足够的资源分配给进程,使之运行完成。开始时先做Finishi=true(2) 从进程中找到一个能满需下属条件的进程1;Finishi=false;2:Needij=Workj;若找到执行步骤(3),否则执行步骤(4)(3) 当进程Pi

6、顺利获得资源后,直至完成,并释放分配给它的资源,执行: Workj=Workj+Allocationij; Finishi=true; Go to step (2);(5) 如果所有的进程Finishi都满足,则表示系统处于安全状态,否则,处于不安全状态。四、模块设计与分析及整体功能概述模块设计与分析: 整个银行家算法分为初始化函数Init(),安全性算法函数 safe(),银行家算法函数bank()三部分。初始化函数生成开始时刻系统中的进程和资源情况,安全性算法判断当某进程申请资源时,系统能否处于安全状态。在本实验中,若系统处于安全状态,便生成一个安全进程序列(安全序列可能有多个)。银行家算

7、法函数bank()负责整体的检查与异常判断。整体功能概述:死锁会引起系统陷入僵局,操作系统必须防止此现象的发生。本实验通过一个动态分配资源的模拟程序,更清楚的理解死锁产生的原因和条件。Dijkstra的银行家算法是最有代表性的避免死锁的方法。运行程序时用户设定系统中进程和可利用资源的种类数目。输入各进程的可利用资源Available,最大需求MAX,已分配资源Allocation ,需求资源Need,之后各系统发出资源请求Request,利用实验中的安全性算法判断能否产生一个安全性队列,若能,则给该进程分配成功,否则,不予分配。五、流程图设计 六、源代码及调试分析 #include#defin

8、e MAXm 50 / 定义最大进程数#define MAXn 100 /定义最大资源数int MAXMAXmMAXn; /最大需求矩阵int AllocationMAXmMAXn; /已分配矩阵int AvailableMAXn; /可用资源数组int NeedMAXmMAXn; /需求矩阵int RequestMAXmMAXn; /请求矩阵int FinishMAXm; /存储完成资源分配的进程int SequenceMAXm; /模拟的资源分配序列int WorkMAXn; /系统是否有足够的资源分配给进程int m,n; /m个进程,n个资源#define False 0#define

9、 True 1void input(); /数据输入函数int safealg(); /安全性算法函数void banker(); /银行家算法函数void main()input();safealg();banker();/*初始化算法*void input() int i,j; /*自定义进程数目与资源种类*cout*n;cout*利用银行家算法避免死锁*n; cout* *n;cout*n;coutm;coutn;/*输入每个进程对每种资源的最大需求、已经获得的数量、每种类型资源的数目cout各进程资源最大需求(Max),按照mxn矩阵输入:n;for(i=0;im;i+) coutPi

10、:; for(j=0;jMAXij; if(j=n)cout资源种类数匹配出现错误!;/当资源配置的种类数大于预先输入的数值时,出错cout各进程当前获得资源(Allocation),按照mxn矩阵输入endl;for(i=0;im;i+) coutPi:;for(j=0;jAllocationij;if(j=n)cout资源种类数匹配出现错误!;/当资源配置的种类数大于预先输入的数值时,出错Needij=MAXij-Allocationij;/需求数等于最大需求减去已经分配数 cout系统可用资源(Available):endl;for(j=0;jAvailablej;/输入各种资源的可利用

11、数cout当前时刻的进程分配情况如图:n;cout进程号-MAX-Allocation-Need-Available-n;/显示各进程的资源情况 for(i=0;im;i+) coutPi: ; for(j=0;jn;j+) cout MAXij; for(j=0;jn;j+) cout Allocationij; cout ; for(j=0;jn;j+) cout Needij; for(j=0;jn;j+) cout Availablej; coutendl;/回车换行/*银行家算法,为进程分配资源*/void banker() int i,j; int choice; while(1)

12、 coutendl; coutchoice; if(choice=1) /分配资源 cout从P0到Pm-1i; if(i=m) couti;/重新输入进程号 cout请输入进程申请的资源(Request):endl;for(j=0;jRequestij; /*银行家算法进行检查*/ for(j=0;jNeedij)coutAvailablej)/资源申请数目大于可利用数,无法分配,得等待cout当前系统可用资源不够,请等待!endl;continue; for(j=0;jn;j+)/资源申请得到允许时,变换各个资源数 Availablej=Availablej-Requestij; /可用资

13、源减少 Allocationij=Allocationij+Requestij;/所得资源增加 Needij=Needij-Requestij; /仍需资源减少if(safealg()0)/安全性算法的返回值cout分配不成功,请等待!;for (j=0;jn;j+) /把资源恢复成分配之前的状态 Availablej=Availablej+Requestij; Allocationij=Allocationij-Requestij; Needij=Needij+Requestij; for(i=0;im;i+) Finishi=False;/没有足够的资源分配给该进程/if(safealg(

14、)0) else cout同意分配请求!endl; for(j=0;jn;j+) Workj=Availablej; cout进程号-Work-Need-Allocation-Work+Allocation- Finish-endl; for(i=0;im;i+)/按模拟分配序列进行分配 cout进程PSequencei: ; for(j=0;jn;j+)coutWorkj ; cout ;for(j=0;jn;j+)coutNeedSequenceij ;cout ;for(j=0;jn;j+)coutAllocationSequenceij ;cout ;for(j=0;jn;j+)cou

15、tAllocationSequenceij+Workj ;cout ; coutFinishSequencei ;/完成该进程for(j=0;jn;j+)Workj=AllocationSequenceij+Workj;/回收该进程所分配的资源cout=0)/if(choice=1)else if(choice=2) /离开break; else cout请输入1或2!;/只认可1或2/while(1)/*安全性算法 */int safealg() int i,j,k,l=0; /int WorkMAXn; /工作组 /记录序列 for(i=0;in;i+) Worki=Availablei;

16、 /工作分配初始化为系统可用资源 for(i=0;im;i+) /扫描所有进程,预设所有进程不能运行 Finishi=False; for(i=0;im;i+) /if(Finishi=True) continue; else /对于未运行的进程,进行如下处理 / for(j=0;jn;j+)/找到一个满足Finishi=false且NeedijWorkj)/由于部分资源得不到满足,进程i无法运行break; if(j=n)/进程各类资源全部得到满足 Finishi=True; for(k=0;kn;k+)/进程i正常运行后,释放其占有的资源 Workk+=Allocationik; /工作分

17、配加上可用资源 Sequencel+=i; /模拟资源分配序列生成 i=-1; /重新扫描所有进程从i=0开始 else /某一资源得不到满足continue; /试探下一个进程待添加的隐藏文字内容2 /if(l=m)/都试探完毕cout系统安全!endl; cout安全序列:;for(i=0;il;i+) /输出安全序列cout进程PSequencei ;coutendl;return 0;/cout系统进入不安全状态!endl;return -1;分析:输入各进程的可利用资源Available,最大需求MAX,已分配资源Allocation ,需求资源Need,之后各系统发出资源请求Req

18、uest,利用实验中的安全性算法判断能否产生一个安全性队列,若能,则给该进程分配成功,否则,不予分配。在确定安全序列的过程中,要检测所有进程的Finishi的值,每次循环检测完后要重复从第一个进程开始。 七、运行结果 假设输入进程个数为5,资源种类数为3,并以此输入各进程初始时刻的各种资源数量 ,如下 若再继续申请资源,假设为P4 ,申请资源(1 2 2) 假设P1 申请资源(2 2 4)有 八、心得体会 经过这次操作系统课程设计,让我受益匪浅,收获颇多。主要体会如下:1.利用Vc+编译程序编写银行家算法,进一步理解到通过银行家算法避免死锁的思想,同时也理解了系统死锁产生的原因及条件。2.在实

19、验过程中所有的设计步骤遵循老师教授的程序功能化的思想,分别定义了三个函数,init()初始化函数,safealg()安全性算法函数,bank()银行家算法函数,体现了函数的模块化思想。这样的话,不仅提高了程序的可读性和可操作性,而且还提高了CPU的利用率和内存的利用率,因为程序的运行是局部性的,这种思想对于段页式存储管理系统尤为重要。3.实验过程中遇到的种种疑难问题通过自己上网查找答案,锻炼了自己纠错能力和搜索有价值信息的能力及自学的能力,并且进一步巩固了自己以前学过的专业知识。Employment tribunals sort out disagreements between employ

20、ers and employees.You may need to make a claim to an employment tribunal if: you dont agree with the disciplinary action your employer has taken against you your employer dismisses you and you think that you have been dismissed unfairly.For more information about dismissal and unfair dismissal, seeD

21、ismissal.You can make a claim to an employment tribunal, even if you haventappealedagainst the disciplinary action your employer has taken against you. However, if you win your case, the tribunal may reduce any compensation awarded to you as a result of your failure to appeal.Remember that in most c

22、ases you must make an application to an employment tribunal within three months of the date when the event you are complaining about happened. If your application is received after this time limit, the tribunal will not usually accept it.If you are worried about how the time limits apply to you, tak

23、e advice from one of the organisations listed underFurther help.Employment tribunals are less formal than some other courts, but it is still a legal process and you will need to give evidence under an oath or affirmation.Most people find making a claim to an employment tribunal challenging. If you a

24、re thinking about making a claim to an employment tribunal, you should get help straight away from one of the organisations listed underFurther help.If you are being represented by a solicitor at the tribunal, they may ask you to sign an agreement where you pay their fee out of your compensation if

25、you win the case. This is known as adamages-based agreement. In England and Wales, your solicitor cant charge you more than 35% of your compensation if you win the case.If you are thinking about signing up for a damages-based agreement, you should make sure youre clear about the terms of the agreeme

26、nt. It might be best to get advice from an experienced adviser, for example, at a Citizens Advice Bureau. To find your nearest CAB, including those that give advice by e-mail, click onnearest CAB.For more information about making a claim to an employment tribunal, seeEmployment tribunals.The (lack o

27、f) air up there Watch mCayman Islands-based Webb, the head of Fifas anti-racism taskforce, is in London for the Football Associations 150th anniversary celebrations and will attend Citys Premier League match at Chelsea on Sunday.I am going to be at the match tomorrow and I have asked to meet Yaya To

28、ure, he told BBC Sport.For me its about how he felt and I would like to speak to him first to find out what his experience was.Uefa hasopened disciplinary proceedings against CSKAfor the racist behaviour of their fans duringCitys 2-1 win.Michel Platini, president of European footballs governing body

29、, has also ordered an immediate investigation into the referees actions.CSKA said they were surprised and disappointed by Toures complaint. In a statement the Russian side added: We found no racist insults from fans of CSKA.Age has reached the end of the beginning of a word. May be guilty in his see

30、ms to passing a lot of different life became the appearance of the same day; May be back in the past, to oneself the paranoid weird belief disillusionment, these days, my mind has been very messy, in my mind constantly. Always feel oneself should go to do something, or write something. Twenty years

31、of life trajectory deeply shallow, suddenly feel something, do it.一字开头的年龄已经到了尾声。或许是愧疚于自己似乎把转瞬即逝的很多个不同的日子过成了同一天的样子;或许是追溯过去,对自己那些近乎偏执的怪异信念的醒悟,这些天以来,思绪一直很凌乱,在脑海中不断纠缠。总觉得自己自己似乎应该去做点什么,或者写点什么。二十年的人生轨迹深深浅浅,突然就感觉到有些事情,非做不可了。The end of our life, and can meet many things really do?而穷尽我们的一生,又能遇到多少事情是真正地非做不可?D

32、uring my childhood, think lucky money and new clothes are necessary for New Year, but as the advance of the age, will be more and more found that those things are optional; Junior high school, thought to have a crush on just means that the real growth, but over the past three years later, his writin

33、g of alumni in peace, suddenly found that isnt really grow up, it seems is not so important; Then in high school, think dont want to give vent to out your inner voice can be in the high school children of the feelings in a period, but was eventually infarction when graduation party in the throat, la

34、ter again stood on the pitch he has sweat profusely, looked at his thrown a basketball hoops, suddenly found himself has already cant remember his appearance.童年时,觉得压岁钱和新衣服是过年必备,但是随着年龄的推进,会越来越发现,那些东西根本就可有可无;初中时,以为要有一场暗恋才意味着真正的成长,但三年过去后,自己心平气和的写同学录的时候,突然就发现是不是真正的成长了,好像并没有那么重要了;然后到了高中,觉得非要吐露出自己的心声才能为高中

35、生涯里的懵懂情愫划上一个句点,但毕业晚会的时候最终还是被梗塞在了咽喉,后来再次站在他曾经挥汗如雨的球场,看着他投过篮球的球框时,突然间发现自己已经想不起他的容颜。Originally, this world, can produce a chemical reaction to an event, in addition to resolutely, have to do, and time.原来,这个世界上,对某个事件能产生化学反应的,除了非做不可的坚决,还有,时间。A persons time, your ideas are always special to clear. Want, w

36、ant, line is clear, as if nothing could shake his. Also once seemed to be determined to do something, but more often is he backed out at last. Dislike his cowardice, finally found that there are a lot of love, there are a lot of miss, like shadow really have been doomed. Those who do, just green yea

37、rs oneself give oneself an arm injection, or is a self-righteous spiritual.一个人的时候,自己的想法总是特别地清晰。想要的,不想要的,界限明确,好像没有什么可以撼动自己。也曾经好像已经下定了决心去做某件事,但更多的时候是最后又打起了退堂鼓。嫌恶过自己的怯懦,最终却发现有很多缘分,有很多错过,好像冥冥之中真的已经注定。那些曾经所谓的非做不可,只是青葱年华里自己给自己注射的一支强心剂,或者说,是自以为是的精神寄托罢了。At the moment, the sky is dark, the air is fresh facto

38、r after just rained. Suddenly thought of blue plaid shirt; Those were broken into various shapes of stationery; From the corner at the beginning of deep friendship; Have declared the end of the encounter that havent start planning. Those years, those days of do, finally, like youth, will end in our

39、life.此刻,天空是阴暗的,空气里有着刚下过雨之后的清新因子。突然想到那件蓝格子衬衫;那些被折成各种各样形状的信纸;那段从街角深巷伊始的友谊;还有那场还没有开始就宣告了终结的邂逅计划那些年那些天的非做不可,终于和青春一样,都将在我们的人生中谢幕。Baumgartner the disappointing news: Mission aborted. r plays an important role in this mission. Starting at the ground, conditions have to be very calm - winds less than 2 mph, with no precipitation or humidity and limited cloud cover. The balloon, with capsule attached, will move through the lower level of the atmosphere (the troposphere) where our day-to-day weather lives. It will climb higher than the tip of Mount Everest (5.5 miles/8.85 kilometers), drif

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公文档 > 其他范文


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号