Answered step by step
Verified Expert Solution
Question
1 Approved Answer
what are the shared variables in the program below ( note: A variable x is shared if and only if multiple threads reference some instance
what are the shared variables in the program below note: A variable x is shared if and only if multiple threads reference some instance of x and what is the output of the program when the number of threads is and number of elements per thread is what about the case when number of threads is and the number of elements per thread is comment on the relative performance of these two cases:
#include "csapp.h
#define MAXTHREADS
void summutexvoid vargp;
long gsum ; Global variable automatically initialized to
long nelemsperthread;
semt mutex;
int mainint argc, char argv
long i nelems, lognelems, nthreads, myidMAXTHREADS;
pthreadt tidMAXTHREADS;
Get input arguments
if argc
printfUsage: s
argv;
exit;
nthreads atoiargv;
lognelems atolargv;
nelems L lognelems;
Check input arguments
if nelems nthreads lognelems
printfError: invalid nelems
;
exit;
Begin psumutex
nelemsperthread nelems nthreads;
seminit&mutex, ;
Create peer threads and wait for them to finish
for i ; i nthreads; i
myidi i;
pthreadcreate&tidi NULL, summutex, &myidi;
for i ; i nthreads; i
pthreadjointidi NULL;
Print final answer
printfresultld
gsum;
exit;
void summutexvoid vargp
long myid long vargp;
long start myid nelemsperthread;
long end start nelemsperthread;
long i;
for i start; i end; i
P&mutex;
gsum i;
V&mutex;
return NULL;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started