Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <pthread.h>
  7. #include <string.h>
  8. #include <semaphore.h>
  9.  
  10. void* A(void* arg);
  11. void* B(void* arg);
  12. void* C(void* arg);
  13.  
  14. int A_counter = 0;
  15. int B_counter = 0;
  16. int C_counter = 0;
  17.  
  18. sem_t handler_A;
  19. sem_t handler_B;
  20. sem_t handler_C;
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28. void* A(void *arg)
  29. {
  30.  
  31. int lock = 1;
  32. int start = 0;
  33. int counter = 0;
  34.  
  35. while(counter < 11)
  36. {
  37. if (start == 0)
  38. {
  39. sem_wait(&handler_A);
  40. printf("A");
  41. fflush(stdout);
  42. sleep(1);
  43. counter++;
  44.  
  45. sem_post(&handler_B);
  46. start = 1;
  47. }
  48. else if (start == 1)
  49. {
  50. sem_wait(&handler_A);
  51. printf(" A");
  52. fflush(stdout);
  53. sleep(1);
  54. counter++;
  55.  
  56. if (lock == 1)
  57. {
  58. lock = 0;
  59. sem_post(&handler_B);
  60.  
  61. }
  62. else if (lock == 0)
  63. {
  64. sem_post(&handler_C);
  65. lock = 1;
  66. }
  67. }
  68. }
  69. pthread_exit(0);
  70. }
  71. void* B(void *arg)
  72. {
  73. int start = 0;
  74. int counter = 0;
  75. while (counter < 6)
  76. {
  77. if (start == 0)
  78. {
  79. sem_wait(&handler_B);
  80. printf("B");
  81. fflush(stdout);
  82. sleep(1);
  83. counter++;
  84.  
  85. sem_post(&handler_C);
  86. start = 1;
  87. }
  88. else if(start == 1)
  89. {
  90. sem_wait(&handler_B);
  91. printf("B");
  92. fflush(stdout);
  93. sleep(1);
  94. counter++;
  95.  
  96. sem_post(&handler_A);
  97. }
  98. }
  99.  
  100. pthread_exit(0);
  101. }
  102.  
  103. void* C(void *arg)
  104. {
  105. int counter = 0;
  106. while (counter < 6)
  107. {
  108. sem_wait(&handler_C);
  109. printf("C");
  110. fflush(stdout);
  111. sleep(1);
  112. counter++;
  113.  
  114. sem_post(&handler_A);
  115. }
  116.  
  117. pthread_exit(0);
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. int main()
  126. {
  127. int i[3];
  128. pthread_t thread_a;
  129. pthread_t thread_b;
  130. pthread_t thread_c;
  131.  
  132. i[0] = 0;
  133. i[1] = 1;
  134. i[2] = 2;
  135.  
  136. sem_init (&handler_A, 0, 1);
  137. sem_init (&handler_B, 1, 0);
  138. sem_init (&handler_C, 0, 0);
  139.  
  140.  
  141. pthread_create (&thread_a, NULL, (void *) &A, (void *) &i[0]);
  142. pthread_create (&thread_b, NULL, (void *) &B, (void *) &i[1]);
  143. pthread_create (&thread_c, NULL, (void *) &C, (void *) &i[2]);
  144.  
  145. pthread_join (thread_a, NULL);
  146. pthread_join (thread_b, NULL);
  147. pthread_join (thread_c, NULL);
  148.  
  149. sem_destroy (&handler_A);
  150. sem_destroy (&handler_B);
  151. sem_destroy (&handler_C);
  152.  
  153. exit(0);
  154. return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement