Write a multithreaded program that calculates the sum of a list of numbers. This program will be passed a series of numbers (5 Numbers exactly) on the command line at the run time and will then create one worker thread that will find the sum of the 5 values, while the parent thread will output (print) the sum value once the worker thread has exited. The variable representing the sum will be stored globally. For example, suppose your program is passed the integers: 7 8 9 10 11 The program will show the following output: The sum is 45.
and i solved that i will put the answer in the last but the part 2 of the example i cant understand it and cant solve it please help:
Modify your code in part (a) by dividing the sum job between two threads. Each thread will be passed the 5 numbers, then the first thread will sum up the even numbers only (i.e. 8 and 10 in the previous example), while the second thread will sum up the odd numbers (i.e. 7, 9, 11 in the previous example). The summation is being done on the same sum global variable. Another global variable called counter is used to count the numbers (even or odd) that have been summed up. The value of counter is incremented and then printed out every time a number (even or odd) is added to the sum variable. The parent thread will output (print) the final sum value and the final counter value once the worker threads have exited. The program will show the following output for the previous example (note that TN is either 1 or 2) : The counter value is 1 and incremented by thread TN
What I have tried:
#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[1];
int numOfElements;
struct Results{
int sum;
}Results;
void *findsum(void *array_ptr){
int i;
int *elements = (int*)array_ptr;
for(i = 0; i < numOfElements; i++){
Results.sum += elements[i];
}
running_threads -= 1;
return NULL;
}
int getArrayInput(int n, int *array_ptr){
int input;
int numberOfElements = 0;
printf("Creating Dynamic Array...\n-\n");
for(;;){
printf("Enter a positive value:\nNegative Number to Stop\n-\n");
if (scanf("%d",&input) != 1){
printf("\nOops that wasn't an Integer\nlets try filling the array again\nRemember INTEGERS only!\n");
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("\nNumber of Integers: %d\n", numberOfElements);
break;
}
}
return numberOfElements;
}
void createThreads(int *array_ptr){
int s;
s = pthread_create(&thread[2], NULL, findsum, (void *)array_ptr);
if (s != 0){
handle_error_en(s, "pthread_create");
}
running_threads += 1;
}
int main(){
int n = 1;
int *array_ptr = malloc(n * sizeof(int));
numOfElements = getArrayInput(n, array_ptr);
createThreads(array_ptr);
while(running_threads>0){
sleep(1);
}
printf("\nThe sum is %d\n",Results.sum);
return(0);
}