Question: #include #include #include #define SIZE 5 0 0 0 #define THREADS 8 int numbers [ SIZE ] ; int partial _ sums [ THREADS ]
#include #include #include #define SIZE #define THREADS int numbersSIZE; int partialsumsTHREADS; To store sum from each thread Function to read integers from file and store in global array void readfileconst char filename FILE file fopenfilenamer; if file NULL perrorUnable to open file"; exit; for int i ; i SIZE; i fscanffiled &numbersi; fclosefile; Thread function to compute the sum of a segment void computesumvoid arg int threadid int arg; int start threadid SIZE THREADS; int end start SIZE THREADS; for int i start; i end; i partialsumsthreadid numbersi; printfThread d Sum: d
threadid partialsumsthreadid; pthreadexit; int mainint argc, char argv if argc fprintfstderr "Usage: s
argv; return ; readfileargv; printfReading file: s
argv; pthreadt threadsTHREADS; int threadidsTHREADS; int totalsum ; Create threads for int i ; i THREADS; i threadidsi i; pthreadcreate&threadsi NULL, computesum, &threadidsi; Wait for threads to complete for int i ; i THREADS; i pthreadjointhreadsi NULL; totalsum partialsumsi; Sum up each thread's partial result printfTotal Sum is: d
totalsum; return ;
Reading file: input.txt
Thread Sum:
Thread Sum:
Thread Sum:
Thread Sum:
Thread Sum:
Thread Sum:
Thread Sum:
Thread Sum:
Total Sum is:
Why i got output like this
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
