Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define TYPES_OF_RESOURCES 3
  5. #define NUM_OF_THREADS 5
  6. #define DEBUG 2
  7.  
  8. void populate_need();
  9. int is_system_in_safe_state();
  10. void traverse_array(int length, int* first, char* name);
  11. void populate_resources_available();
  12. int has_finished(int* first);
  13. int next_thread(int current_thread);
  14.  
  15. // Number of existent resources of each type
  16. int resources_existent[TYPES_OF_RESOURCES] = { 10, 5, 7 };
  17.  
  18. // Number of available resources of each type
  19. int resources_available[TYPES_OF_RESOURCES] = { 0, 0, 0 };
  20.  
  21. // Max demand of each thread for each resource
  22. // Each line illustrates a thread
  23. // Each column illustrates a type of resource
  24. int max[NUM_OF_THREADS][TYPES_OF_RESOURCES] = {
  25. { 7, 5, 3 },
  26. { 3, 2, 2 },
  27. { 9, 0, 2 },
  28. { 2, 2, 2 },
  29. { 4, 3, 3 }
  30. };
  31.  
  32. // Number of each type of resource currently allocated to each thread
  33. /*
  34. int allocated[NUM_OF_THREADS][TYPES_OF_RESOURCES] = {
  35. { 0, 0, 0 },
  36. { 0, 0, 0 },
  37. { 0, 0, 0 },
  38. { 0, 0, 0 },
  39. { 0, 0, 0 }
  40. };
  41. */
  42.  
  43. int allocated[NUM_OF_THREADS][TYPES_OF_RESOURCES] = {
  44. { 0, 1, 0 },
  45. { 2, 0, 0 },
  46. { 3, 0, 2 },
  47. { 2, 1, 1 },
  48. { 0, 0, 2 }
  49. };
  50.  
  51. int need[NUM_OF_THREADS][TYPES_OF_RESOURCES] = {
  52. { 0, 0, 0 },
  53. { 0, 0, 0 },
  54. { 0, 0, 0 },
  55. { 0, 0, 0 },
  56. { 0, 0, 0 }
  57. };
  58.  
  59. int main() {
  60. populate_resources_available();
  61. populate_need();
  62. is_system_in_safe_state();
  63. return 0;
  64. }
  65.  
  66. void populate_resources_available()
  67. {
  68. for (int resource_num = 0; resource_num < TYPES_OF_RESOURCES; ++resource_num) {
  69. int occupied = 0;
  70. for (int thread_num = 0; thread_num < NUM_OF_THREADS; ++thread_num) {
  71. occupied += allocated[thread_num][resource_num];
  72. }
  73. if (DEBUG > 1) printf("Resource number %d has %d and %d are occupied...\n", resource_num, resources_existent[resource_num], occupied);
  74. resources_available[resource_num] = resources_existent[resource_num] - occupied;
  75. }
  76. if (DEBUG) traverse_array(TYPES_OF_RESOURCES, &resources_available[0], "Resources available");
  77. }
  78.  
  79. void populate_need()
  80. {
  81. for (int thread_num = 0; thread_num < NUM_OF_THREADS; ++thread_num) {
  82. for (int resource_num = 0; resource_num < TYPES_OF_RESOURCES; ++resource_num) {
  83. need[thread_num][resource_num] = max[thread_num][resource_num] - allocated[thread_num][resource_num];
  84. }
  85. }
  86. }
  87.  
  88. // Returns 1 if it is in safe state, 0 otherwise
  89. int is_system_in_safe_state()
  90. {
  91. int work[TYPES_OF_RESOURCES];
  92. int finish[NUM_OF_THREADS];
  93. for (int resource_num = 0; resource_num < TYPES_OF_RESOURCES; ++resource_num) {
  94. work[resource_num] = resources_available[resource_num];
  95. }
  96. for (int thread_num = 0; thread_num < NUM_OF_THREADS; ++thread_num) {
  97. finish[thread_num] = 0;
  98. }
  99. int thread_num = 0;
  100. int is_safe = 0;
  101. while (!has_finished(&finish[0])) {
  102. if (finish[thread_num]) {
  103. thread_num = next_thread(thread_num);
  104. continue;
  105. }
  106. int passed = 1;
  107. if (DEBUG > 1) {
  108. printf("Current thread T%d = ",thread_num);
  109. traverse_array(TYPES_OF_RESOURCES, &need[thread_num][0], "");
  110. }
  111. for (int resource_num = 0; resource_num < TYPES_OF_RESOURCES; ++resource_num) {
  112. if (need[thread_num][resource_num] > work[resource_num]) {
  113. passed = 0;
  114. break;
  115. }
  116. }
  117. if (!passed) {
  118. thread_num = next_thread(thread_num);
  119. continue;
  120. }
  121. if (DEBUG) printf("Thread T%d has passed...\n", thread_num);
  122. finish[thread_num] = 1;
  123. for (int resource_num = 0; resource_num < TYPES_OF_RESOURCES; ++resource_num) {
  124. work[resource_num] += allocated[thread_num][resource_num];
  125. }
  126. if (DEBUG) traverse_array(TYPES_OF_RESOURCES, &work[0], "Resources available");
  127. thread_num = next_thread(thread_num);
  128. }
  129. return is_safe;
  130. }
  131.  
  132. void traverse_array(int length, int* first, char* name)
  133. {
  134. printf("Array: %s = ", name);
  135. for (int i = 0; i < length; ++i) {
  136. printf("%d ", *(first+i));
  137. }
  138. printf("\n");
  139. }
  140.  
  141. int has_finished(int* first)
  142. {
  143. int finished = 1;
  144. for (int thread_num = 0; thread_num < NUM_OF_THREADS; ++thread_num) {
  145. if (!*(first + thread_num)) {
  146. finished = 0;
  147. break;
  148. }
  149. }
  150. printf("Has finished ? %s...\n", finished ? "true" : "false");
  151. printf("-------------------------\n");
  152. return finished;
  153. }
  154.  
  155. int next_thread(int current_thread)
  156. {
  157. return current_thread == NUM_OF_THREADS - 1 ? 0 : current_thread + 1;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement