银行家算法模拟 使用 C++ 实现

爱被打了一巴掌 2022-12-30 12:00 98阅读 0赞

大三的学生,老师留的作业。各位小伙伴看看就好。代码我也不懂啥意思,就自己看着吧。能运行就是了。(代码运行的环境是在 dev 下)
在这里插入图片描述

  1. #include "string.h"
  2. #include <iostream>
  3. using namespace std;
  4. #define M 5 //M个进程数
  5. #define N 3 //N类资源数
  6. #define FALSE 0
  7. #define TRUE 1
  8. //M个进程对N类资源最大资源需求量
  9. int MAX[M][N]={ { 7,5,3},{ 3,2,2},{ 9,0,2},{ 2,2,2},{ 4,3,3}};
  10. //系统可用资源数
  11. int AVAILABLE[N]={ 10,5,7};
  12. //M个进程已经得到N类资源的资源量
  13. int ALLOCATION[M][N]={ { 0,0,0},{ 0,0,0},{ 0,0,0},{ 0,0,0},{ 0,0,0}};
  14. //M个进程尚需要N类资源的资源量
  15. int NEED[M][N]={ { 7,5,3},{ 3,2,2},{ 9,0,2},{ 2,2,2},{ 4,3,3}};
  16. int Request[N]={ 0,0,0};
  17. int main()
  18. { int i=0,j=0;
  19. char flag='Y';
  20. int tflag=0;
  21. void showdata();
  22. void changdata(int);
  23. void rstordata(int);
  24. int chkerr(int);
  25. showdata();
  26. while(flag=='Y'||flag=='y')
  27. { i=-1;
  28. while(i<0||i>=M)
  29. { cout<<" 请输入需申请资源的进程号(从0到"<<M-1<<",否则重输入!):";
  30. cin>>i;
  31. if(i<0||i>=M)cout<<" 输入的进程号不存在,重新输入!"<<endl; }
  32. cout<<" 请输入进程"<<i<<"申请的资源数"<<endl;
  33. for (j=0;j<N;j++) { cout<<" 资源"<<j<<": "; cin>>Request[j];
  34. if(Request[j]>NEED[i][j])
  35. { cout<<" 进程"<<i<<"申请的资源数大于进程"<<i<<"还需要"<<j<<"类资源的资源量!";
  36. cout<<"申请不合理,出错!请重新选择!"<<endl<<endl;
  37. flag='N';
  38. break; }
  39. else
  40. { if(Request[j]>AVAILABLE[j])
  41. { cout<<" 进程"<<i<<"申请的资源数大于系统可用"<<j<<"类资源的资源量!";
  42. cout<<"申请不合理,出错!请重新选择!"<<endl<<endl; flag='N'; break; }
  43. }
  44. }
  45. if(flag=='Y'||flag=='y')
  46. { changdata(i);
  47. if(chkerr(i)) { rstordata(i); showdata(); }
  48. else showdata();
  49. }
  50. else showdata();
  51. for (j=0;j<N;j++)
  52. if (NEED[i][j]==0) tflag=1;
  53. else tflag=0;
  54. if (tflag==1 )
  55. { for (j=0;j<N;j++) AVAILABLE[j]=AVAILABLE[j]+MAX[i][j];
  56. cout<<"第i个资源" <<MAX[i][j];}
  57. cout<<endl;
  58. cout<<" 是否继续银行家算法演示,按'Y'或'y'键继续,按'N'或'n'键退出演示: ";
  59. cin>>flag;
  60. cout<<endl;
  61. }
  62. }
  63. void showdata()
  64. {
  65. int i,j;
  66. cout<<" 系统可用的资源数为:"<<endl<<endl;
  67. cout<<" ";
  68. for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<AVAILABLE[j];
  69. cout<<endl;
  70. // cout<<endl;
  71. // cout<<" 各进程资源的最大需求量:"<<endl<<endl;
  72. // for (i=0;i<M;i++)
  73. // {
  74. // cout<<"进程"<<i<<":";
  75. // for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<MAX[i][j];
  76. // cout<<endl;
  77. // }
  78. cout<<endl;
  79. cout<<" 各进程还需要的资源量:"<<endl<<endl;
  80. for (i=0;i<M;i++)
  81. { cout<<"进程"<<i<<":";
  82. for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<NEED[i][j];
  83. cout<<endl;}
  84. cout<<endl;
  85. cout<<" 各进程已经得到的资源量: "<<endl<<endl;
  86. for (i=0;i<M;i++)
  87. { cout<<"进程"<<i<<":";
  88. for (j=0;j<N;j++)cout<<" 资源"<<j<<": "<<ALLOCATION[i][j];
  89. cout<<endl;
  90. }
  91. cout<<endl;
  92. }
  93. }
  94. void changdata(int k)
  95. { int j;
  96. for (j=0;j<N;j++)
  97. { AVAILABLE[j]=AVAILABLE[j]-Request[j];
  98. ALLOCATION[k][j]=ALLOCATION[k][j]+Request[j];
  99. NEED[k][j]=NEED[k][j]-Request[j]; }
  100. }
  101. void rstordata(int k)
  102. { int j;
  103. for (j=0;j<N;j++)
  104. { AVAILABLE[j]=AVAILABLE[j]+Request[j];
  105. ALLOCATION[k][j]=ALLOCATION[k][j]-Request[j];
  106. NEED[k][j]=NEED[k][j]+Request[j];}
  107. }
  108. int chkerr(int s)
  109. { int WORK,FINISH[M],temp[M];
  110. int i,j,k=0;
  111. for(i=0;i<M;i++)FINISH[i]=FALSE;
  112. for(j=0;j<N;j++)
  113. { WORK=AVAILABLE[j]; i=s;
  114. while(i<M)
  115. { if (FINISH[i]==FALSE&&NEED[i][j]<=WORK)
  116. { WORK=WORK+ALLOCATION[i][j]; FINISH[i]=TRUE;
  117. temp[k]=i; k++; i=0; }
  118. else { i++; }
  119. }
  120. for(i=0;i<M;i++)
  121. if(FINISH[i]==FALSE) { cout<<endl;
  122. cout<<" 系统不安全!!! 本次资源申请不成功!!!"<<endl;
  123. cout<<endl; return 1; }
  124. }
  125. cout<<endl;
  126. cout<<" 经安全性检查,系统安全,本次分配成功。"<<endl;
  127. cout<<endl; cout<<" 本次安全序列:";
  128. for(i=0;i<M;i++)cout<<"进程"<<temp[i]<<"->";
  129. cout<<endl<<endl;;
  130. return 0;}

我给你们演示一下

在这里插入图片描述

期末考要到了,所以做题的原理咱还是得懂,毕竟挂科不太好。
推荐一个B站视频,up主讲的很好
https://www.bilibili.com/video/BV1i4411P7kh?from=search&seid=372181843460536848

发表评论

表情:
评论列表 (有 0 条评论,98人围观)

还没有评论,来说两句吧...

相关阅读

    相关 银行家算法实现代码

    银行家算法代码 简介:本文为大家奉献银行家算法的java代码实现,拿去吧,我的算法思路就是深度优先遍历(DFS),对深度优先遍历不熟悉的,可以看看我的这篇博客。 理解D