




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实 验 报 告系 别班 级学 号姓 名时间地点计算机科学系软件14016.25课程名称操作系统实验名称死锁避免银行家算法实 验 过 程一实验目的1、掌握死锁产生的原因和必要条件。2、掌握银行家算法的实现二实验原理1、银行家算法之主体算法:1) 进程Pi发出资源请求Requesti2) 若非RequestiNeedi,出错返回3) 若非Requesti Available,则应使Pi等待并返回4) 系统试探性地满足Pi请求,并作以下修改: Available = Available Requesti Allocationi = Allocationi + Requesti Needi = Nee
2、di Requesti5) 系统调用安全性算法进行资源分配检查,若安全则执行分配,否则恢复试探分配前状态,并使Pi等待。2、 银行家算法之安全性子算法:1) 令Work = Available, Finish=FALSE2) 从进程集合中查找一个满足Finishi=FALSE且 Needi <= Work 的进程Pi 。若找到,则可假定Pi能获得所需资源并顺利执行,故有: Work = Work + Allocationi Finishi = True 然后重复执行第2步;否则转至第3步执行3) 如果Finish=TRUE,则表示系统处于安全状态;否则系统处于不安全状态三实验内容1、创建
3、C语言工程项目,按照教材上的有关说明,定义相应的数据结构。 int AllocMatrix54 / 已经分配资源矩阵 int RequestMatrix54 / 需求矩阵 int AvailResource4 / 可用资源向量 int TryProcess5 / 尝试序列2、给各个数据结构设定合适的初始值。 按照教材课后习题31的内容给上述数据结构设定初始值。 如:int AllocMatrix54=/ 已经分配资源矩阵0,0,3,2,1,0,0,0,1,3,5,4,0,3,3,2,0,0,1,4; 注意:步骤1、2可同时进行,即利用C语言中的定义变量就可同时初始化的方式进行数值初设。 3、依
4、据银行家算法的描述依次进行资源的试探性分配,直至成功或失败,成功则说明当前状态是安全的;失败后,还应该将资源回到初始状态,并进行另一次试探;只有所有的试探都失败了,才能说明当前状态是不安全的。 通常,这种试探性算法采用递归的方法是很合适的,程序也是很简洁的。 代码片段: if( AvailResource0 >= RequestMatrixk0&& AvailResource1 >= RequestMatrixk1&& AvailResource2 >= RequestMatrixk2&& AvailResource3 >
5、= RequestMatrixk3)/若资源能使进程ProcessID结束 for(int i=0;i<4;i+)AvailResourcei += AllocMatrixki;/ 恢复资源 int SearchSecurity(int level) /递归函数 if(level = 4)elseSearchSecurity(level+1); / 有条件递归调用自己 其余由大家自行完成。四实验程序#include "malloc.h"#include "stdio.h"#include "stdlib.h"#define al
6、loclen sizeof(struct allocation)#define maxlen sizeof(struct max)#define avalen sizeof(struct available)#define needlen sizeof(struct need)#define finilen sizeof(struct finish)#define pathlen sizeof(struct path)struct allocationint value;struct allocation *next;struct maxint value;struct max *next;s
7、truct available /*可用资源数*/int value;struct available *next;struct need /*需求资源数*/int value;struct need *next;struct pathint value;struct path *next;struct finishint stat;struct finish *next;int main()int row,colum,status=0,i,j,t,temp,processtest;struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
8、struct max *maxhead,*maxium1,*maxium2,*maxtemp;struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;struct need *needhead,*need1,*need2,*needtemp;struct finish *finihead,*finish1,*finish2,*finishtemp;struct path *pathhead,*path1,*path2;printf("n请输入系统资源的
9、种类数:");scanf("%d",&colum);printf("请输入现时内存中的进程数:");scanf("%d",&row);printf("请输入已分配资源矩阵:n");for(i=0;i<row;i+)for (j=0;j<colum;j+)printf("请输入已分配给进程 p%d 的 %c 种系统资源:",i,'A'+j);if(status=0)allochead=alloc1=alloc2=(struct allocati
10、on*)malloc(alloclen);alloc1->next=alloc2->next=NULL;scanf("%d",&allochead->value);status+;elsealloc2=(struct allocation *)malloc(alloclen);scanf("%d,%d",&alloc2->value);if(status=1)allochead->next=alloc2;status+;alloc1->next=alloc2;alloc1=alloc2;alloc2-&
11、gt;next=NULL;status=0;printf("请输入最大需求矩阵:n");for(i=0;i<row;i+)for (j=0;j<colum;j+)printf("请输入进程 p%d 种类 %c 系统资源最大需求:",i,'A'+j);if(status=0)maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);maxium1->next=maxium2->next=NULL;scanf("%d",&maxium1->
12、value);status+;elsemaxium2=(struct max *)malloc(maxlen);scanf("%d,%d",&maxium2->value);if(status=1)maxhead->next=maxium2;status+;maxium1->next=maxium2;maxium1=maxium2;maxium2->next=NULL;status=0;printf("请输入现时系统剩余的资源矩阵:n");for (j=0;j<colum;j+)printf("种类 %c
13、的系统资源剩余:",'A'+j);if(status=0)avahead=available1=available2=(struct available*)malloc(avalen);workhead=work1=work2=(struct available*)malloc(avalen);available1->next=available2->next=NULL;work1->next=work2->next=NULL;scanf("%d",&available1->value);work1->v
14、alue=available1->value;status+;elseavailable2=(struct available*)malloc(avalen);work2=(struct available*)malloc(avalen);scanf("%d,%d",&available2->value);work2->value=available2->value;if(status=1)avahead->next=available2;workhead->next=work2;status+;available1->ne
15、xt=available2;available1=available2;work1->next=work2;work1=work2;available2->next=NULL;work2->next=NULL;status=0;alloctemp=allochead;maxtemp=maxhead;for(i=0;i<row;i+)for (j=0;j<colum;j+)if(status=0)needhead=need1=need2=(struct need*)malloc(needlen);need1->next=need2->next=NULL;
16、need1->value=maxtemp->value-alloctemp->value;status+;elseneed2=(struct need *)malloc(needlen);need2->value=(maxtemp->value)-(alloctemp->value);if(status=1)needhead->next=need2;status+;need1->next=need2;need1=need2;maxtemp=maxtemp->next;alloctemp=alloctemp->next;need2-&g
17、t;next=NULL;status=0;for(i=0;i<row;i+)if(status=0)finihead=finish1=finish2=(struct finish*)malloc(finilen);finish1->next=finish2->next=NULL;finish1->stat=0;status+;elsefinish2=(struct finish*)malloc(finilen);finish2->stat=0;if(status=1)finihead->next=finish2;status+;finish1->nex
18、t=finish2;finish1=finish2;finish2->next=NULL; /*Initialization compleated*/status=0;processtest=0;for(temp=0;temp<row;temp+)alloctemp=allochead;needtemp=needhead;finishtemp=finihead;worktemp=workhead;for(i=0;i<row;i+)worktemp1=worktemp;if(finishtemp->stat=0)for(j=0;j<colum;j+,needtemp
19、=needtemp->next,worktemp=worktemp->next)if(needtemp->value<=worktemp->value)processtest+;if(processtest=colum)for(j=0;j<colum;j+)worktemp1->value+=alloctemp->value;worktemp1=worktemp1->next;alloctemp=alloctemp->next;if(status=0)pathhead=path1=path2=(struct path*)malloc(pathlen);path1->next=path2->next=NULL;path1->value=i;status+;elsepath2=(struct path*)malloc(pathlen);path2->value=i;if(status=1)pathhead->next=path2;status+;path1->next=path2;path1=path2;finishtemp->stat=1;elsef
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年嘉兴南湖学院单招职业适应性测试题库汇编
- 科技教育与孩子成长网络的双重作用
- 十月革命的胜利与苏联的社会主义实践课件-2024-2025学年高一下统编版(2019)必修中外历史纲要下
- 科技企业如何应对伦理问题挑战
- 2025年湖北省荆门市单招职业倾向性测试题库完美版
- 科技助力提升药膳选择的专业性
- 电影文化展览馆的展陈与观演系统
- 2025年鹤岗师范高等专科学校单招职业技能测试题库审定版
- 科技展会营销实战线上线下双轮驱动策略
- 2025年衡阳科技职业学院单招职业技能测试题库完整版
- 2024四川省凉山州林业局招聘60人历年(高频重点复习提升训练)共500题附带答案详解
- DL∕T 5106-2017 跨越电力线路架线施工规程
- 西师大版数学四年级下册全册教学课件(2024年3月修订)
- 绿化养护服务投标方案(技术标)
- 九年级物理第一课
- 代孕合同范本
- 医疗事故处理条例解读专家讲座
- 心电监护仪的使用幻灯片
- 全年无休供货承诺书
- 博物馆跨界合作的趋势与挑战
- 宁夏银川三中2023-2024学年九年级上学期期末物理试卷
评论
0/150
提交评论