fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h> // replace this with #include<windows.h> for windows platform
  5.  
  6. #define BUFFER_SIZE 99
  7. #define PRODUCTION_COST 0
  8.  
  9. char producer_array[BUFFER_SIZE * 10];
  10. char consumer_array[BUFFER_SIZE];
  11.  
  12. int producer_index = 0;
  13. int consumer_index = 0;
  14.  
  15. pthread_mutex_t producer_mutex = PTHREAD_MUTEX_INITIALIZER;
  16. pthread_mutex_t consumer_mutex = PTHREAD_MUTEX_INITIALIZER;
  17. pthread_cond_t producer_cond = PTHREAD_COND_INITIALIZER;
  18. pthread_cond_t consumer_cond = PTHREAD_COND_INITIALIZER;
  19.  
  20. void *producer1(void *arg) {
  21. while (1) {
  22. char product;
  23. int rand_int = rand() % 2;
  24.  
  25. if (rand_int == 0) {
  26. product = 'G';
  27. sleep(PRODUCTION_COST);
  28. } else {
  29. product = 'R';
  30. sleep(PRODUCTION_COST);
  31. }
  32.  
  33. pthread_mutex_lock(&producer_mutex);
  34. if (producer_index >= BUFFER_SIZE * 10) {
  35. pthread_mutex_unlock(&producer_mutex);
  36. break;
  37. }
  38. char last_char = (producer_index > 0) ? producer_array[producer_index - 1] : '\0';
  39. if (product == 'G') {
  40. if (last_char == 'B' || last_char == '\0') {
  41. producer_array[producer_index++] = 'G';
  42. }
  43. } else if (product == 'R') {
  44. if (last_char == 'G') {
  45. producer_array[producer_index++] = 'R';
  46. }
  47. }
  48. pthread_cond_signal(&producer_cond);
  49. pthread_mutex_unlock(&producer_mutex);
  50. }
  51. pthread_exit(NULL);
  52. }
  53.  
  54. void *producer2(void *arg) {
  55. while (1) {
  56. char product;
  57. int rand_int = rand() % 2;
  58.  
  59. if (rand_int == 0) {
  60. product = 'R';
  61. sleep(PRODUCTION_COST);
  62. } else {
  63. product = 'B';
  64. sleep(PRODUCTION_COST);
  65. }
  66.  
  67. pthread_mutex_lock(&producer_mutex);
  68. if (producer_index >= BUFFER_SIZE * 10) {
  69. pthread_mutex_unlock(&producer_mutex);
  70. break;
  71. }
  72. char last_char = (producer_index > 0) ? producer_array[producer_index - 1] : '\0';
  73. if (product == 'R') {
  74. if (last_char == 'G') {
  75. producer_array[producer_index++] = 'R';
  76. }
  77. } else if (product == 'B') {
  78. if (last_char == 'R') {
  79. producer_array[producer_index++] = 'B';
  80. }
  81. }
  82. pthread_cond_signal(&producer_cond);
  83. pthread_mutex_unlock(&producer_mutex);
  84. }
  85. pthread_exit(NULL);
  86. }
  87.  
  88. void *producer3(void *arg) {
  89. while (1) {
  90. char product;
  91. int rand_int = rand() % 2;
  92.  
  93. if (rand_int == 0) {
  94. product = 'B';
  95. sleep(PRODUCTION_COST);
  96. } else {
  97. product = 'G';
  98. sleep(PRODUCTION_COST);
  99. }
  100.  
  101. pthread_mutex_lock(&producer_mutex);
  102. if (producer_index >= BUFFER_SIZE * 10) {
  103. pthread_mutex_unlock(&producer_mutex);
  104. break;
  105. }
  106. char last_char = (producer_index > 0) ? producer_array[producer_index - 1] : '\0';
  107. if (product == 'B') {
  108. if (last_char == 'R') {
  109. producer_array[producer_index++] = 'B';
  110. }
  111. } else if (product == 'G') {
  112. if (last_char == 'B') {
  113. producer_array[producer_index++] = 'G';
  114. }
  115. }
  116. pthread_cond_signal(&producer_cond);
  117. pthread_mutex_unlock(&producer_mutex);
  118. }
  119. pthread_exit(NULL);
  120. }
  121.  
  122. void *consumer(void *arg) {
  123. while (1) {
  124. pthread_mutex_lock(&consumer_mutex);
  125. if (consumer_index >= BUFFER_SIZE) {
  126. pthread_mutex_unlock(&consumer_mutex);
  127. break;
  128. }
  129.  
  130. pthread_mutex_lock(&producer_mutex);
  131. while (producer_index <= 0) {
  132. pthread_cond_wait(&producer_cond, &producer_mutex);
  133. }
  134. char last_p_char = producer_array[producer_index - 1];
  135. char last_c_char = (consumer_index > 0) ? consumer_array[consumer_index - 1] : '\0';
  136. if ((last_c_char == 'R' && last_p_char == 'G') ||
  137. (last_c_char == 'G' && last_p_char == 'B') ||
  138. (last_c_char == 'B' && last_p_char == 'R') ||
  139. (last_c_char == '\0' && last_p_char == 'G')) {
  140. consumer_array[consumer_index++] = last_p_char;
  141. producer_index--;
  142. }
  143. pthread_cond_signal(&consumer_cond);
  144. pthread_mutex_unlock(&producer_mutex);
  145. pthread_mutex_unlock(&consumer_mutex);
  146. }
  147. pthread_exit(NULL);
  148. }
  149.  
  150. int main() {
  151. pthread_t producer_threads[3];
  152. pthread_t consumer_threads[3];
  153.  
  154. pthread_create(&producer_threads[0], NULL, producer1, NULL);
  155. pthread_create(&producer_threads[1], NULL, producer2, NULL);
  156. pthread_create(&producer_threads[2], NULL, producer3, NULL);
  157. pthread_create(&consumer_threads[0], NULL, consumer, NULL);
  158. pthread_create(&consumer_threads[1], NULL, consumer, NULL);
  159. pthread_create(&consumer_threads[2], NULL, consumer, NULL);
  160.  
  161. for (int i = 0; i < 3; i++) {
  162. pthread_join(producer_threads[i], NULL);
  163. }
  164. for (int i = 0; i < 3; i++) {
  165. pthread_join(consumer_threads[i], NULL);
  166. }
  167.  
  168. printf("Consumer Array: ");
  169. for (int i = 0; i < consumer_index; i++) {
  170. printf("%c", consumer_array[i]);
  171. }
  172. printf("\n");
  173.  
  174. printf("Producer Array: ");
  175. for (int i = 0; i < producer_index; i++) {
  176. printf("%c", producer_array[i]);
  177. }
  178. printf("\n");
  179.  
  180. return 0;
  181. }
  182.  
Success #stdin #stdout 0.14s 5292KB
stdin
Standard input is empty
stdout
Consumer Array: GBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBR
Producer Array: GRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBGRBG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