fork download
  1. /*
  2. Bryant Siegfried
  3. Bxs122530
  4. 7/6/2018
  5. Homework 3
  6. */
  7.  
  8. #include <pthread.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. /* these may be any values >= 0 */
  14. #define NUMBER_OF_CUSTOMERS 5
  15. #define NUMBER_OF_RESOURCES 3
  16.  
  17. /* the available amount of each resource */
  18. int available[NUMBER_OF_RESOURCES];
  19.  
  20. /*the maximum demand of each customer */
  21. int maximum[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
  22.  
  23. /* the amount currently allocated to each customer */
  24. int allocation[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
  25.  
  26. /* the remaining need of each customer */
  27. int need[NUMBER_OF_CUSTOMERS][NUMBER_OF_RESOURCES];
  28.  
  29. /* return 0 if successful or -1 if unsuccessful */
  30. int request_resources(int customer_num, int request[]);
  31. int release_resources(int customer_num, int release[]);
  32.  
  33. /* resource checking*/
  34. int request[NUMBER_OF_RESOURCES];
  35. int release[NUMBER_OF_RESOURCES];
  36.  
  37. pthread_mutex_t mutex;
  38. void *create(void *params);
  39. int safe(int process);
  40.  
  41.  
  42.  
  43.  
  44. int request_resources(int customer_num, int request[]) {
  45.  
  46. printf("P%d requested the following: {%d, %d, %d}\n", customer_num, request[0], request[1], request[2]);
  47.  
  48. for(int i=0; i<NUMBER_OF_RESOURCES; i++) {
  49.  
  50. if(request[i] <= need[i]) {
  51.  
  52. if(request[i] <= available[i]) {
  53.  
  54. available[i] = available[i] - request[i]; //available allocation
  55.  
  56. int condition = safety(customer_num);
  57.  
  58. allocation[customer_num][i] = allocation[customer_num][i] + request[i];
  59.  
  60. need[customer_num][i] = need[customer_num][i] - request[i];
  61.  
  62. if(condition==0) {//0= sucess, safe!
  63.  
  64. printf("\n");
  65. printf(" Allocation");
  66. printf(" Max");
  67. printf(" Need\n");
  68.  
  69. for(i=0; i<NUMBER_OF_CUSTOMERS; i++) {
  70. printf("P%d ", i);
  71. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {
  72. printf(" {");
  73. printf("%d ", allocation[i][j]);
  74. printf("%d ", maximum[i][j]);
  75. printf("%d", need[i][j]);
  76. printf("} ");
  77. }
  78.  
  79. printf("\n");
  80. }
  81.  
  82. return 0; //success!
  83.  
  84. }
  85.  
  86.  
  87. else {//unsafe state entered
  88. available[i] = available[i] + request[i];
  89.  
  90. allocation[customer_num][i] = allocation[customer_num][i] - request[i];//allocate ammount requested
  91.  
  92. need[customer_num][i] = need[customer_num][i] + request[i]; // how much required by customer
  93.  
  94. printf("Unsafe State\n");
  95. return -1;
  96. }
  97.  
  98. }
  99. else if(request[i] > available[i]) {
  100. printf("Process will wait since resource requested is too large\n");
  101. return -1;
  102. }
  103. }
  104.  
  105.  
  106. else if(request[i] > need[i]) {//Request is too large pg332
  107. printf("Error too large of a request\n");
  108. return -1;
  109. }
  110.  
  111. }
  112.  
  113. }
  114.  
  115.  
  116. int safety(int process) {//are we in a safe state?
  117.  
  118. int work[process];
  119. int finish[process];
  120.  
  121. work[process] = available;
  122. finish[process] = -1;
  123.  
  124. for(int i=0; i<NUMBER_OF_CUSTOMERS; i++) {
  125. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {//test resources with customers
  126. if(finish[i]==-1 && need[i][j] <= work[i]) {//success formula, if true, fail
  127. return -1;//fail! unssafe
  128. }
  129. else {
  130. work[i] = work[i] + allocation[i][j];
  131. finish[i] = 0;
  132. }
  133. }
  134. }
  135.  
  136. return 0;//success!
  137. }
  138.  
  139.  
  140. //returns a 0 successful or -1 unsuccessful
  141. int release_resources(int customer_num, int release[]) {
  142.  
  143. for(int i=0; i<NUMBER_OF_RESOURCES; i++) {
  144. available[i] = available[i] - request[i]; //Available algorithm from pg332
  145.  
  146. allocation[customer_num][i] = allocation[customer_num][i] + request[i]; //Remove algorithm from pg332
  147.  
  148. need[customer_num][i] = need[customer_num][i] - request[i]; //Need algorithm from pg332
  149. }
  150.  
  151. printf("\n");
  152. printf(" Allocation");
  153. printf(" Max");
  154. printf(" Need\n");
  155.  
  156. for(int i=0; i<NUMBER_OF_CUSTOMERS; i++) {
  157. printf("P%d ", i);
  158. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {
  159. printf(" {");
  160. printf("%d ", allocation[i][j]);
  161. printf("%d ", maximum[i][j]);
  162. printf("%d", need[i][j]);
  163. printf("} ");
  164. }
  165.  
  166. printf("\n");
  167. }
  168. return 0;//Success
  169. }
  170. void *create(void *params) { //creates allocation based on input
  171. pthread_mutex_lock(&mutex);
  172.  
  173. int counter = 0;
  174.  
  175. for(int i=0; i<20; i++) {
  176.  
  177. counter++;
  178.  
  179. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {
  180. request[j] = rand() % (maximum[i][j]+1);
  181. }
  182.  
  183.  
  184. int k = rand() % NUMBER_OF_CUSTOMERS;
  185. i = k;
  186.  
  187. request_resources(i, request);
  188.  
  189. sleep(1);
  190.  
  191. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {
  192. release[j] = rand() % (maximum[i][j]+1);
  193. }
  194.  
  195. release_resources(i, release);
  196. pthread_mutex_unlock(&mutex);
  197. pthread_exit(0);
  198. }
  199. }
  200.  
  201. int main(int argumentCount, char* argumentArray[]) {
  202.  
  203. printf("Available\n{ ");
  204.  
  205. for(int i=1; i<argumentCount; i++) { //create argument array
  206. printf("%d ", atoi(argumentArray[i]));
  207. }
  208. printf("}");
  209. printf("\n");
  210. printf(" Allocation");
  211. printf(" Max");
  212. printf(" Need\n");
  213.  
  214. for(int i=0; i<NUMBER_OF_CUSTOMERS; i++) {
  215. printf("P%d ", i);
  216. for(int j=0; j<NUMBER_OF_RESOURCES; j++) {
  217. allocation[i][j] = 0;
  218. maximum[i][j] = rand() % (argumentCount+1);
  219. need[i][j] = maximum[i][j];
  220. printf(" {");
  221. printf("%d ", allocation[i][j]);
  222. printf("%d ", maximum[i][j]);
  223. printf("%d", need[i][j]);
  224. printf("} ");
  225. }
  226.  
  227. printf("\n");
  228. }
  229.  
  230.  
  231. pthread_t Customer[NUMBER_OF_CUSTOMERS];//Customer threads created
  232.  
  233. pthread_attr_t attr;
  234.  
  235. pthread_attr_init(&attr);//initialize threads
  236.  
  237. pthread_mutex_init(&mutex, NULL);//initialize mutex
  238.  
  239. for(int i=0; i<NUMBER_OF_CUSTOMERS; i++) {//create multiple threads
  240. pthread_create(&Customer[i], &attr, create, NULL);
  241. }
  242.  
  243. for(int i=0; i<NUMBER_OF_CUSTOMERS; i++) {//join threads
  244. pthread_join(Customer[i], NULL);
  245. }
  246.  
  247. pthread_mutex_destroy(&mutex); //end threading here
  248. }
  249.  
Success #stdin #stdout 0.01s 5436KB
stdin
10 5 7
stdout
Available
{ }
   Allocation    Max      Need
P0   {0 1 1}   {0 0 0}   {0 1 1}  
P1   {0 1 1}   {0 1 1}   {0 1 1}  
P2   {0 0 0}   {0 0 0}   {0 1 1}  
P3   {0 1 1}   {0 0 0}   {0 1 1}  
P4   {0 0 0}   {0 1 1}   {0 1 1}  
P2 requested the following: {0, 0, 0}

   Allocation    Max      Need
P0  {0 1 1}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {0 0 0}   {0 0 0}   {0 1 1}  
P3  {0 1 1}   {0 0 0}   {0 1 1}  
P4  {0 0 0}   {0 1 1}   {0 1 1}  

   Allocation    Max      Need
P0  {0 1 1}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {0 0 0}   {0 0 0}   {0 1 1}  
P3  {0 1 1}   {0 0 0}   {0 1 1}  
P4  {0 0 0}   {0 1 1}   {0 1 1}  
P0 requested the following: {1, 0, 0}
Process will wait since resource requested is too large

   Allocation    Max      Need
P0  {1 1 0}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {0 0 0}   {0 0 0}   {0 1 1}  
P3  {0 1 1}   {0 0 0}   {0 1 1}  
P4  {0 0 0}   {0 1 1}   {0 1 1}  
P2 requested the following: {1, 0, 0}
Process will wait since resource requested is too large

   Allocation    Max      Need
P0  {1 1 0}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {1 0 -1}   {0 0 0}   {0 1 1}  
P3  {0 1 1}   {0 0 0}   {0 1 1}  
P4  {0 0 0}   {0 1 1}   {0 1 1}  
P2 requested the following: {1, 0, 1}
Process will wait since resource requested is too large

   Allocation    Max      Need
P0  {1 1 0}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {2 0 -2}   {0 0 0}   {1 1 0}  
P3  {0 1 1}   {0 0 0}   {0 1 1}  
P4  {0 0 0}   {0 1 1}   {0 1 1}  
P3 requested the following: {1, 0, 1}
Process will wait since resource requested is too large

   Allocation    Max      Need
P0  {1 1 0}   {0 0 0}   {0 1 1}  
P1  {0 1 1}   {0 1 1}   {0 1 1}  
P2  {2 0 -2}   {0 0 0}   {1 1 0}  
P3  {1 1 0}   {0 0 0}   {1 1 0}  
P4  {0 0 0}   {0 1 1}   {0 1 1}