操作系统实验报告 死锁的避免.docx
- 文档编号:29320932
- 上传时间:2023-07-22
- 格式:DOCX
- 页数:16
- 大小:312.21KB
操作系统实验报告 死锁的避免.docx
《操作系统实验报告 死锁的避免.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告 死锁的避免.docx(16页珍藏版)》请在冰豆网上搜索。
操作系统实验报告死锁的避免
操作系统实验
(二)死锁的避免
1.实验内容
使用C++实现模拟随机算法和银行家算法
2.实验目的
(1)了解死锁的产生原因(随机算法)
(2)理解死锁的解决办法(银行家算法)
3.实验题目
使用随机算法和银行家算法设计程序
4.程序流程图
主要过程流程图
银行家算法流程图
安全性算法流程图
5.程序代码和运行结果
#include<>
#include<>
typedefstruct
{
intA;
intB;
intC;
}RES;
#definefalse0
#definetrue1
&Max[i].B,&Max[i].C);
}
&Allocation[i].B,&Allocation[i].C);
}
&Need[i].B,&Need[i].C);
}
printf("================结束配置资源==================\n");
}
voidloadConfig()
{
FILE*fp1;
if((fp1=fopen("","r"))==NULL)
{
printf("没有发现配置文件,请手动输入!
!
!
\n");
setConfig();
}
else{
inti=0;
printf("发现配置文件,开始导入..\n");
&Max[i].B,&Max[i].C);
}
&Allocation[i].B,&Allocation[i].C);
}
&Need[i].B,&Need[i].C);
}
}
}
+=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;
}
-=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;
}
<=&&Need[i].B<=&&Need[i].C<=
{
;
+=Allocation[i].B;
+=Allocation[i].C;
Finish[i]=true;
safe[j++]=i;
i=-1;&&res->B<=Need[process].B&&res->C<=Need[process].C)
{
.\n");
RollBack(process,res);
}
}
else
{
printf("安全性检查失败。
原因:
请求大于可利用资源。
\n");
}
}
else
{
printf("安全性检查失败。
原因:
请求大于需求。
\n");
}
returnfalse;
}
Max[0].B,Max[0].C,Allocation[0].A,Allocation[0].B,Allocation[0].C,Need[0].A,Need[0].B,Need[0].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");
}
.\n");
printf("执行完毕。
\n");
getchar();
return;
}
0.1.2.)\n");
scanf("%d%d%d%d",&process,&,&,&;
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");
}
<=Allocation[process].A&&Max[process].B<=Allocation[process].B&&Max[process].C<=Allocation[process].C)
{
printf("\nP%d执行完毕,释放所分配的资源...\n",process);
;
+=Allocation[process].B;
+=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;
}
.\n");
printf("执行完毕。
\n");
getchar();
return;
}
0.1.2.)\n");
scanf("%d%d%d%d",&process,&,&,&;
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:
{
...\n");
getchar();
};break;
case2:
banker();break;
case3:
randPatch();break;
case0:
printf(“退出系统.\n\n”);return0;break;
default:
printf("请输入0~1之间的数字\n");
}
}
return0;
}
/*
555
753
322
902
010
200
302
743
122
600*/
6.实验心得
多个进程同时运行时,系统根据各类系统资源的最大需求和各类系统的剩余资源为进程安排安全序列,使得系统能快速且安全地运行进程,不至发生死锁。
银行家算法是避免死锁的主要方法,其思路在很多方面都非常值得我们来学习借鉴。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统实验报告 死锁的避免 操作系统 实验 报告 死锁 避免