In: Computer Science
please use linux or unix to complete, and include pictures of the output.
Modify the code below to implement the program that will sum up 1000 numbers using 5 threads.
1st thread will sum up numbers from 1-200
2nd thread will sum up numbers from 201 - 400
...
5th thread will sum up numbers from 801 - 1000
Make main thread wait for other threads to finish execution and sum up all the results.
Display the total to the user.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define N_OF_THREADS 4
void * print(void *tid){
printf("Hello %d\n", tid); pthread_exit(NULL);
}
int main(){
pthread_t threads[N_OF_THREADS];
int status, i; //
printf("Main. Creating thread %d\n", i);
for (int i=0; i<N_OF_THREADS; i++) {
printf("Main. Creating thread %d\n", i);
status = pthread_create(&threads[i], NULL, print, (void*)i);
if(status){
printf("Error %d\n", status);
exit(-1);
}
}
exit(NULL);
}
Look at my code and in case of indentation issues check the screenshots.
---------main.c---------------
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define N_OF_THREADS 5
struct range{ //create a structure to pass as
parameter to the thread
int tid; //The structure contains thread
id, the and the range of numbers to compute sum
int start;
int end;
};
int sum_values[N_OF_THREADS] = {0}; //create sum array with all values as 0, each thread will write its output to sum array at thread index
void *computeSum(void *param)
{
struct range *ptr = (struct range *)
param; //get the structure parameter as pointer
int i;
for(i = ptr->start; i <= ptr->end;
i++) //compute the sum from start to
end
sum_values[ptr->tid] =
sum_values[ptr->tid] + i; //update the sum array at
the thread index
//print threaad output
printf("\nThread %d computed sum from %d to %d, SUM =
%d", ptr->tid, ptr->start, ptr->end,
sum_values[ptr->tid]);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[N_OF_THREADS]; //create
thread ids as array
int status, i; //
for (i = 0; i < N_OF_THREADS; i++)
//loop for number of threads
{
printf("\nMain. Creating thread
%d", i);
struct range *r =
malloc(sizeof(struct range)); //create a structure
object pointer
r->tid = i;
//update the thread id
if(i == 0){
//update the range for
according to the thread id
r->start =
1;
r->end =
200;
}
else if(i == 1){
r->start =
201;
r->end =
400;
}
else if(i == 2){
r->start =
401;
r->end =
600;
}
else if(i == 3){
r->start =
601;
r->end =
800;
}
else if(i == 4){
r->start =
801;
r->end =
1000;
}
status =
pthread_create(&threads[i], NULL, computeSum, r); //start the
thread, pass structure parameter
if(status){
printf("Error
%d\n", status);
exit(-1);
}
}
for (i = 0; i < N_OF_THREADS; i++){
pthread_join(threads[i],
NULL); //main waits for all the threads to exit
}
int total = 0;
for (i = 0; i < N_OF_THREADS; i++){
total = total +
sum_values[i]; //main adds the sum of the values
computed by the threads
}
printf("\n\nThe sum of numbers from 1 to 1000 is
%d\n", total); //print total
exit(0);
}
----------Screenshots--------------
----------Output--------------
---------Updated answer-------------------------
----------------------------------------
Do comment if you need any clarification.
Please let me know if you are facing any issues.
Please Rate the answer if it helped.
Thankyou