Advertisement
sorinavancea

75p-tema2

Apr 22nd, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <pthread.h>
  6. #include <fcntl.h> /* For O_* constants */
  7. #include <sys/stat.h> /* For mode constants */
  8. #include <semaphore.h>
  9. #include "a2_helper.h"
  10. sem_t *s3, *s4, *s5;
  11. sem_t sem, s1, s2;
  12. pthread_t t3_1, t3_2, t3_3, t3_4, t3_5, t7_1, t7_2, t7_3, t7_4, t7_5, t2[43];
  13.  
  14. void *T3_1(void *arg)
  15. {
  16. sem_wait(s3);
  17. info(BEGIN, 3, 1);
  18. info(END, 3, 1);
  19. sem_post(s5);
  20. pthread_exit(NULL);
  21. }
  22.  
  23. void *T3_2(void *arg)
  24. {
  25. info(BEGIN, 3, 2);
  26. info(END, 3, 2);
  27. pthread_exit(NULL);
  28. }
  29.  
  30. void *T3_3(void *arg)
  31. {
  32. sem_wait(&s1);
  33. info(BEGIN, 3, 3);
  34. info(END, 3, 3);
  35. sem_post(&s2);
  36. pthread_exit(NULL);
  37. }
  38.  
  39. void *T3_4(void *arg)
  40. {
  41. info(BEGIN, 3, 4);
  42. sem_post(&s1);
  43. sem_wait(&s2);
  44. pthread_join(t3_3, NULL);
  45. info(END, 3, 4);
  46. sem_destroy(&s1);
  47. sem_destroy(&s2);
  48. pthread_exit(NULL);
  49. }
  50.  
  51. void *T3_5(void *arg)
  52. {
  53. info(BEGIN, 3, 5);
  54. info(END, 3, 5);
  55. pthread_exit(NULL);
  56. }
  57.  
  58. void *T7_1(void *arg)
  59. {
  60. info(BEGIN, 7, 1);
  61. info(END, 7, 1);
  62. pthread_exit(NULL);
  63. }
  64.  
  65. void *T7_2(void *arg)
  66. {
  67. info(BEGIN, 7, 2);
  68. info(END, 7, 2);
  69. sem_post(s3);
  70. pthread_exit(NULL);
  71. }
  72.  
  73. void *T7_3(void *arg)
  74. {
  75. sem_wait(s5);
  76. info(BEGIN, 7, 3);
  77. info(END, 7, 3);
  78. pthread_exit(NULL);
  79. }
  80.  
  81. void *T7_4(void *arg)
  82. {
  83.  
  84. info(BEGIN, 7, 4);
  85. info(END, 7, 4);
  86. pthread_exit(NULL);
  87. }
  88.  
  89. void *T7_5(void *arg)
  90. {
  91. info(BEGIN, 7, 5);
  92. info(END, 7, 5);
  93. pthread_exit(NULL);
  94. }
  95.  
  96. void *thread(void *arg)
  97. {
  98. int id = (intptr_t)arg;
  99. info(BEGIN, 4, id);
  100. info(END, 4, id);
  101. pthread_exit(NULL);
  102. }
  103.  
  104. int main()
  105. {
  106. pid_t p2 = -1, p3 = -1, p4 = -1, p5 = -1, p6 = -1, p7 = -1;
  107. init();
  108. info(BEGIN, 1, 0);
  109. s3 = sem_open("/semafor1", O_CREAT, 0644, 0);
  110. s5 = sem_open("/semafor3", O_CREAT, 0644, 0);
  111. p2 = fork();
  112.  
  113. if (p2 == 0)
  114. {
  115. info(BEGIN, 2, 0);
  116. info(END, 2, 0);
  117. }
  118. else
  119. {
  120.  
  121. p3 = fork();
  122. if (p3 == 0)
  123. {
  124. info(BEGIN, 3, 0);
  125. sem_init(&s1, 0, 0);
  126. sem_init(&s2, 0, 0);
  127.  
  128. pthread_create(&t3_4, NULL, &T3_4, (void *)4);
  129. pthread_create(&t3_3, NULL, &T3_3, (void *)3);
  130. pthread_create(&t3_1, NULL, &T3_1, (void *)1);
  131. pthread_create(&t3_5, NULL, &T3_5, (void *)5);
  132. pthread_create(&t3_2, NULL, &T3_2, (void *)2);
  133.  
  134. pthread_join(t3_3, NULL);
  135. pthread_join(t3_4, NULL);
  136. pthread_join(t3_1, NULL);
  137. pthread_join(t3_5, NULL);
  138. pthread_join(t3_2, NULL);
  139. info(END, 3, 0);
  140. }
  141. else
  142. {
  143. p4 = fork();
  144. if (p4 == 0)
  145. {
  146. info(BEGIN, 4, 0);
  147. // for (int i = 0; i < 43; i++)
  148. //{
  149. // pthread_create(&t2[i], NULL, &thread, (void *)(intptr_t)i + 1);
  150. // }
  151.  
  152. // for (int i = 0; i < 43; i++)
  153. // {
  154. // pthread_join(t2[i], NULL);
  155. // }
  156.  
  157. info(END, 4, 0);
  158. }
  159. else
  160. {
  161. p5 = fork();
  162. if (p5 == 0)
  163. {
  164. info(BEGIN, 5, 0);
  165. info(END, 5, 0);
  166. }
  167. else
  168. {
  169. p6 = fork();
  170. if (p6 == 0)
  171. {
  172. info(BEGIN, 6, 0);
  173. info(END, 6, 0);
  174. }
  175. else
  176. {
  177. p7 = fork();
  178. if (p7 == 0)
  179. {
  180. info(BEGIN, 7, 0);
  181. pthread_create(&t7_4, NULL, &T7_4, (void *)4);
  182. pthread_create(&t7_3, NULL, &T7_3, (void *)3);
  183. pthread_create(&t7_1, NULL, &T7_1, (void *)1);
  184. pthread_create(&t7_5, NULL, &T7_5, (void *)5);
  185. pthread_create(&t7_2, NULL, &T7_2, (void *)2);
  186.  
  187. pthread_join(t7_3, NULL);
  188. pthread_join(t7_4, NULL);
  189. pthread_join(t7_1, NULL);
  190. pthread_join(t7_5, NULL);
  191. pthread_join(t7_2, NULL);
  192.  
  193. info(END, 7, 0);
  194. }
  195. else
  196. {
  197. wait(NULL);
  198. wait(NULL);
  199. wait(NULL);
  200. wait(NULL);
  201. wait(NULL);
  202. wait(NULL);
  203. info(END, 1, 0);
  204. sem_close(s3);
  205. sem_close(s5);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212.  
  213. return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement