Write a multithreaded program that calculates various statistical values for a l
ID: 3678694 • Letter: W
Question
Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers
90 81 78 95 79 72 85
The program will report
The average value is 82
The minimum value is 72
The maximum value is 95
The variables representing the average, minimum, and maximum values will be stored globally. The worker threads will set these values, and the parent thread will output the values once the workers have exited. (We could obviously expand this program by creating additional threads
that determine other statistical values, such as median and standard deviation.)
*** Integer should be 7 and then their average value, minimum value and maximum value need to be determine. ****
Explanation / Answer
#include #include #include #include /*Error handling for pthread_create and pthread_join*/ /*from the pthread_create man page*/ #define handle_error_en(en, msg) do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0) /* # of running threads */ volatile int running_threads = 0; pthread_t thread[3]; /*Descriptors for our 3 threads*/ int numOfElements;/*Total # of elements from the user*/ struct Results{ /*Struct to hold the statistical results*/ int min; int max; int average; }Results; /*This function finds the minimum element of an array*/ void *findMin(void *array_ptr){ int i; /*counter*/ int *elements = (int*)array_ptr; /*re reference void array pointer*/ Results.min = elements[0]; /*set minimum to first element */ for(i = 0; i 0){ /*Wait for each thread to decrement*/ sleep(1); } joinThreads(2); /*Call our thread joining function passing # of threads */ /*Prompt the user with our results*/ printf(" The average is %d The maximum is %d The minimum is %d ",Results.average, Results.max, Results.min); return(0); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.