银行家算法实现代码

电玩女神 2024-03-30 12:09 147阅读 0赞

银行家算法代码

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

理解DFS参考文章:排列数字(Java每日一题)

银行家算法银行家算法(Banker’s Algorithm)是一个避免死锁(Deadlock)的著名算法,是由艾兹格·迪杰斯特拉在1965年为T.H.E系统设计的一种避免死锁产生的算法。它以银行借贷系统的分配策略为基础,判断并保证系统的安全运行。

  1. /*银行家算法 Banker Algorithm*/
  2. /*算法思路:dfs(深度优先遍历)*/
  3. import java.util.*;
  4. public class Main{
  5. static class Process{
  6. // 每个进程的情况
  7. public Resources max;
  8. public Resources allocation;
  9. public Resources need;
  10. public Process() {
  11. // TODO Auto-generated constructor stub
  12. }
  13. public Process(Resources max, Resources allocation, Resources need)
  14. {
  15. this.max = max;
  16. this.allocation = allocation;
  17. this.need = need;
  18. }
  19. }
  20. static class Resources // 这里以三个资源为例子
  21. {
  22. int A, B, C;
  23. public Resources(int A, int B, int C) {
  24. this.A = A;
  25. this.B = B;
  26. this.C = C;
  27. //System.out.println("RE" + this.A + " " + this.B + " " + this.C);
  28. }
  29. void showAll() // 查看当前的资源的占比情况 调试用的
  30. {
  31. System.out.println("A:" + A + " " + "B:" + B + " " + "C:" + C);
  32. }
  33. }
  34. static List<Process> pset = new ArrayList<Process>(); // 存放每个进程的情况
  35. static List<Integer> v = new ArrayList<Integer>(); // 存放最后的安全序列的
  36. static int N = 110; // 资源的最大个数的限制
  37. static boolean [] st = new boolean[N]; // dfs的辅助数组
  38. static int n;
  39. static Resources available; // 把可用的资源定义为全局数组 方便修改
  40. static int cnt = 0; // 存放最后的结果数
  41. public static void main(String[] args) {
  42. Scanner in = new Scanner(System.in);
  43. System.out.print("输入进程数:");
  44. n = in.nextInt(); // 进程的数量
  45. int a, b, c;
  46. int i = 0;
  47. int t = n;
  48. while(t -- > 0)
  49. {
  50. System.out.println("输入进程P" + i + "的Max:");
  51. System.out.print("A:");
  52. a = in.nextInt();
  53. System.out.print("B:");
  54. b = in.nextInt();
  55. System.out.print("C:");
  56. c = in.nextInt();
  57. i ++;
  58. Resources max = new Resources(a, b, c);
  59. System.out.println("输入Allocation:");
  60. System.out.print("A:");
  61. a = in.nextInt();
  62. System.out.print("B:");
  63. b = in.nextInt();
  64. System.out.print("C:");
  65. c = in.nextInt();
  66. Resources allocation = new Resources(a, b, c);
  67. pset.add(new Process(max, allocation, Sub(max, allocation)));
  68. }
  69. System.out.println("输入Available:");
  70. System.out.print("A:");
  71. a = in.nextInt();
  72. System.out.print("B:");
  73. b = in.nextInt();
  74. System.out.print("C:");
  75. c = in.nextInt();
  76. available = new Resources(a, b, c);
  77. if (n == 0) return;
  78. dfs(0);
  79. if (cnt == 0) System.out.println("没有安全序列");
  80. System.out.println("安全序列的数量:" + cnt);
  81. }
  82. static Resources Sub(Resources r1, Resources r2) // 计算两个资源集合相减的结果
  83. {
  84. Resources r = new Resources(r1.A - r2.A, r1.B - r2.B, r1.C - r2.C);
  85. // System.out.println("Sub");
  86. // r.showAll();
  87. return r;
  88. }
  89. static Resources Add(Resources r1, Resources r2) // 两个资源集合相加
  90. {
  91. Resources r = new Resources(r1.A + r2.A, r1.B + r2.B, r1.C + r2.C);
  92. // System.out.println("Add:");
  93. // r.showAll();
  94. return r;
  95. }
  96. static boolean CP(Resources r1, Resources r2) // 对两个资源集合进行比较 r1 <= r2返回true
  97. {
  98. boolean flag = false;
  99. if (r1.A <= r2.A && r1.B <= r2.B && r1.C <= r2.C) flag = true;
  100. // if (flag == false)
  101. // {
  102. // System.out.println("r1 ");
  103. // r1.showAll();
  104. // System.out.println("r2 ");
  105. // r2.showAll();
  106. // }
  107. return flag;
  108. }
  109. // 深度优先遍历的模板
  110. static void dfs(int u)
  111. {
  112. if (u == n)
  113. {
  114. cnt ++;
  115. for (Integer it : v) // 打印结果
  116. {
  117. System.out.print("P" + it + " ");
  118. }
  119. System.out.println();
  120. return;
  121. }
  122. for (int i = 0; i < n; ++ i)
  123. {
  124. Resources t1 = pset.get(i).need;
  125. Resources t2 = pset.get(i).allocation;
  126. if (!st[i] && CP(t1, available)) // 如果当前的这个进程的need小于available而且没有被访问过
  127. {
  128. st[i] = true;
  129. available = Add(t2, available);
  130. v.add(i);
  131. dfs(u + 1);
  132. available = Sub(available, t2);
  133. st[i] = false;
  134. v.remove(v.size() - 1);
  135. }
  136. }
  137. }
  138. }

运行结果

  1. 输入进程数:5
  2. 输入进程P0Max:
  3. A:7
  4. B:5
  5. C:3
  6. 输入Allocation:
  7. A:0
  8. B:1
  9. C:0
  10. 输入进程P1Max:
  11. A:3
  12. B:2
  13. C:2
  14. 输入Allocation:
  15. A:2
  16. B:0
  17. C:0
  18. 输入进程P2Max:
  19. A:9
  20. B:0
  21. C:2
  22. 输入Allocation:
  23. A:3
  24. B:0
  25. C:2
  26. 输入进程P3Max:
  27. A:2
  28. B:2
  29. C:2
  30. 输入Allocation:
  31. A:2
  32. B:1
  33. C:1
  34. 输入进程P4Max:
  35. A:4
  36. B:3
  37. C:3
  38. 输入Allocation:
  39. A:0
  40. B:0
  41. C:2
  42. 输入Available:
  43. A:3
  44. B:3
  45. C:2
  46. P1 P3 P0 P2 P4
  47. P1 P3 P0 P4 P2
  48. P1 P3 P2 P0 P4
  49. P1 P3 P2 P4 P0
  50. P1 P3 P4 P0 P2
  51. P1 P3 P4 P2 P0
  52. P1 P4 P3 P0 P2
  53. P1 P4 P3 P2 P0
  54. P3 P1 P0 P2 P4
  55. P3 P1 P0 P4 P2
  56. P3 P1 P2 P0 P4
  57. P3 P1 P2 P4 P0
  58. P3 P1 P4 P0 P2
  59. P3 P1 P4 P2 P0
  60. P3 P4 P1 P0 P2
  61. P3 P4 P1 P2 P0
  62. 安全序列的数量:16

发表评论

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

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

相关阅读

    相关 银行家算法实现代码

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

    相关 银行家算法

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

    相关 银行家算法

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