欢迎来到天天文库
浏览记录
ID:47620422
大小:396.50 KB
页数:16页
时间:2019-10-13
《操作系统实验报告材料死锁地避免》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、操作系统实验(二)死锁的避免1.实验内容使用C++实现模拟随机算法和银行家算法2.实验目的(1)了解死锁的产生原因(随机算法)(2)理解死锁的解决办法(银行家算法)3.实验题目使用随机算法和银行家算法设计程序4.程序流程图主要过程流程图银行家算法流程图安全性算法流程图5.程序代码和运行结果#include#includetypedefstruct{intA;intB;intC;}RES;#definefalse0#definetrue1//系统中所有进程数量#definePNUMBER3//最大需求矩阵RE
2、SMax[PNUMBER];//已分配资源数矩阵RESAllocation[PNUMBER];//需求矩阵RESNeed[PNUMBER];//可用资源向量RESAvailable={0,0,0};//安全序列intsafe[PNUMBER];voidsetConfig(){inti=0,j=0;printf("================开始手动配置资源==================");//可分配资源printf("输入可分配资源");scanf("%d%d%d",&Available.A,&Available.B,&Av
3、ailable.C);//最大需求矩阵MAXprintf("输入最大需求矩阵%dx%d",PNUMBER,PNUMBER);for(i=0;i4、);}//需求矩阵printf("输入需求矩阵%dx%d",PNUMBER,PNUMBER);for(i=0;i5、Config();}else{inti=0;printf("发现配置文件,开始导入..");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i6、llocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;iA;Available.B-=res->B;Available.C-=res->C;Allocation[process].A+=res->A;Allocation[process].B+=r7、es->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}//若试探分配后进入不安全状态,将分配回滚voidRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res->B;Available.C+=res->C;Allocation[process].A-=res->A;Allocation[process].B-8、=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process]
4、);}//需求矩阵printf("输入需求矩阵%dx%d",PNUMBER,PNUMBER);for(i=0;i5、Config();}else{inti=0;printf("发现配置文件,开始导入..");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i6、llocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;iA;Available.B-=res->B;Available.C-=res->C;Allocation[process].A+=res->A;Allocation[process].B+=r7、es->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}//若试探分配后进入不安全状态,将分配回滚voidRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res->B;Available.C+=res->C;Allocation[process].A-=res->A;Allocation[process].B-8、=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process]
5、Config();}else{inti=0;printf("发现配置文件,开始导入..");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i6、llocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;iA;Available.B-=res->B;Available.C-=res->C;Allocation[process].A+=res->A;Allocation[process].B+=r7、es->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}//若试探分配后进入不安全状态,将分配回滚voidRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res->B;Available.C+=res->C;Allocation[process].A-=res->A;Allocation[process].B-8、=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process]
6、llocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;iA;Available.B-=res->B;Available.C-=res->C;Allocation[process].A+=res->A;Allocation[process].B+=r
7、es->B;Allocation[process].C+=res->C;Need[process].A-=res->A;Need[process].B-=res->B;Need[process].C-=res->C;}//若试探分配后进入不安全状态,将分配回滚voidRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res->B;Available.C+=res->C;Allocation[process].A-=res->A;Allocation[process].B-
8、=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process]
此文档下载收益归作者所有