A multithreaded program to compute the sum of all numbers between 1 and 1000 is needed. Each thread will compute the sum

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

A multithreaded program to compute the sum of all numbers between 1 and 1000 is needed. Each thread will compute the sum

Post by answerhappygod »

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);
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply