fork download
  1. #include <stdlib.h> /* NULL */
  2. #include <stdio.h> /* printf */
  3. #include <sys/types.h> /* pid_t */
  4. #include <unistd.h> /* get_pid */
  5. #include <stdlib.h> /* exit, EXIT_FAILURE */
  6. #include <sys/wait.h> /* wait */
  7. #include <pthread.h>
  8.  
  9. #define PRODUCER_NO 5 //Number of producers, can change here
  10. #define NUM_PRODUCED 20 //2000 //Number of items to be produced, can change here
  11.  
  12. void *generator_function(void*);
  13. void *print_function(void*);
  14. long sum; /* Sum of generated values*/
  15. long finished_producers; /* number of the producer that finished producing */
  16.  
  17. pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
  18. pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER;
  19.  
  20. int main(void) {
  21. int i;
  22. /* initialize random seed: */
  23. srand(time(NULL));
  24. sum = 0;
  25. finished_producers = 0;
  26.  
  27. // A: Creates five generator threads
  28. int thread_return[PRODUCER_NO];
  29. pthread_t Threads[PRODUCER_NO], Print_Thread;
  30.  
  31. // Create a thread for the print function
  32. pthread_create(&Print_Thread, NULL, print_function, NULL);
  33.  
  34. for (int i = 0; i < PRODUCER_NO; i++) {
  35. thread_return[i] = pthread_create(&Threads[i], NULL, generator_function, NULL);
  36. }
  37.  
  38. // E: Makes sure that print thread has finished before proceeding
  39. pthread_join(Print_Thread, NULL);
  40.  
  41. return (0);
  42. }
  43.  
  44. void *generator_function(void* junk) {
  45. pthread_mutex_lock(&mutex1); // lock
  46.  
  47. long counter = 0;
  48. long sum_this_generator = 0;
  49.  
  50. while (counter < NUM_PRODUCED) {
  51. long tmpNumber = sum;
  52. long rnd_number = 1; //rand() % 10;
  53. // can make = 1, output should be 100 (20 loops*5 threads)
  54. printf("current sum of the generated number up to now is %ld going to add %ld to it.\n", tmpNumber, rnd_number);
  55. sum = tmpNumber + rnd_number;
  56. counter++;
  57. sum_this_generator += rnd_number;
  58. usleep(1000);
  59. }
  60.  
  61. printf("--+---+----+----------+---------+---+--+---+------+----\n");
  62. printf("The sum of produced items for this number generator at the end is: %ld \n", sum_this_generator);
  63. printf("--+---+----+----------+---------+---+--+---+------+----\n");
  64. finished_producers++;
  65. pthread_mutex_unlock(&mutex1); // unlock
  66.  
  67. // H: If all generator has finished, fire signal for condition variable
  68. if (finished_producers == PRODUCER_NO) {
  69. pthread_cond_signal(&condition_cond);
  70. }
  71.  
  72. return (0);
  73. }
  74.  
  75. void *print_function(void* junk) {
  76. pthread_mutex_lock(&mutex1);
  77.  
  78. // G: Wait until all generator has finished
  79. while (finished_producers != PRODUCER_NO) {
  80. pthread_cond_wait(&condition_cond, &mutex1);
  81. }
  82.  
  83. printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  84. printf("The value of counter at the end is: %ld \n", sum);
  85. printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
  86.  
  87. pthread_mutex_unlock(&mutex1);
  88. }
  89.  
