Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Common usage() prints out stuff for command-line help
  2. void usage() {
  3. fprintf(stderr, "usage: \n");
  4. fprintf(stderr, " -l <number of items each producer produces>\n");
  5. fprintf(stderr, " -m <size of the shared producer/consumer buffer>\n");
  6. fprintf(stderr, " -p <number of producers>\n");
  7. fprintf(stderr, " -c <number of consumers>\n");
  8. fprintf(stderr, " -P <sleep string: how each producer should sleep at various points in execution>\n");
  9. fprintf(stderr, " -C <sleep string: how each consumer should sleep at various points in execution>\n");
  10. fprintf(stderr, " -v [ verbose flag: trace what is happening and print it ]\n");
  11. fprintf(stderr, " -t [ timing flag: time entire execution and print total time ]\n");
  12. exit(1);
  13. }
  14. // Common main() for all four programs
  15. // - Does arg parsing
  16. // - Starts producers and consumers
  17. // - Once producers are finished, puts END_OF_STREAM
  18. // marker into shared queue to signal end to consumers
  19. // - Then waits for consumers and prints some final info
  20. int main(int argc, char *argv[]) {
  21. loops = 1;
  22. max = 1;
  23. consumers = 1;
  24. producers = 1;
  25. char *producer_pause_string = NULL;
  26. char *consumer_pause_string = NULL;
  27. opterr = 0;
  28. int c;
  29. while ((c = getopt (argc, argv, "l:m:p:c:P:C:vt")) != -1) {
  30. switch (c) {
  31. case 'l':
  32. loops = atoi(optarg);
  33. break;
  34. case 'm':
  35. max = atoi(optarg);
  36. break;
  37. case 'p':
  38. producers = atoi(optarg);
  39. break;
  40. case 'c':
  41. consumers = atoi(optarg);
  42. break;
  43. case 'P':
  44. producer_pause_string = optarg;
  45. break;
  46. case 'C':
  47. consumer_pause_string = optarg;
  48. break;
  49. case 'v':
  50. do_trace = 1;
  51. break;
  52. case 't':
  53. do_timing = 1;
  54. break;
  55. default:
  56. usage();
  57. }
  58. }
  59. assert(loops > 0);
  60. assert(max > 0);
  61. assert(producers <= MAX_THREADS);
  62. assert(consumers <= MAX_THREADS);
  63. if (producer_pause_string != NULL)
  64. parse_pause_string(producer_pause_string, "producers", producers, producer_pause_times);
  65. if (consumer_pause_string != NULL)
  66. parse_pause_string(consumer_pause_string, "consumers", consumers, consumer_pause_times);
  67. // make space for shared buffer, and init it ...
  68. buffer = (int *) Malloc(max * sizeof(int));
  69. int i;
  70. for (i = 0; i < max; i++) {
  71. buffer[i] = EMPTY;
  72. }
  73. do_print_headers();
  74. double t1 = Time_GetSeconds();
  75. // start up all threads; order doesn't matter here
  76. pthread_t pid[MAX_THREADS], cid[MAX_THREADS];
  77. int thread_id = 0;
  78. for (i = 0; i < producers; i++) {
  79. Pthread_create(&pid[i], NULL, producer, (void *) (long long) thread_id);
  80. thread_id++;
  81. }
  82. for (i = 0; i < consumers; i++) {
  83. Pthread_create(&cid[i], NULL, consumer, (void *) (long long) thread_id);
  84. thread_id++;
  85. }
  86. // now wait for all PRODUCERS to finish
  87. for (i = 0; i < producers; i++) {
  88. Pthread_join(pid[i], NULL);
  89. }
  90. // end case: when producers are all done
  91. // - put "consumers" number of END_OF_STREAM's in queue
  92. // - when consumer sees -1, it exits
  93. for (i = 0; i < consumers; i++) {
  94. Mutex_lock(&m);
  95. while (num_full == max)
  96. Cond_wait(empty_cv, &m);
  97. do_fill(END_OF_STREAM);
  98. do_eos();
  99. Cond_signal(fill_cv);
  100. Mutex_unlock(&m);
  101. }
  102. // now OK to wait for all consumers
  103. int counts[consumers];
  104. for (i = 0; i < consumers; i++) {
  105. Pthread_join(cid[i], (void *) &counts[i]);
  106. }
  107. double t2 = Time_GetSeconds();
  108. if (do_trace) {
  109. printf("\nConsumer consumption:\n");
  110. for (i = 0; i < consumers; i++) {
  111. printf(" C%d -> %d\n", i, counts[i]);
  112. }
  113. printf("\n");
  114. }
  115. if (do_timing) {
  116. printf("Total time: %.2f seconds\n", t2-t1);
  117. }
  118. return 0;
  119. }