Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include static int *values = NULL; // buffer to hold input values static int size =0; // buffer size static float average; // average of numbers static int minimum; //minimum of list of numbers static int maximum; // maximum of list of numbers //Declare the method of initializing the buffer values which takes two values as arguments int num(int argc, char **argv); // Declare the method for the thread which calculates the average of the list of number and it takes on parameter. void *average_num(void *param); //Declare the method of initializing the buffer values which takes two values as arguments int num(int argc, char **argv); // Declare the method for the thread that calculates the minimum value. void *minimum_num(void *param); //Declare the method for the thread that calculates the maximum value. void *maximum_num(void *param); // Define the main method of the program int main(int argc, char **argv) { // the ids of the threads that calculate the average, minimum and maximum pthread_t average_id, minimum_id,maximum_id; // the pthreads attribute pthread_attr_t attr; int return_values; // the return value of funtions if(argc !=2) { fprintf(stderr,"usage: a.out ", **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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

ISBN: 3319234609, 978-3319234601

More Books

Students also viewed these Databases questions

Question

Consider this article:...

Answered: 1 week ago