c语言银行家算法_C语言中的银行家算法

旧城等待, 2022-12-08 04:48 216阅读 0赞

0249e20d69a29ba46bcec9e52908ab12.png

c语言银行家算法

Here you will get program for banker’s algorithm in C.

在这里,您将获得C语言中银行家算法的程序。

The banker’s algorithm which is also known as avoidance algorithm is a deadlock detection algorithm. It was developed by Edsger Dijkstra. It is designed to check the safe state whenever a resource is requested. It takes analogy of bank, where customer request to withdraw cash. Based on some data the cash is lent to the customer. The banker can’t give more cash than what the customer has requested for, and the total available cash. As this algorithm uses bank analogy so named as banker’s algorithm

银行家算法(也称为回避算法)是一种死锁检测算法。 它是由Edsger Dijkstra开发的。 它旨在在请求资源时检查安全状态。 它以银行为类比,其中客户要求提取现金。 根据一些数据,现金贷给了客户。 银行家所提供的现金不能超过客户的要求以及可用现金总额。 由于此算法使用银行类比,因此被称为银行家算法

Banker’s Algorithm in C

The basic data structures used to implement this algorithm are given below.

下面给出了用于实现该算法的基本数据结构。

Let n be the total number of processes and m be the total number of resource types in the system.

令n为进程总数,m为系统中资源类型的总数。

Available: A vector of length m. It shows number of available resources of each type. If Available[i] = k, then k instances of resource Ri are available.

可用:长度为m的向量。 它显示每种类型的可用资源数量。 如果Available [i] = k,则资源R i的 k个实例可用。

Max: An n×m matrix that contain maximum demand of each process. If Max[i,j] = k, then process Pi can request maximum k instances of resource type Rj.

最大值:包含每个过程的最大需求的n×m矩阵。 如果Max [i,j] = k,则进程P i可以请求最多k个资源类型R j的实例。

Allocation: An n×m matrix that contain number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then Pi is currently allocated k instances of resource type Rj.

分配: n×m矩阵,其中包含当前分配给每个进程的每种类型的资源数量。 如果Allocation [i,j] = k,则为P i当前分配资源类型R j的 k个实例。

Need: An n×m matrix that shows the remaining resource need of each process. If Need[i,j] = k, then process Pi may need k more instances of resource type Rj to complete the task.

需求:一个n×m矩阵,显示每个进程的剩余资源需求。 如果Need [i,j] = k,则进程P i可能需要k个资源类型R j的更多实例来完成任务。

C语言中银行家算法程序 (Program for Banker’s Algorithm in C)

  1. #include <stdio.h>
  2. int current[5][5], maximum_claim[5][5], available[5];
  3. int allocation[5] = {0, 0, 0, 0, 0};
  4. int maxres[5], running[5], safe = 0;
  5. int counter = 0, i, j, exec, resources, processes, k = 1;
  6. int main()
  7. {
  8. printf("\nEnter number of processes: ");
  9. scanf("%d", &processes);
  10. for (i = 0; i < processes; i++)
  11. {
  12. running[i] = 1;
  13. counter++;
  14. }
  15. printf("\nEnter number of resources: ");
  16. scanf("%d", &resources);
  17. printf("\nEnter Claim Vector:");
  18. for (i = 0; i < resources; i++)
  19. {
  20. scanf("%d", &maxres[i]);
  21. }
  22. printf("\nEnter Allocated Resource Table:\n");
  23. for (i = 0; i < processes; i++)
  24. {
  25. for(j = 0; j < resources; j++)
  26. {
  27. scanf("%d", &current[i][j]);
  28. }
  29. }
  30. printf("\nEnter Maximum Claim Table:\n");
  31. for (i = 0; i < processes; i++)
  32. {
  33. for(j = 0; j < resources; j++)
  34. {
  35. scanf("%d", &maximum_claim[i][j]);
  36. }
  37. }
  38. printf("\nThe Claim Vector is: ");
  39. for (i = 0; i < resources; i++)
  40. {
  41. printf("\t%d", maxres[i]);
  42. }
  43. printf("\nThe Allocated Resource Table:\n");
  44. for (i = 0; i < processes; i++)
  45. {
  46. for (j = 0; j < resources; j++)
  47. {
  48. printf("\t%d", current[i][j]);
  49. }
  50. printf("\n");
  51. }
  52. printf("\nThe Maximum Claim Table:\n");
  53. for (i = 0; i < processes; i++)
  54. {
  55. for (j = 0; j < resources; j++)
  56. {
  57. printf("\t%d", maximum_claim[i][j]);
  58. }
  59. printf("\n");
  60. }
  61. for (i = 0; i < processes; i++)
  62. {
  63. for (j = 0; j < resources; j++)
  64. {
  65. allocation[j] += current[i][j];
  66. }
  67. }
  68. printf("\nAllocated resources:");
  69. for (i = 0; i < resources; i++)
  70. {
  71. printf("\t%d", allocation[i]);
  72. }
  73. for (i = 0; i < resources; i++)
  74. {
  75. available[i] = maxres[i] - allocation[i];
  76. }
  77. printf("\nAvailable resources:");
  78. for (i = 0; i < resources; i++)
  79. {
  80. printf("\t%d", available[i]);
  81. }
  82. printf("\n");
  83. while (counter != 0)
  84. {
  85. safe = 0;
  86. for (i = 0; i < processes; i++)
  87. {
  88. if (running[i])
  89. {
  90. exec = 1;
  91. for (j = 0; j < resources; j++)
  92. {
  93. if (maximum_claim[i][j] - current[i][j] > available[j])
  94. {
  95. exec = 0;
  96. break;
  97. }
  98. }
  99. if (exec)
  100. {
  101. printf("\nProcess%d is executing\n", i + 1);
  102. running[i] = 0;
  103. counter--;
  104. safe = 1;
  105. for (j = 0; j < resources; j++)
  106. {
  107. available[j] += current[i][j];
  108. }
  109. break;
  110. }
  111. }
  112. }
  113. if (!safe)
  114. {
  115. printf("\nThe processes are in unsafe state.\n");
  116. break;
  117. }
  118. else
  119. {
  120. printf("\nThe process is in safe state");
  121. printf("\nAvailable vector:");
  122. for (i = 0; i < resources; i++)
  123. {
  124. printf("\t%d", available[i]);
  125. }
  126. printf("\n");
  127. }
  128. }
  129. return 0;
  130. }

If you found anything incorrect in above program for banker’s algorithm in C then comment below.

如果您在上述程序中对于C中的银行家算法发现任何不正确的内容,请在下面注释。

Source: https://en.wikipedia.org/wiki/Banker%27s_algorithm

来源: https : //zh.wikipedia.org/wiki/Banker%27s_algorithm

翻译自: https://www.thecrazyprogrammer.com/2016/07/bankers-algorithm-in-c.html

c语言银行家算法

发表评论

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

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

相关阅读

    相关 银行家算法

    银行家算法 定义 原理 局限 参数 原则 定义 银行家算法是一种死锁避免算法,该算法允许进程动态申请资源。 原理 系统毎次在

    相关 银行家算法

    银行家算法 进程申请资源时,系统通过一定的算法判断本次申请是否不可能产生死锁(处于安全状态)。若可能产生死锁(处于不安全状态),则暂不进行本次资源分配,以避免死锁。算法有

    相关 银行家算法

    银行家算法 银行家算法是一种用来避免操作系统死锁出现的有效算法,所以在引入银行家算法的解释之前,有必要简单介绍一下死锁的概念。 一、死锁 死锁:是指两个或两个以上