I’m writing a multi-threaded program in c that takes in a list of numbers from the command line, then uses 3 seperate threads to get the average, max number, and min number. I completed it, but I’m not getting the correct output and I’m quite confused, because the reason it’s not working is because my index variable in my threads is not incrementing. It just stays at 0 no matter what, even when trying both a while and for loop. Here is my code:
Note: I’m coding on Linux
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
int sum =0;
int num;
int count;
int max;
int min;
float average;
void* Thread_Avg(void *arg)
{
int i = 0;
int *array = (int *)arg;
while (i < count)
{
printf("%.fn",i);
sum += array[i];
i +=1;
}
average = sum/count;
pthread_exit(0);
}
void* Thread_max(void *arg)
{
i = 0;
int *array = (int *)arg;
for (i = 0; i < count; i++)
//printf("%.f",max);
{
if(i ==0)
{
max = array[i];
}
else if(max < array[i])
{
max = array[i];
}
}
pthread_exit(0);
}
void* Thread_min(void *arg)
{
int i = 0;
int *array = (int *)arg;
for (i = 0; i < count; i++)
{
if (array[i] < min)
{
min = array[i];
}
}
pthread_exit(0);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <at least one integer as input>n", argv[0]);
return 0;
}
int *num = (int *)malloc((argc-1)*sizeof(int));
int i;
for (i =1; i <argc; i++)
{
num[i-1] = atoi(argv[i]);
count++;
}
//count = argc;
pthread_t thread1, thread2, thread3;
pthread_create(&thread1, NULL, Thread_Avg, (void *)num);
pthread_create(&thread2, NULL, Thread_max, (void *)num);
pthread_create(&thread3, NULL, Thread_min, (void *)num);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
printf("The average value is %.fn", average);
printf("The minimum value is %.fn", min);
printf("The maximum value is %.fn", max);
return 0;
}
incorrect Output: (the zeroes are just from my print statement to check if i was actually being incremented, which it’s not. It’s stuck at 0) My average, max, and min are also not calculating properly
gcc Program.c -pthread
os@debian:~$ ./a.out 1 2 3 4 5
0
0
0
0
The average value is 2
The minimum value is 2
The maximum value is 2
3
Answers
You probably need to read a lot more about how threads work and what they are useful for. Your threads are sharing global variables (including i), and updating them without any regard to the other threads that are relying upon them.
If you change the section of your main program to this:
Do you get the expected results? Yes, so the next step is why. It is because they are no longer simultaneously updating / relying on these global variables. So, your task is:
Also, programs are meant to be read. Indenting is a basic visual organisation which enables people to rapidly grasp the concepts a program is expressing. Please learn about it; but until you do, imitate the masters, like the authors of C (Kerningham and Ritchie); or at the very least, install indent and run your code through it periodically. You will be amazed how much easier it is to understand a program that has a reasonable notion of indentation.
I tried to run it on my machine and it works:
Your
min
algorithm won’t work correctly as themin
variable is initialized to0
. If all input values are >0, yourmin
will be incorrectly0
. The algorithm formax
has the extra check and works correctly.You also have a few
printf
format bugs in your program that will lead to incorrect display of values. You should enable warnings in your compiler, for instance by adding the-Wall
flag or equivalent, to help identify these.printf("%.fn",i);
should beprintf("%dn",i);
printf("The minimum value is %.fn", min);
should beprintf("The minimum value is %dn", min);
printf("The maximum value is %.fn", max);
should beprintf("The maximum value is %dn", max);