Success #stdin #stdout 0s 5300KB
stdin
Standard input is empty
stdout
current sum of the generated number up to now is 0 going to add 1 to it.
current sum of the generated number up to now is 1 going to add 1 to it.
current sum of the generated number up to now is 2 going to add 1 to it.
current sum of the generated number up to now is 3 going to add 1 to it.
current sum of the generated number up to now is 4 going to add 1 to it.
current sum of the generated number up to now is 5 going to add 1 to it.
current sum of the generated number up to now is 6 going to add 1 to it.
current sum of the generated number up to now is 7 going to add 1 to it.
current sum of the generated number up to now is 8 going to add 1 to it.
current sum of the generated number up to now is 9 going to add 1 to it.
current sum of the generated number up to now is 10 going to add 1 to it.
current sum of the generated number up to now is 11 going to add 1 to it.
current sum of the generated number up to now is 12 going to add 1 to it.
current sum of the generated number up to now is 13 going to add 1 to it.
current sum of the generated number up to now is 14 going to add 1 to it.
current sum of the generated number up to now is 15 going to add 1 to it.
current sum of the generated number up to now is 16 going to add 1 to it.
current sum of the generated number up to now is 17 going to add 1 to it.
current sum of the generated number up to now is 18 going to add 1 to it.
current sum of the generated number up to now is 19 going to add 1 to it.
--+---+----+----------+---------+---+--+---+------+----
The sum of produced items for this number generator at the end is: 20 
--+---+----+----------+---------+---+--+---+------+----
current sum of the generated number up to now is 20 going to add 1 to it.
current sum of the generated number up to now is 21 going to add 1 to it.
current sum of the generated number up to now is 22 going to add 1 to it.
current sum of the generated number up to now is 23 going to add 1 to it.
current sum of the generated number up to now is 24 going to add 1 to it.
current sum of the generated number up to now is 25 going to add 1 to it.
current sum of the generated number up to now is 26 going to add 1 to it.
current sum of the generated number up to now is 27 going to add 1 to it.
current sum of the generated number up to now is 28 going to add 1 to it.
current sum of the generated number up to now is 29 going to add 1 to it.
current sum of the generated number up to now is 30 going to add 1 to it.
current sum of the generated number up to now is 31 going to add 1 to it.
current sum of the generated number up to now is 32 going to add 1 to it.
current sum of the generated number up to now is 33 going to add 1 to it.
current sum of the generated number up to now is 34 going to add 1 to it.
current sum of the generated number up to now is 35 going to add 1 to it.
current sum of the generated number up to now is 36 going to add 1 to it.
current sum of the generated number up to now is 37 going to add 1 to it.
current sum of the generated number up to now is 38 going to add 1 to it.
current sum of the generated number up to now is 39 going to add 1 to it.
--+---+----+----------+---------+---+--+---+------+----
The sum of produced items for this number generator at the end is: 20 
--+---+----+----------+---------+---+--+---+------+----
current sum of the generated number up to now is 40 going to add 1 to it.
current sum of the generated number up to now is 41 going to add 1 to it.
current sum of the generated number up to now is 42 going to add 1 to it.
current sum of the generated number up to now is 43 going to add 1 to it.
current sum of the generated number up to now is 44 going to add 1 to it.
current sum of the generated number up to now is 45 going to add 1 to it.
current sum of the generated number up to now is 46 going to add 1 to it.
current sum of the generated number up to now is 47 going to add 1 to it.
current sum of the generated number up to now is 48 going to add 1 to it.
current sum of the generated number up to now is 49 going to add 1 to it.
current sum of the generated number up to now is 50 going to add 1 to it.
current sum of the generated number up to now is 51 going to add 1 to it.
current sum of the generated number up to now is 52 going to add 1 to it.
current sum of the generated number up to now is 53 going to add 1 to it.
current sum of the generated number up to now is 54 going to add 1 to it.
current sum of the generated number up to now is 55 going to add 1 to it.
current sum of the generated number up to now is 56 going to add 1 to it.
current sum of the generated number up to now is 57 going to add 1 to it.
current sum of the generated number up to now is 58 going to add 1 to it.
current sum of the generated number up to now is 59 going to add 1 to it.
--+---+----+----------+---------+---+--+---+------+----
The sum of produced items for this number generator at the end is: 20 
--+---+----+----------+---------+---+--+---+------+----
current sum of the generated number up to now is 60 going to add 1 to it.
current sum of the generated number up to now is 61 going to add 1 to it.
current sum of the generated number up to now is 62 going to add 1 to it.
current sum of the generated number up to now is 63 going to add 1 to it.
current sum of the generated number up to now is 64 going to add 1 to it.
current sum of the generated number up to now is 65 going to add 1 to it.
current sum of the generated number up to now is 66 going to add 1 to it.
current sum of the generated number up to now is 67 going to add 1 to it.
current sum of the generated number up to now is 68 going to add 1 to it.
current sum of the generated number up to now is 69 going to add 1 to it.
current sum of the generated number up to now is 70 going to add 1 to it.
current sum of the generated number up to now is 71 going to add 1 to it.
current sum of the generated number up to now is 72 going to add 1 to it.
current sum of the generated number up to now is 73 going to add 1 to it.
current sum of the generated number up to now is 74 going to add 1 to it.
current sum of the generated number up to now is 75 going to add 1 to it.
current sum of the generated number up to now is 76 going to add 1 to it.
current sum of the generated number up to now is 77 going to add 1 to it.
current sum of the generated number up to now is 78 going to add 1 to it.
current sum of the generated number up to now is 79 going to add 1 to it.
--+---+----+----------+---------+---+--+---+------+----
The sum of produced items for this number generator at the end is: 20 
--+---+----+----------+---------+---+--+---+------+----
current sum of the generated number up to now is 80 going to add 1 to it.
current sum of the generated number up to now is 81 going to add 1 to it.
current sum of the generated number up to now is 82 going to add 1 to it.
current sum of the generated number up to now is 83 going to add 1 to it.
current sum of the generated number up to now is 84 going to add 1 to it.
current sum of the generated number up to now is 85 going to add 1 to it.
current sum of the generated number up to now is 86 going to add 1 to it.
current sum of the generated number up to now is 87 going to add 1 to it.
current sum of the generated number up to now is 88 going to add 1 to it.
current sum of the generated number up to now is 89 going to add 1 to it.
current sum of the generated number up to now is 90 going to add 1 to it.
current sum of the generated number up to now is 91 going to add 1 to it.
current sum of the generated number up to now is 92 going to add 1 to it.
current sum of the generated number up to now is 93 going to add 1 to it.
current sum of the generated number up to now is 94 going to add 1 to it.
current sum of the generated number up to now is 95 going to add 1 to it.
current sum of the generated number up to now is 96 going to add 1 to it.
current sum of the generated number up to now is 97 going to add 1 to it.
current sum of the generated number up to now is 98 going to add 1 to it.
current sum of the generated number up to now is 99 going to add 1 to it.
--+---+----+----------+---------+---+--+---+------+----
The sum of produced items for this number generator at the end is: 20 
--+---+----+----------+---------+---+--+---+------+----
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The value of counter at the end is: 100 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~