Advertisement
kish-dev

Untitled

May 16th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. /* mutex [] f1
  2.  
  3. sf ([] (flag0_true -> <> proc0)) && ([] (flag1_true -> <> proc1)) && ([] (flag2_true -> <> proc2))
  4.  
  5. deadlock ([]((c0 && c1) -> <>(proc0 || proc1)) && []((c0 && c2) -> <>(proc0 || proc2)) && []((c1 && c2) -> <>(proc1 || proc2)))
  6. []((c0 && c1 && !c2) -> <>(proc0 || proc1)) && []((c0 && c2 && !c1) -> <>(proc0 || proc2)) && []((c1 && c2 && !c0) -> <>(proc1 || proc2)) && []((c0 && c1 && c2) -> <>(proc0 || proc1 || proc2))
  7.  
  8. #define f1 (ncrit == 0 || ncrit == 1)
  9.  
  10. #define flag0_true (FLAG[0] == true)
  11. #define proc0 (proc[0]@crit)
  12. #define flag1_true (FLAG[1] == true)
  13. #define proc1 (proc[1]@crit)
  14. #define flag2_true (FLAG[2] == true)
  15. #define proc2 (proc[2]@crit)
  16.  
  17. #define c0 (FLAG[0] == true)
  18. #define proc0 (proc[0]@crit)
  19. #define c1 (FLAG[1] == true)
  20. #define proc1 (proc[1]@crit)
  21. #define c2 (FLAG[2] == true)
  22. #define proc2 (proc[2]@crit)*/
  23.  
  24. #define N_PROC 3
  25.  
  26. int M = N_PROC;
  27. bool FLAG[N_PROC] = false;
  28. int TURN[N_PROC] = 0;
  29.  
  30. byte ncrit = 0;
  31.  
  32. inline max(max_buf, temp_j_max) {
  33. max_buf = 0; temp_j_max = 0;
  34.  
  35. do
  36. :: temp_j_max < N_PROC ->
  37. if
  38. :: TURN[temp_j_max] > max_buf ->
  39. max_buf = TURN[temp_j_max];
  40. :: TURN[temp_j_max] <= max_buf;
  41. fi;
  42. temp_j_max++;
  43. :: temp_j_max == N_PROC -> break;
  44. od;
  45. }
  46.  
  47. inline acquire_mutex(i, MAX_TURN, j, j_max, j_waiting) {
  48. MAX_TURN = 0; j = 0; j_max = 0; j_waiting = 0;
  49. L1: atomic {
  50. j = 0;
  51. do
  52. :: j < N_PROC ->
  53. (TURN[j] < M);
  54. j++;
  55. :: j == N_PROC -> break;
  56. od; j = 0;
  57. };
  58.  
  59. FLAG[i] = true;
  60. max(MAX_TURN, j_max); TURN[i] = MAX_TURN;
  61.  
  62. if
  63. :: TURN[i] >= M ->
  64. TURN[i] = 0; FLAG[i] = false;goto L1;
  65. :: TURN[i] < M -> TURN[i] = TURN[i] + 1;
  66. fi;
  67.  
  68. FLAG[i] = false;
  69.  
  70. do
  71. :: j_waiting < N_PROC ->
  72. if
  73. :: j_waiting != i ->
  74. !(FLAG[j_waiting] != false);
  75. !(TURN[j_waiting] != 0 && (TURN[j_waiting] < TURN[i] || (TURN[j_waiting] == TURN[i] && j_waiting < i)));
  76. :: j_waiting == i;
  77. fi;
  78. j_waiting++;
  79. :: j_waiting == N_PROC -> break;
  80. od;
  81. }
  82.  
  83. inline release_mutex(i) {
  84. TURN[i] = 0;
  85. }
  86.  
  87. active [N_PROC] proctype proc() {
  88. int MAX_TURN, j, j_max, j_waiting = 0;
  89. again:
  90. acquire_mutex(_pid, MAX_TURN, j, j_max, j_waiting);
  91. crit: {
  92. ncrit++;
  93. assert(ncrit == 1);
  94. ncrit--;
  95. };
  96. release_mutex(_pid);
  97. goto again;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement