Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. /*
  2. *
  3. *
  4. * CS 441/541: Bounded Buffer (Project 4)
  5. *
  6. */
  7. #include "bounded-buffer.h"
  8.  
  9.  
  10. int main(int argc, char * argv[]) {
  11. int ret;
  12. time_t t;
  13.  
  14. // Initialize
  15. elements_produced = 0;
  16. elements_consumed = 0;
  17. in = 0;
  18. out = 0;
  19. sleep_time = atoi(argv[1]);
  20. num_producer_threads = atoi(argv[2]);
  21. num_consumer_threads = atoi(argv[3]);
  22.  
  23. srand((unsigned) time(&t));
  24.  
  25. // Check for BUFFER_SIZE argument
  26. if(argc > 4) {
  27. buffer_size = atoi(argv[4]);
  28. }
  29.  
  30. // Create num of threads based on user input
  31. pthread_t producer_threads[num_producer_threads];
  32. pthread_t consumer_threads[num_consumer_threads];
  33.  
  34. if( 0 != (ret = init_buffer()) ){
  35. fprintf(stderr, "Error initalizing buffer");
  36. }
  37.  
  38. print_inital_info();
  39.  
  40. int rc, i;
  41.  
  42. // Create producer threads
  43. for(i = 0; i < num_producer_threads; i++){
  44. //printf("In main creating producer thread: %d\n", i);
  45. rc = pthread_create(&producer_threads[i], NULL, producer, (void *)(intptr_t)i);
  46. if(0 != rc){
  47. printf("ERROR");
  48. exit(-1);
  49. }
  50. }
  51.  
  52. // Create consumer threads
  53. for(i = 0; i < num_consumer_threads; i++){
  54. //printf("In main creating consumer thread: %d\n", i);
  55. rc = pthread_create(&consumer_threads[i], NULL, consumer, (void *)(intptr_t)i);
  56. if(0 != rc){
  57. printf("ERROR");
  58. exit(-1);
  59. }
  60. }
  61.  
  62. sleep(sleep_time);
  63.  
  64. printf("-------------------------------\n");
  65. printf("Produced | %d\n", elements_produced);
  66. printf("Consumed | %d\n", elements_consumed);
  67. free(buffer);
  68. return 0;
  69. }
  70.  
  71. int insert_item(int item){
  72. buffer[in] = item;
  73. in = (in+1) % buffer_size;
  74. elements_produced++;
  75.  
  76. return 0;
  77. }
  78.  
  79. int remove_item(){
  80. if (buffer[out] == -1){
  81. int start_idx = out;
  82. int x;
  83. for(x = out; x != start_idx; x++){
  84. x = (x+1) % buffer_size;
  85.  
  86. //Found our next non-negative index
  87. if(buffer[x] != -1){
  88. out = x;
  89. break;
  90. }
  91. }
  92. // Found no non-negative values to consume
  93. return -1;
  94. }
  95. buffer[out] = -1;
  96. out = (out+1) % buffer_size;
  97. elements_consumed++;
  98.  
  99. return 0;
  100. }
  101.  
  102. void *producer(void *threadid){
  103. int tid = (intptr_t)threadid;
  104. int nextProduced;
  105. long s_timer;
  106. while(TRUE) {
  107.  
  108. s_timer = rand() % 1000000;
  109. usleep(s_timer);
  110.  
  111. nextProduced = rand() % 10;
  112.  
  113. semaphore_wait(&empty);
  114. semaphore_wait(&mutex);
  115.  
  116. insert_item(nextProduced);
  117. print_buffer(1, tid, nextProduced);
  118.  
  119. semaphore_post(&mutex);
  120. semaphore_post(&full);
  121.  
  122. }
  123.  
  124. }
  125.  
  126. void *consumer(void *threadid){
  127. int tid = (intptr_t)threadid;
  128. int nextConsumed;
  129. long s_timer;
  130.  
  131. while(TRUE) {
  132.  
  133. semaphore_wait(&full);
  134. semaphore_wait(&mutex);
  135.  
  136. nextConsumed = buffer[out];
  137. remove_item();
  138. print_buffer(0, tid, nextConsumed);
  139.  
  140. semaphore_post(&mutex);
  141. semaphore_post(&empty);
  142.  
  143. s_timer = rand() % 1000000;
  144. usleep(s_timer);
  145. }
  146. }
  147.  
  148. void print_inital_info(){
  149. printf("Buffer Size: %d\n", buffer_size);
  150. printf("Time To Live (seconds): %d\n", sleep_time);
  151. printf("Number of Producer threads: %d\n", num_producer_threads);
  152. printf("Number of Consumer threads: %d\n", num_consumer_threads);
  153. printf("-------------------------------\n");
  154.  
  155. printf("Initial Buffer:\t [");
  156.  
  157. // Printing buffer values with carrots
  158. int j;
  159. for(j = 0; j < buffer_size; j++){
  160. printf("%d", buffer[j]);
  161. if(j == in){
  162. printf("^");
  163. }
  164. if(j == out){
  165. printf("v");
  166. }
  167. printf("\t");
  168. }
  169. printf("]\n");
  170. }
  171.  
  172. void print_buffer(int thread_type, int thread_id, int item_entered){
  173. // thread_type of 0 is consumer and 1 is producer
  174. if(thread_type){
  175. printf("Producer %d: Total %d, Value %d ", thread_id, elements_produced, item_entered);
  176. }
  177. else {
  178. printf("Consumer %d: Total %d, Value %d ", thread_id, elements_consumed, item_entered);
  179. }
  180.  
  181. // Printing the buffer values with carrots
  182. int j;
  183. printf("[");
  184. for(j = 0; j < buffer_size; j++){
  185. printf("%d", buffer[j]);
  186. if(j == in){
  187. printf("^");
  188. }
  189. if(j == out){
  190. printf("v");
  191. }
  192. printf("\t");
  193. }
  194. printf("]\n");
  195.  
  196. }
  197.  
  198. int init_buffer(){
  199. int y, ret;
  200.  
  201. buffer = malloc(buffer_size * sizeof(int));
  202.  
  203. // Init buffer to have all -1 values
  204. for(y = 0; y < buffer_size; y++){
  205. buffer[y] = -1;
  206. }
  207.  
  208. // Init semaphores
  209. if( 0 != (ret = semaphore_create(&mutex, 1)) ) {
  210. fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
  211. return -1;
  212. }
  213. if( 0 != (ret = semaphore_create(&full, 0)) ) {
  214. fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
  215. return -1;
  216. }
  217. if( 0 != (ret = semaphore_create(&empty, buffer_size)) ) {
  218. fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
  219. return -1;
  220. }
  221.  
  222. return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement