For this assignment you are to write a Pthread program that consists of 3 thread
ID: 3714148 • Letter: F
Question
For this assignment you are to write a Pthread program that consists of 3 threads. The first thread will sum the digits counting by 3’s, the second thread will sum the digits counting by 7’s and the third thread with sum the digits counting by 11’s. You can use the following defines to specify the maximum value to use in the respective series: #define MAX_VALUE 10000 You should max sure you value in the series is strictly less than the specified max value, e.g., for (i = 0; i < MAX_VALUE; …).. You should use only one thread function. This will require you to pass in the number to count by for each thread instance, i.e., 3, 7, and 11. Your program should return the sum of each of these series: 3+6+9+...+ = sum3 7+14+21+…+ = sum7 11+22+33+…+ = sum11 Result = sum3 + sum7 + sum11 Make sure you don’t use any global variables within the loop of the thread code.
Explanation / Answer
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>
#define MAX_VALUE 10000
pthread_t t[3];
void *t1(void *arg)
{
int *count=(int*)arg;
int i=0;
long int sum_=0;
for(i=0;i<MAX_VALUE;i+=(*count))
{
sum_=sum_+i;
}
printf("%ld ",sum_);
return (void *)sum_;
}
int main()
{
int sum3=3,sum7=7,sum11=11;
void *ret3,*ret7,*ret11;
long int sum=0;
long int a,b,c;
pthread_create(&t[0],NULL,t1,(void*)&sum3);
pthread_create(&t[1],NULL,t1,(void*)&sum7);
pthread_create(&t[2],NULL,t1,(void*)&sum11);
pthread_join(t[0],&ret3);
pthread_join(t[1],&ret7);
pthread_join(t[2],&ret11);
a=(long int*)ret3;
b=(long int*)ret7;
c=(long int*)ret11;
printf(" SUM3+SUM7+Sum11=%ld",a+b+c);
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.