Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. /*
  2. Author: Chris Osborn
  3. Class: CSE2431
  4. Assignment: Lab4
  5. Compilation Command: gcc PartA.c -o A -lpthread -lrt
  6. */
  7.  
  8. #include <pthread.h>
  9. #include <semaphore.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13. // Thread routines
  14. void *Thread1();
  15. void *Thread2();
  16. void *Thread3();
  17. void *Thread4();
  18.  
  19. pthread_t tid[4]; // array of thread IDs
  20. sem_t semaphore1; // sem1
  21. sem_t semaphore2; // sem2
  22. sem_t semaphore3; // sem3
  23. sem_t semaphore4; // sem4
  24.  
  25. // Only shared variable
  26. int Account[3];
  27.  
  28. int main()
  29. {
  30. int i;
  31.  
  32. // Initialize accounts
  33. Account[0] = 100000;
  34. Account[1] = 100000;
  35. Account[2] = 100000;
  36.  
  37. // Initialize semaphores to 1
  38. i = sem_init(&semaphore1, 0, 1);
  39. if(i < 0){exit;}
  40. i = sem_init(&semaphore2, 0, 1);
  41. if(i < 0){exit;}
  42. i = sem_init(&semaphore3, 0, 1);
  43. if(i < 0){exit;}
  44. i = sem_init(&semaphore4, 0, 1);
  45. if(i < 0){exit;}
  46.  
  47. // Create threads 1, 2, & 3
  48. i = pthread_create(&tid[0], NULL, Thread1, NULL);
  49. if(i < 0){exit;}
  50. i = pthread_create(&tid[1], NULL, Thread2, NULL);
  51. if(i < 0){exit;}
  52. i = pthread_create(&tid[2], NULL, Thread3, NULL);
  53. if(i < 0){exit;}
  54.  
  55. // Wait for Thread2 to finish, then exit
  56. i = pthread_join(tid[1], NULL);
  57. if(i < 0){exit;}
  58. exit;
  59. }
  60.  
  61. void *Thread1()
  62. {
  63. int i, internal_reg;
  64.  
  65. // Create Thread4
  66. i = pthread_create(&tid[3], NULL, Thread4, NULL);
  67. if(i < 0){exit;}
  68.  
  69. /* here sync with Thread2, Thread3, and Thread4 (4 way) */
  70. sem_post(&semaphore2);
  71. sem_post(&semaphore3);
  72. sem_post(&semaphore4);
  73.  
  74. sem_wait(&semaphore1);
  75. sem_wait(&semaphore1);
  76. sem_wait(&semaphore1);
  77.  
  78. for (i = 0; i < 10000; i++)
  79. {
  80. sem_wait(&semaphore1);
  81. internal_reg = Account[0];
  82. internal_reg = internal_reg - 200;
  83. Account[0] = internal_reg;
  84. /* moving $200 from Account[0] to Account[1] */
  85. internal_reg = Account [1];
  86. internal_reg = internal_reg + 200;
  87. Account[1] = internal_reg;
  88. sem_post(&semaphore1);
  89.  
  90. /* Prints contents of each account and their sum after each 1000 iterations*/
  91. if (i % 1000 == 0 && i != 0){
  92. printf("Thread1 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
  93. }
  94. // Sleep every 3000 iterations
  95. if(i % 3000 == 0 && i != 0)
  96. {usleep(200000);}
  97. }
  98. sem_post(&semaphore4);
  99. pthread_exit(NULL);
  100. }
  101.  
  102. void *Thread2()
  103. {
  104. int i, internal_reg;
  105.  
  106. /* here sync with Thread1, Thread3, and Thread4 (4 way) */
  107. sem_post(&semaphore1);
  108. sem_post(&semaphore3);
  109. sem_post(&semaphore4);
  110.  
  111. sem_wait(&semaphore2);
  112. sem_wait(&semaphore2);
  113. sem_wait(&semaphore2);
  114.  
  115. for (i = 0; i < 10000; i++)
  116. {
  117. sem_wait(&semaphore1);
  118. internal_reg = Account[1];
  119. internal_reg = internal_reg - 200;
  120. Account[1] = internal_reg;
  121. /* moving $200 from Account[1] to Account[2] */
  122. internal_reg = Account [2];
  123. internal_reg = internal_reg + 200;
  124. Account[2] = internal_reg;
  125. sem_post(&semaphore1);
  126.  
  127. /* Prints contents of each account and their sum after each 1000 iterations*/
  128. if (i % 1000 == 0 && i != 0){
  129. printf("Thread2 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
  130. }
  131. // Sleep after 2000, 4000, 6000, and 9000 iterations
  132. if(i == 2000 || i == 4000 || i == 6000 || i == 9000)
  133. {usleep(200000);}
  134. }
  135. sem_post(&semaphore4);
  136. pthread_exit(NULL);
  137. }
  138.  
  139. void *Thread3()
  140. {
  141. int i, internal_reg;
  142.  
  143. /* here sync with Thread1, Thread2, and Thread4 (4 way) */
  144. sem_post(&semaphore1);
  145. sem_post(&semaphore2);
  146. sem_post(&semaphore4);
  147.  
  148. sem_wait(&semaphore3);
  149. sem_wait(&semaphore3);
  150. sem_wait(&semaphore3);
  151.  
  152. for (i = 0; i < 10000; i++)
  153. {
  154. sem_wait(&semaphore1);
  155. internal_reg = Account[2];
  156. internal_reg = internal_reg - 200;
  157. Account[2] = internal_reg;
  158. /* moving $200 from Account[2] to Account[0] */
  159. internal_reg = Account [0];
  160. internal_reg = internal_reg + 200;
  161. Account[0] = internal_reg;
  162. sem_post(&semaphore1);
  163.  
  164. /* Prints contents of each account and their sum after each 1000 iterations*/
  165. if (i % 1000 == 0 && i != 0){
  166. printf("Thread3 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
  167. }
  168. // Sleep after 2000 and 7500 iterations
  169. if(i == 2000 || i == 7500)
  170. {usleep(300000);}
  171. }
  172. sem_post(&semaphore4);
  173. pthread_exit(NULL);
  174. }
  175.  
  176. void *Thread4()
  177. {
  178. int i, count = 0;
  179. /* here sync with Thread1, Thread2, and Thread3 (4 way) */
  180. sem_post(&semaphore1);
  181. sem_post(&semaphore2);
  182. sem_post(&semaphore3);
  183.  
  184. sem_wait(&semaphore4);
  185. sem_wait(&semaphore4);
  186. sem_wait(&semaphore4);
  187.  
  188. for(i = 0; i < 50000; i++)
  189. {
  190. if(Account[0]+Account[1]+Account[2] != 300000)
  191. {count++;}
  192. if(i % 10000 == 0 && i != 0)
  193. {usleep(50000);}
  194. }
  195. // 4 process 1 way sync to ensure Thread4 prints and exits last
  196. sem_wait(&semaphore4);
  197. sem_wait(&semaphore4);
  198. sem_wait(&semaphore4);
  199.  
  200. printf("The accounts did not add up to $300,000 %d times out of 50,000.\n", count);
  201. exit;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement