Question
Im writing a c program that is a multithread program that calculate various statistical values for a list of numbers. The three threads are seperate
Im writing a c program that is a multithread program that calculate various statistical values for a list of numbers. The three threads are seperate threads that are calculating the average, min and max of the numbers. The program will display the average value, minumum value and maximum value. The problem that i am having is calulating the average, minumum value and maximum value of the numbers using a buffer. here is a sample my code;
#include ", **argv); /*exit(1);*/ return -1; } if (atoi(argv[1]) < 0) { fprintf(stderr,"Argument %d must be non-negative ",atoi(argv[1])); /*exit(1);*/ return -1; } /* get the default attributes */ pthread_attr_init(&attr);
/* create the thread */ pthread_create(&average_id, &minimum_id,&maximum_id,&attr,average_num,maximum_num,minimum_num, argv[1]);
/* now wait for the thread to exit */ pthread_join( average_id,minimum_id,maximum_id,NULL); printf("The average value = %d ",average); printf("The maximum value = %d ", maximum); printf("The minimum value = %d ", minumum);
}
// Define the method average_value which takes one value as arguments to calculate of the intergers in values buffer. void *average_num (void *param) { // check if the values of the buffer exist if (values && size >0) { // index into the values buffer int index; //set the average to the 1st element average = values[0]; // For the rest of the values in the buffer for(index=1;index< size; index ++) // add the value to the average average+= values[index]; // divide by size to get the average average /= size;
} // exit from this thread pthread.exit(0); } // Define the method minimum_value which takes one value as arguments to calculate of the intergers in values buffer. void * minimum_num(void *param) { // check if the values of the buffer exist if (values && size >0) { // index into the values buffer int index; // set the minimum to the 1st element minumum =values [0]; // for the rest of the values in the buffer for(index=1; index < size; index++) // check the minimum if(minimum >values[index]) // update the value of the minimum minimum=values[index]; } // exit from this thread pthread.exit(0); }
// Define the method maximum_value which takes one value as arguments to calculate of the intergers in values buffer. void * maximum_num(void *param) { // check if the values of the buffer exist if (values && size >0) { // index into the values buffer int index; // set the maximum to the 1st element maximum=values [0]; // for the rest of the values in the buffer for(index=1; index < size; index++) // check the maximum if(maximum < values[index]) // update the value of the maximum maximum=values[index]; } pthread.exit(0); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started