fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. #define BUFFER_SIZE 99
  6. #define PRODUCTION_COST 0
  7.  
  8. char producer_array[BUFFER_SIZE * 10];
  9. char consumer_array[BUFFER_SIZE];
  10.  
  11. int producer_index = 0;
  12. int consumer_index = 0;
  13.  
  14. pthread_mutex_t producer_mutex = PTHREAD_MUTEX_INITIALIZER;
  15. pthread_mutex_t consumer_mutex = PTHREAD_MUTEX_INITIALIZER;
  16.  
  17. void *producer1(void *arg)
  18. {
  19. while (1)
  20. {
  21. pthread_mutex_lock(&producer_mutex);
  22. if (producer_index >= BUFFER_SIZE || consumer_index >= BUFFER_SIZE)
  23. {
  24. pthread_mutex_unlock(&producer_mutex);
  25. break;
  26. }
  27.  
  28. producer_array[producer_index++] = 'G';
  29.  
  30. pthread_mutex_unlock(&producer_mutex);
  31. }
  32.  
  33. pthread_exit(NULL);
  34. }
  35.  
  36. void *producer2(void *arg)
  37. {
  38. while (1)
  39. {
  40. pthread_mutex_lock(&producer_mutex);
  41. if (producer_index >= BUFFER_SIZE || consumer_index >= BUFFER_SIZE)
  42. {
  43. pthread_mutex_unlock(&producer_mutex);
  44. break;
  45. }
  46.  
  47. producer_array[producer_index++] = 'B';
  48.  
  49. pthread_mutex_unlock(&producer_mutex);
  50. }
  51.  
  52. pthread_exit(NULL);
  53. }
  54.  
  55. void *producer3(void *arg)
  56. {
  57. while (1)
  58. {
  59. pthread_mutex_lock(&producer_mutex);
  60. if (producer_index >= BUFFER_SIZE || consumer_index >= BUFFER_SIZE)
  61. {
  62. pthread_mutex_unlock(&producer_mutex);
  63. break;
  64. }
  65.  
  66. producer_array[producer_index++] = 'R';
  67.  
  68. pthread_mutex_unlock(&producer_mutex);
  69. }
  70.  
  71. pthread_exit(NULL);
  72. }
  73.  
  74. void *consumer(void *arg)
  75. {
  76. while (1)
  77. {
  78. pthread_mutex_lock(&consumer_mutex);
  79. if (consumer_index >= BUFFER_SIZE || producer_index >= BUFFER_SIZE)
  80. {
  81. pthread_mutex_unlock(&consumer_mutex);
  82. break;
  83. }
  84.  
  85. char last_p_char = (producer_index > 0) ? producer_array[producer_index - 1] : '\0';
  86. if (last_p_char == 'G')
  87. {
  88. consumer_array[consumer_index++] = producer_array[--producer_index];
  89. }
  90. else if (last_p_char == 'B')
  91. {
  92. consumer_array[consumer_index++] = producer_array[--producer_index];
  93. }
  94. else if (last_p_char == 'R')
  95. {
  96. consumer_array[consumer_index++] = producer_array[--producer_index];
  97. }
  98.  
  99. pthread_mutex_unlock(&consumer_mutex);
  100. }
  101.  
  102. pthread_exit(NULL);
  103. }
  104.  
  105. int main()
  106. {
  107. pthread_t producer_threads[3];
  108. pthread_t consumer_thread;
  109.  
  110. pthread_create(&producer_threads[0], NULL, producer1, NULL);
  111. pthread_create(&producer_threads[1], NULL, producer2, NULL);
  112. pthread_create(&producer_threads[2], NULL, producer3, NULL);
  113. pthread_create(&consumer_thread, NULL, consumer, NULL);
  114.  
  115. for (int i = 0; i < 3; i++)
  116. {
  117. pthread_join(producer_threads[i], NULL);
  118. }
  119. pthread_join(consumer_thread, NULL);
  120.  
  121. printf("Consumer Array: ");
  122. for (int i = 0; i < consumer_index; i++)
  123. {
  124. printf("%c", consumer_array[i]);
  125. }
  126. printf("\n");
  127.  
  128. printf("Producer Array: ");
  129. for (int i = 0; i < producer_index; i++)
  130. {
  131. printf("%c", producer_array[i]);
  132. }
  133. printf("\n");
  134.  
  135. return 0;
  136. }
  137.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Consumer Array: 
Producer Array: RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR