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