欢迎来到天天文库
浏览记录
ID:20422244
大小:613.30 KB
页数:16页
时间:2018-10-10
《操作系统实验报告死锁的避免》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、操作系统实验(二)死锁的避免1.实验内容使用C++实现模拟随机算法和银行家算法2.实验目的(1)丫解死锁的产生原因(随机算法)(2)理解死锁的解决办法(银行家算法)3.实验题目使用随机算法和银行家算法设计程序4.程序流程图主要过程流程图进入系统]进行初始化■丁丨打印输出此时刻资源分配怙况提出中请资源请求I实阽分紀并打印输出退出系统安全性算法流程图5.程序代码和运行结果#include#includetypedefstruct{intA;intB;intC;}RES;#definefalse0#definetrue1//系统中所冇进程数量#definePN
2、UMBER3//最大需求矩阵RESMax[PNUMBER];//己分配资源数矩阵RESAllocation[PNUMBER];//需求矩阵RESNeed[PNUMBER];//可用资源向量RESAvailable={0,0,0};//安全序列intsafe[PNUMBER];voidsetConfigO{inti=0,j=0;printf(”================开始手动配置资源==================”);//可分配资源printf("输入可分配资源”);scanf("%d%d%d",&Available.A,&Available.B,&Available.C
3、);//最大需求矩阵MAXprintff输入最大需求矩阵。/odx%d’’,PNUMBER,PNUMBER);for(i=O;i4、,??41;]18£1<,?1;1^£1<);for(i=O;i5、阵。/odx%d’’,PNUMBER,PNUMBER);for(i=O;i6、ble.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=O;i7、&Need[i].C);}//试探分配voidProbeAlloc(intprocess,RES*res){Available.A-=res->A;Available.B-=res->B;Available.Cres-〉C;Allocation[process].A+=res-〉A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Needfprocess].A-=res-〉A;Need[process].B-=res->B;Need[process].C•=res->C;}//若试探分配后进入不安全状态,将分配回滚voi8、dRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res-〉B;Available.C+=res-〉C;Allocation[process].A-=res-〉A;Allocation[process].B-=res-〉B;A1location[process].C-=res->C;Need[process]A;Ne
4、,??41;]18£1<,?1;1^£1<);for(i=O;i5、阵。/odx%d’’,PNUMBER,PNUMBER);for(i=O;i6、ble.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=O;i7、&Need[i].C);}//试探分配voidProbeAlloc(intprocess,RES*res){Available.A-=res->A;Available.B-=res->B;Available.Cres-〉C;Allocation[process].A+=res-〉A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Needfprocess].A-=res-〉A;Need[process].B-=res->B;Need[process].C•=res->C;}//若试探分配后进入不安全状态,将分配回滚voi8、dRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res-〉B;Available.C+=res-〉C;Allocation[process].A-=res-〉A;Allocation[process].B-=res-〉B;A1location[process].C-=res->C;Need[process]A;Ne
5、阵。/odx%d’’,PNUMBER,PNUMBER);for(i=O;i6、ble.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=O;i7、&Need[i].C);}//试探分配voidProbeAlloc(intprocess,RES*res){Available.A-=res->A;Available.B-=res->B;Available.Cres-〉C;Allocation[process].A+=res-〉A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Needfprocess].A-=res-〉A;Need[process].B-=res->B;Need[process].C•=res->C;}//若试探分配后进入不安全状态,将分配回滚voi8、dRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res-〉B;Available.C+=res-〉C;Allocation[process].A-=res-〉A;Allocation[process].B-=res-〉B;A1location[process].C-=res->C;Need[process]A;Ne
6、ble.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=O;i7、&Need[i].C);}//试探分配voidProbeAlloc(intprocess,RES*res){Available.A-=res->A;Available.B-=res->B;Available.Cres-〉C;Allocation[process].A+=res-〉A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Needfprocess].A-=res-〉A;Need[process].B-=res->B;Need[process].C•=res->C;}//若试探分配后进入不安全状态,将分配回滚voi8、dRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res-〉B;Available.C+=res-〉C;Allocation[process].A-=res-〉A;Allocation[process].B-=res-〉B;A1location[process].C-=res->C;Need[process]A;Ne
7、&Need[i].C);}//试探分配voidProbeAlloc(intprocess,RES*res){Available.A-=res->A;Available.B-=res->B;Available.Cres-〉C;Allocation[process].A+=res-〉A;Allocation[process].B+=res->B;Allocation[process].C+=res->C;Needfprocess].A-=res-〉A;Need[process].B-=res->B;Need[process].C•=res->C;}//若试探分配后进入不安全状态,将分配回滚voi
8、dRollBack(intprocess,RES*res){Available.A+=res->A;Available.B+=res-〉B;Available.C+=res-〉C;Allocation[process].A-=res-〉A;Allocation[process].B-=res-〉B;A1location[process].C-=res->C;Need[process]A;Ne
此文档下载收益归作者所有