Question: 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:
#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
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
