操作系统--银行家算法c语言代码

avatar 2017年04月06日13:50:26 1 13725 views
博主分享免费Java教学视频,B站账号:Java刘哥
直接上代码了,两个文件分别是main.c和banker.h main.c
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "banker.h"
  4. /*
  5. @author  WellsLiu
  6. @url     liuyanzhao.com 
  7. 测试数据 
  8. max        4 1 1 1 0 2 1 2 4 2 1 0 1 1 1 1 2 1 1 0
  9. allocation 3 0 1 1 0 1 0 0 1 1 1 0 1 1 0 1 0 0 0 0
  10. request测试 
  11. eg1:   1   1 0 1 0       failed         request > need
  12. eg2:   1   0 1 0 1       failed         request > avaliable
  13. eg3:   1   0 0 1 0    successful
  14. */
  15. void inputMax()
  16. {
  17.     int i;
  18.     printf("请输入最大需求矩阵\n");
  19.     for (i = 0; i < PROCESSES_NUMBER; i++)
  20.     {
  21.         scanf("%d%d%d%d",&Max[i].A,&Max[i].B,&Max[i].C,&Max[i].D);
  22.     }
  23. }
  24. void inputAllocation()
  25. {
  26.     int i;
  27.     printf("请输入已分配资源数矩阵\n");
  28.     for (i = 0; i < PROCESSES_NUMBER; i++)
  29.     {
  30.         scanf("%d%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C,&Allocation[i].D);
  31.     }
  32. }
  33. void inputAllocaltion()
  34. {
  35.     int i;
  36.     printf("请输入已分配资源数矩阵\n");
  37.     for (i = 0; i < PROCESSES_NUMBER; i++)
  38.     {
  39.         scanf("%d%d%d%d",&Allocation[i].A,&Allocation[i].B,&Allocation[i].C,&Allocation[i].D);
  40.     }
  41. }
  42. //试探分配
  43. void ProbeAlloc(int process,RESOURCE *res)
  44. {
  45.     Available.A -= res->A;
  46.     Available.B -= res->B;
  47.     Available.C -= res->C;
  48.     Available.D -= res->D;
  49.     Allocation[process].A += res->A;
  50.     Allocation[process].B += res->B;
  51.     Allocation[process].C += res->C;
  52.     Allocation[process].D += res->D;
  53.     Need[process].A -= res->A;
  54.     Need[process].B -= res->B;
  55.     Need[process].C -= res->C;
  56.     Need[process].D -= res->D;
  57. }
  58. //若试探分配后进入不安全状态,将分配回滚
  59. void RollBack(int process,RESOURCE *res)
  60. {
  61.     Available.A += res->A;
  62.     Available.B += res->B;
  63.     Available.C += res->C;
  64.     Available.D += res->D;
  65.     Allocation[process].A -= res->A;
  66.     Allocation[process].B -= res->B;
  67.     Allocation[process].C -= res->C;
  68.     Allocation[process].D -= res->D;
  69.     Need[process].A += res->A;
  70.     Need[process].B += res->B;
  71.     Need[process].C += res->C;
  72.     Need[process].D += res->D;
  73. }
  74. //安全性检查
  75. bool SafeCheck()
  76. {
  77.     RESOURCE    Work = Available;
  78.     bool        Finish[PROCESSES_NUMBER] = {false,false,false,false,false};
  79.     int     i;
  80.     int     j = 0;
  81.     for (i = 0; i < PROCESSES_NUMBER; i++)
  82.     {
  83.         //是否已检查过
  84.         if(Finish[i] == false)
  85.         {
  86.             //是否有足够的资源分配给该进程
  87.             if(Need[i].A <= Work.A && Need[i].B <= Work.B && Need[i].C <= Work.C && Need[i].D <= Work.D)
  88.             {
  89.                 //有则使其执行完成,并将已分配给该进程的资源全部回收
  90.                 Work.A += Allocation[i].A;
  91.                 Work.B += Allocation[i].B;
  92.                 Work.C += Allocation[i].C;
  93.                 Work.D += Allocation[i].D;
  94.                 Finish[i] = true;
  95.                 safe[j++] = i;
  96.                 i = -1;             //重新进行遍历
  97.             }
  98.         }
  99.     }
  100.     //如果所有进程的Finish向量都为true则处于安全状态,否则为不安全状态
  101.     for (i = 0; i < PROCESSES_NUMBER; i++)
  102.     {
  103.         if (Finish[i] == false)
  104.         {
  105.             return false;
  106.         }
  107.     }
  108.     return true;
  109. }
  110. //资源分配请求
  111. bool request(int process,RESOURCE *res)
  112. {
  113.     //request向量需小于Need矩阵中对应的向量
  114.     if(res->A <= Need[process].A && res->B <= Need[process].B && res->C <= Need[process].C&& res->D <= Need[process].D)
  115.     {
  116.         //request向量需小于Available向量
  117.         if(res->A <= Available.A && res->B <= Available.B && res->C <= Available.C && res->D <= Available.D)
  118.         {
  119.             //试探分配
  120.             ProbeAlloc(process,res);
  121.             //如果安全检查成立,则请求成功,否则将分配回滚并返回失败
  122.             if(SafeCheck())
  123.             {
  124.                 return true;
  125.             }
  126.             else
  127.             {
  128.                 printf("安全性检查失败。原因:系统将进入不安全状态,有可能引起死锁。\n");
  129.                 printf("正在回滚...\n");
  130.                 RollBack(process,res);
  131.             }
  132.         }
  133.         else
  134.         {
  135.             printf("安全性检查失败。原因:请求向量大于可利用资源向量。\n");
  136.         }
  137.     }
  138.     else
  139.     {
  140.         printf("安全性检查失败。原因:请求向量大于需求向量。\n");
  141.     }
  142.     return false;
  143. }
  144. //输出资源分配表
  145. void PrintTable()
  146. {
  147.     printf("\t\t\t*********资源分配表*********\n");
  148.     printf("Process       Max                  Allocation             Need                Available\n");
  149.     printf("          A    B    C    D     A    B    C    D      A    B    C    D     A    B    C   D\n");
  150.     printf("  P0      %d    %d    %d    %d     %d    %d    %d    %d      %d    %d    %d    %d     %d    %d    %d   %d\n",Max[0].A,Max[0].B,Max[0].C,Max[0].D,Allocation[0].A,Allocation[0].B,Allocation[0].C,Allocation[0].D,Need[0].A,Need[0].B,Need[0].C,Need[0].D,Available.A,Available.B,Available.C,Available.D);
  151.     printf("  P1      %d    %d    %d    %d     %d    %d    %d    %d      %d    %d    %d    %d\n",Max[1].A,Max[1].B,Max[1].C,Max[1].D,Allocation[1].A,Allocation[1].B,Allocation[1].C,Allocation[1].D,Need[1].A,Need[1].B,Need[1].C,Need[1].D);
  152.     printf("  P2      %d    %d    %d    %d     %d    %d    %d    %d      %d    %d    %d    %d\n",Max[2].A,Max[2].B,Max[2].C,Max[2].D,Allocation[2].A,Allocation[2].B,Allocation[2].C,Allocation[2].D,Need[2].A,Need[2].B,Need[2].C,Need[2].D);
  153.     printf("  P3      %d    %d    %d    %d     %d    %d    %d    %d      %d    %d    %d    %d\n",Max[3].A,Max[3].B,Max[3].C,Max[3].D,Allocation[3].A,Allocation[3].B,Allocation[3].C,Allocation[3].D,Need[3].A,Need[3].B,Need[3].C,Need[3].D);
  154.     printf("  P4      %d    %d    %d    %d     %d    %d    %d    %d      %d    %d    %d    %d\n",Max[4].A,Max[4].B,Max[4].C,Max[4].D,Allocation[4].A,Allocation[4].B,Allocation[4].C,Allocation[4].D,Need[4].A,Need[4].B,Need[4].C,Need[4].D);
  155.     printf("\n");
  156. }
  157. int main()
  158. {
  159.     inputMax() ;
  160.     inputAllocation();
  161.     int i;
  162.     for (i = 0; i < PROCESSES_NUMBER; i++)
  163.     {
  164.             Need[i].A = Max[i].A - Allocation[i].A;
  165.             Need[i].B = Max[i].B - Allocation[i].B;
  166.             Need[i].C = Max[i].C - Allocation[i].C;
  167.             Need[i].D = Max[i].D - Allocation[i].D;
  168.     }
  169.     int ch;
  170.     printf("先检查初始状态是否安全。\n");
  171.     if (SafeCheck())
  172.     {
  173.         printf("系统处于安全状态。\n");
  174.         printf("安全序列是{P%d,P%d,P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2],safe[3],safe[4]);
  175.     }
  176.     else
  177.     {
  178.         printf("系统处于不安全状态。程序将退出...\n");
  179.         goto over;
  180.     }
  181.     do
  182.     {
  183.         int     process;
  184.         RESOURCE    res;
  185.         PrintTable();
  186.         printf("请依次输入请求分配的进程和对四类资源的请求数量:");
  187.         scanf("%d%d%d%d%d",&process,&res.A,&res.B,&res.C,&res.D);
  188.         if (request(process,&res))
  189.         {
  190.             printf("分配成功。\n");
  191.             printf("安全序列是{P%d,P%d,P%d,P%d,P%d}。\n",safe[0],safe[1],safe[2],safe[3],safe[4]);
  192.         }
  193.         else
  194.         {
  195.             printf("分配失败。\n");
  196.         }
  197.         printf("是否继续分配?(Y/N):");
  198.         fflush(stdin);              //虽然C标准不支持这种用法,但是VC++支持
  199.         ch = getchar();
  200.     } while (ch == 'Y' || ch == 'y');
  201. over:
  202.     printf("执行完毕。");
  203.     return 0;
  204. }
banker.h
  1. #pragma warning(disable:4996)
  2. typedef int bool;
  3. #define false 0
  4. #define true !false
  5. //系统中所有进程数量
  6. #define PROCESSES_NUMBER    5
  7. typedef struct {
  8.     int A;
  9.     int B;
  10.     int C;
  11.     int D;
  12. }RESOURCE;
  13. //最大需求矩阵  Max
  14. RESOURCE Max[PROCESSES_NUMBER];
  15. //已分配资源数矩阵 Allocation
  16. RESOURCE Allocation[PROCESSES_NUMBER];
  17. //需求矩阵
  18. RESOURCE Need[PROCESSES_NUMBER];
  19. //可用资源向量
  20. RESOURCE Available = {1,0,2,0};
  21. int safe[PROCESSES_NUMBER];
 
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0