操作系统实验报告死锁的避免.docx
《操作系统实验报告死锁的避免.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告死锁的避免.docx(24页珍藏版)》请在冰豆网上搜索。
操作系统实验报告死锁的避免
操作系统实验
(二)死锁的避免
1.实验内容
使用C++实现模拟随机算法和银行家算法
2.实验目的
(1)了解死锁的产生原因(随机算法)
(2)理解死锁的解决办法(银行家算法)
3.实验题目
使用随机算法和银行家算法设计程序
4.程序流程图
主要过程流程图
银行家算法流程图
安全性算法流程图
5.程序代码和运行结果
#include
typedefstruct
{
intA;
intB;
intC;
}RES;
#definefalse0
#definetrue1
//系统中所有进程数量
#definePNUMBER3
//最大需求矩阵
RESMax[PNUMBER];
//已分配资源数矩阵
RESAllocation[PNUMBER];
//需求矩阵
RESNeed[PNUMBER];
//可用资源向量
RESAvailable={0,0,0};
//安全序列
intsafe[PNUMBER];
voidsetConfig()
inti=0,j=0;
printf("================开始手动配置资源==================\n");
//可分配资源
printf("输入可分配资源\n");
scanf("%d%d%d",&Available.A,&Available.B,&Available.C);
//最大需求矩阵MAX
printf("输入最大需求矩阵%dx%d\n",PNUMBER,PNUMBER);
for(i=0;i{scanf("%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);}//已分配矩阵Allocprintf("输入已分配矩阵%dx%d\n",PNUMBER,PNUMBER);for(i=0;i{scanf("%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵printf("输入需求矩阵%dx%d\n",PNUMBER,PNUMBER);for(i=0;i{scanf("%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}printf("================结束配置资源==================\n");}voidloadConfig(){FILE*fp1;if((fp1=fopen("config.txt","r"))==NULL){printf("没有发现配置文件,请手动输入!!!\n");setConfig();}else{inti=0;printf("发现配置文件,开始导入..\n");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i{fscanf(fp1,"%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);}//已分配矩阵Allocfor(i=0;i{fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
scanf("%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);
}
//已分配矩阵Alloc
printf("输入已分配矩阵%dx%d\n",PNUMBER,PNUMBER);
for(i=0;i{scanf("%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵printf("输入需求矩阵%dx%d\n",PNUMBER,PNUMBER);for(i=0;i{scanf("%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}printf("================结束配置资源==================\n");}voidloadConfig(){FILE*fp1;if((fp1=fopen("config.txt","r"))==NULL){printf("没有发现配置文件,请手动输入!!!\n");setConfig();}else{inti=0;printf("发现配置文件,开始导入..\n");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i{fscanf(fp1,"%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);}//已分配矩阵Allocfor(i=0;i{fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
scanf("%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);
printf("输入需求矩阵%dx%d\n",PNUMBER,PNUMBER);
for(i=0;i{scanf("%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}printf("================结束配置资源==================\n");}voidloadConfig(){FILE*fp1;if((fp1=fopen("config.txt","r"))==NULL){printf("没有发现配置文件,请手动输入!!!\n");setConfig();}else{inti=0;printf("发现配置文件,开始导入..\n");//可分配资源fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);//最大需求矩阵MAXfor(i=0;i{fscanf(fp1,"%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);}//已分配矩阵Allocfor(i=0;i{fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
scanf("%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);
printf("================结束配置资源==================\n");
voidloadConfig()
FILE*fp1;
if((fp1=fopen("config.txt","r"))==NULL)
printf("没有发现配置文件,请手动输入!
!
\n");
setConfig();
else{
inti=0;
printf("发现配置文件,开始导入..\n");
fscanf(fp1,"%d%d%d",&Available.A,&Available.B,&Available.C);
for(i=0;i{fscanf(fp1,"%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);}//已分配矩阵Allocfor(i=0;i{fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
fscanf(fp1,"%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C);
for(i=0;i{fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);}//需求矩阵for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
fscanf(fp1,"%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C);
for(i=0;i{fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);}}}//试探分配voidProbeAlloc(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;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-=res->B;Allocation[process].C-=res->C;Need[process].A+=res->A;Need[process].B+=res->B;Need[process].C+=res->C;}//安全性检查boolSafeCheck(){RESWork;Work.A=Available.A;Work.B=Available.B;Work.C=Available.C;boolFinish[PNUMBER]={false,false,false};inti;intj=0;for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
fscanf(fp1,"%d%d%d",&Need[i].A,&Need[i].B,&Need[i].C);
//试探分配
voidProbeAlloc(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;
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-=res->B;
Allocation[process].C-=res->C;
Need[process].A+=res->A;
Need[process].B+=res->B;
Need[process].C+=res->C;
//安全性检查
boolSafeCheck()
RESWork;
Work.A=Available.A;
Work.B=Available.B;
Work.C=Available.C;
boolFinish[PNUMBER]={false,false,false};
inti;
intj=0;
for(i=0;i{//是否已检查过if(Finish[i]==false){//是否有足够的资源分配给该进程if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C){//有则使其执行完成,并将已分配给该进程的资源全部回收Work.A+=Allocation[i].A;Work.B+=Allocation[i].B;Work.C+=Allocation[i].C;Finish[i]=true;safe[j++]=i;i=-1;//重新进行遍历}}}//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
//是否已检查过
if(Finish[i]==false)
//是否有足够的资源分配给该进程
if(Need[i].A<=Work.A&&Need[i].B<=Work.B&&Need[i].C<=Work.C)
//有则使其执行完成,并将已分配给该进程的资源全部回收
Work.A+=Allocation[i].A;
Work.B+=Allocation[i].B;
Work.C+=Allocation[i].C;
Finish[i]=true;
safe[j++]=i;
i=-1;//重新进行遍历
//如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态
for(i=0;i{if(Finish[i]==false){returnfalse;}}returntrue;}//资源分配请求boolrequest(intprocess,RES*res){//request向量需小于Need矩阵中对应的向量if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//如果安全检查成立,则请求成功,否则将分配回滚并返回失败if(SafeCheck()){returntrue;}else{printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");printf("正在回滚...\n");RollBack(process,res);}}else{printf("安全性检查失败。原因:请求大于可利用资源。\n");}}else{printf("安全性检查失败。原因:请求大于需求。\n");}returnfalse;}//输出资源分配表voidPrintTable(){printf("===================================资源分配表==================================\n");printf("ProcessMaxAllocationNeedAvailable\n");printf("ABCABCABCABC\n");printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);printf("===============================================================================\n");}//银行家算法分配voidbanker(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(process<3&&process>=0){if(request(process,&res)){printf("分配成功。\n");PrintTable();printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}else{printf("输入的进程号0~2\n");ch='y';}}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}//随机分配算法执行boolRandRequest(intprocess,RES*res){//request向量需小于Available向量if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C){//试探分配ProbeAlloc(process,res);//判断进程是否执行完,执行完释放资源if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C){printf("\nP%d执行完毕,释放所分配的资源...\n",process);//有则使其执行完成,并将已分配给该进程的资源全部回收Available.A+=Allocation[process].A;Available.B+=Allocation[process].B;Available.C+=Allocation[process].C;Allocation[process].A=0;Allocation[process].B=0;Allocation[process].C=0;Need[process].A=Max[process].A;Need[process].B=Max[process].B;Need[process].C=Max[process].C;}returntrue;}else{printf("分配失败。原因:请求大于可利用资源。\n");}returnfalse;}//随机分配voidrandPatch(){charch;//判断输入的是否是安全状态PrintTable();printf("先检查初始状态是否安全。\n");if(SafeCheck()){printf("系统处于安全状态。\n");printf("安全序列是{P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2]);}else{printf("系统处于不安全状态。程序将退出...\n");printf("执行完毕。\n");getchar();return;}//开始分配do{intprocess;RESres;printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);if(RandRequest(process,&res)){printf("分配成功。\n");PrintTable();if(!SafeCheck()){printf("系统发生死锁。");getchar();getchar();break;}}else{printf("分配失败。\n");}printf("是否继续分配?(Y/N):");getchar();ch=getchar();}while(ch=='Y'||ch=='y');printf("执行完毕。\n");}intmain(){intx;while(1){printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法\n");printf("===============================================================================\n");printf("\t\t\t按1.导入配置信息\n");printf("\t\t\t按2.银行家算法\n");printf("\t\t\t按3.随机分配算法\n");printf("\t\t\t按0.退出系统\n");printf("===============================================================================\n");printf("您输入的是:");scanf("%d",&x);fflush(stdin);system("cls");printf("===============================================================================\n");printf("\t\t\t共享资源分配与银行家算法");if(x==2){printf("\t---银行家算法\n");}elseif(x==3){printf("\t---随机分配算法\n");}printf("===============================================================================\n");switch(x){case1:{//加载配置文件loadConfig();//打印资源分配表PrintTable();printf("信息导入完成.....\n");getchar();};break;case2:banker();break;case3:randPatch();break;case0:printf(“退出系统.\n\n”);return0;break;default:printf("请输入0~1之间的数字\n");}}return0;}/*Config.txt555753322902010200302743122600*/6.实验心得多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
returnfalse;
returntrue;
//资源分配请求
boolrequest(intprocess,RES*res)
//request向量需小于Need矩阵中对应的向量
if(res->A<=Need[process].A&&res->B<=Need[process].B&&res->C<=Need[process].C)
//request向量需小于Available向量
if(res->A<=Available.A&&res->B<=Available.B&&res->C<=Available.C)
ProbeAlloc(process,res);
//如果安全检查成立,则请求成功,否则将分配回滚并返回失败
if(SafeCheck())
else
printf("安全性检查失败。
原因:
系统将进入不安全状态,有可能引起死锁。
printf("正在回滚...\n");
RollBack(process,res);
请求大于可利用资源。
请求大于需求。
//输出资源分配表
voidPrintTable()
printf("===================================资源分配表==================================\n");
printf("ProcessMaxAllocationNeedAvailable\n");
printf("ABCABCABCABC\n");
printf("P0%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[0].A,Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].C,Available.A,Available.B,Available.C);
printf("P1%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[1].A,Max[1].B,Max[1].C,Allocation[1].A,Allocation[1].B,Allocation[1].C,Need[1].A,Need[1].B,Need[1].C);
printf("P2%2d%2d%2d%2d%2d%2d%2d%2d%2d\n",Max[2].A,Max[2].B,Max[2].C,Allocation[2].A,Allocation[2].B,Allocation[2].C,Need[2].A,Need[2].B,Need[2].C);
printf("===============================================================================\n");
//银行家算法分配
voidbanker()
charch;
//判断输入的是否是安全状态
PrintTable();
printf("先检查初始状态是否安全。
printf("系统处于安全状态。
printf("安全序列是{P%d,P%d,P%d}。
\n",safe[0],safe[1],safe[2]);
printf("系统处于不安全状态。
程序将退出...\n");
printf("执行完毕。
getchar();
return;
//开始分配
do
intprocess;
RESres;
printf("请依次输入请求分配的进程和对三类资源的请求数量(进程编号0.1.2...)\n");
scanf("%d%d%d%d",&process,&res.A,&res.B,&res.C);
if(process<3&&process>=0){
if(request(process,&res))
printf("分配成功。
printf("分配失败。
printf("是否继续分配?
(Y/N):
");
ch=getchar();
}else
printf("输入的进程号0~2\n");
ch='y';
}while(ch=='Y'||ch=='y');
//随机分配算法执行
boolRandRequest(intprocess,RES*res)
//判断进程是否执行完,执行完释放资源
if(Max[process].A<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C)
printf("\nP%d执行完毕,释放所分配的资源...\n",process);
Available.A+=Allocation[process].A;
Available.B+=Allocation[process].B;
Available.C+=Allocation[process].C;
Allocation[process].A=0;
Allocation[process].B=0;
Allocation[process].C=0;
Need[process].A=Max[process].A;
Need[process].B=Max[process].B;
Need[process].C=Max[process].C;
//随机分配
voidrandPatch()
if(RandRequest(process,&res))
if(!
SafeCheck())
printf("系统发生死锁。
break;
intmain()
intx;
while
(1)
printf("\t\t\t共享资源分配与银行家算法\n");
printf("\t\t\t按1.导入配置信息\n");
printf("\t\t\t按2.银行家算法\n");
printf("\t\t\t按3.随机分配算法\n");
printf("\t\t\t按0.退出系统\n");
printf("您输入的是:
scanf("%d",&x);
fflush(stdin);
system("cls");
printf("\t\t\t共享资源分配与银行家算法");
if(x==2)
printf("\t---银行家算法\n");
}elseif(x==3)
printf("\t---随机分配算法\n");
switch(x)
case1:
//加载配置文件
loadConfig();
//打印资源分配表
printf("信息导入完成.....\n");
};break;
case2:
banker();break;
case3:
randPatch();break;
case0:
printf(“退出系统.\n\n”);return0;break;
default:
printf("请输入0~1之间的数字\n");
return0;
/*Config.txt
555
753
322
902
010
200
302
743
122
600*/
6.实验心得
多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。
银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1