Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a multithreaded program that calculates various statistical values for a l

ID: 674106 • Letter: W

Question

Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed three numbers on the command line and will then create three separate worker threads. The three numbers should be passed to each thread of the program through the thread creation function. one thread will determine the average of the number, 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 78 72

The program will report

The average value is 80

The minimum value is 72

The maximum value is 90

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.

Explanation / Answer

program

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>

#define handle_error_en(en, msg)
   do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

volatile int running_threads = 0;

pthread_t thread[3];

int numOfElements;

struct Results{

   int min;
   int max;
   int average;

}Results;

void *findMin(void *array_ptr){

   int i;

   int *elements = (int*)array_ptr;

   Results.min = elements[0];
   for(i = 0; i < numOfElements; i++){  

       if(elements[i] < Results.min){  

           Results.min = elements[i];  

       }

   }

   running_threads -= 1;  
return NULL;

}

void *findMax(void *array_ptr){

   int i;

   int *elements = (int*)array_ptr;

   for(i = 0; i < numOfElements; i++){

       if(elements[i] > Results.max){   

           Results.max = elements[i];

       }
   }

   running_threads -= 1;  
return NULL;

}

void *findAverage(void *array_ptr){

   int i;   

   int *elements = (int*)array_ptr;   

   for(i = 0; i < numOfElements; i++){   

       Results.average += elements[i];

   }

   Results.average = Results.average/numOfElements;

   running_threads -= 1;  

return NULL;

}

int getArrayInput(int n, int *array_ptr){
      
       int input;

       int numberOfElements = 0;

   printf("Creating Dynamic Array... - ");

       for(;;){

       printf("Enter a positive value: Negative Number to Stop - ");

           if (scanf("%d",&input) != 1){

               printf(" Oops that wasn't an Integer lets try filling the array again Remember INTEGERS only! ");

               exit(EXIT_FAILURE);
          
           }

       if (input >= 0){

           if (numberOfElements == n){

       n += 1;
      
       array_ptr = realloc(array_ptr, n * sizeof(int));
  
            }

       array_ptr[numberOfElements++] = input;
  
       } else {
  
        printf(" Number of Integers: %d ", numberOfElements);

        break;

                }

           }

   return numberOfElements;
  
       }


void joinThreads(int numberOfThreads){
  
   int i; /*count*/

   int s; /*error #*/

   while(numberOfThreads >= 0)

{   

       s = pthread_join(thread[numberOfThreads], NULL);

  
       if (s != 0){
          /*handle error*/
           handle_error_en(s, "pthread_create");
         
       }

       numberOfThreads--;

   }
  
}
  
void createThreads(int *array_ptr){
  
   int s;

    s = pthread_create(&thread[0], NULL, findMin, (void *)array_ptr);

   if (s != 0){

           handle_error_en(s, "pthread_create");
         
       }
          running_threads += 1;

   s = pthread_create(&thread[1], NULL, findMax, (void *)array_ptr);

       if (s != 0){
  
handle_error_en(s, "pthread_create");
   
    }
   running_threads += 1;

   s = pthread_create(&thread[2], NULL, findAverage, (void *)array_ptr);
            
       if (s != 0){

handle_error_en(s, "pthread_create");
   
    }
          
           running_threads += 1;

}


int main(){

   int n = 1; /* Initial Array Size*/

   int *array_ptr = malloc(n * sizeof(int));/*Initialize array pointer*/
      

       numOfElements = getArrayInput(n, array_ptr);
      
       createThreads(array_ptr);
      
       while(running_threads>0){   
  
               sleep(1);

           }

       joinThreads(2);


       printf(" The average is %d The maximum is %d The minimum is %d ",Results.average, Results.max, Results.min);

   return(0);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote