A multithreaded program to compute the sum of all numbers
between 1 and 1000 is needed. Each thread will compute the sum of
250 numbers. The main thread will then join the results. The
program is given below. Fill the missing parts and verify that the
program works correctly.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int threadSum1 = 0;
int threadSum2 = 0;
int threadSum3 = 0;
int threadSum4 = 0;
void *threadFun1(NULL)//
{
for(int i=1;i<250;i++)
}
void *threadFun2(NULL)//
{
for(int i=251;i<500;i++)
}
void *threadFun3(NULL)//
{
for(
)
}
void *threadFun4(NULL)//
{
for(
)
}
void main()
{
pthread_t thread1;
pthread_create(&thread1, NULL,
threadFun1, NULL);
pthread_join(thread1,NULL);
printf("Overall =
%d\n",threadSum1+threadSum2+threadSum3+threadSum4);
exit(0);
}
A multithreaded program to compute the sum of all numbers between 1 and 1000 is needed. Each thread will compute the sum
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am