#include <stdlib.h> /* NULL */ #include <stdio.h> /* printf */ #include <sys/types.h> /* pid_t */ #include <unistd.h> /* get_pid */ #include <stdlib.h> /* exit, EXIT_FAILURE */ #include <sys/wait.h> /* wait */ #include <pthread.h> #define PRODUCER_NO 5 //Number of producers, can change here #define NUM_PRODUCED 20 //2000 //Number of items to be produced, can change here void *generator_function(void*); void *print_function(void*); long sum; /* Sum of generated values*/ long finished_producers; /* number of the producer that finished producing */ pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER; int main(void) { int i; /* initialize random seed: */ sum = 0; finished_producers = 0; // A: Creates five generator threads int thread_return[PRODUCER_NO]; pthread_t Threads[PRODUCER_NO], Print_Thread; // Create a thread for the print function pthread_create(&Print_Thread, NULL, print_function, NULL); for (int i = 0; i < PRODUCER_NO; i++) { thread_return[i] = pthread_create(&Threads[i], NULL, generator_function, NULL); } // E: Makes sure that print thread has finished before proceeding pthread_join(Print_Thread, NULL); return (0); } void *generator_function(void* junk) { pthread_mutex_lock(&mutex1); // lock long counter = 0; long sum_this_generator = 0; while (counter < NUM_PRODUCED) { long tmpNumber = sum; long rnd_number = 1; //rand() % 10; // can make = 1, output should be 100 (20 loops*5 threads) printf("current sum of the generated number up to now is %ld going to add %ld to it.\n", tmpNumber, rnd_number); sum = tmpNumber + rnd_number; counter++; sum_this_generator += rnd_number; usleep(1000); } printf("The sum of produced items for this number generator at the end is: %ld \n", sum_this_generator); finished_producers++; pthread_mutex_unlock(&mutex1); // unlock // H: If all generator has finished, fire signal for condition variable if (finished_producers == PRODUCER_NO) { pthread_cond_signal(&condition_cond); } return (0); } void *print_function(void* junk) { pthread_mutex_lock(&mutex1); // G: Wait until all generator has finished while (finished_producers != PRODUCER_NO) { pthread_cond_wait(&condition_cond, &mutex1); } pthread_mutex_unlock(&mutex1); }
Standard input is empty
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~