Question: [ Counting ] Consider the following program using pthread. #include #include #include #include common.h #include common _ threads.h int max; volatile int counter
Counting Consider the following program using pthread.
#include
#include
#include
#include "common.h
#include "commonthreads.h
int max;
volatile int counter ; shared global variable
void mythreadvoid arg
char letter arg;
int i; stack private per thread
printfs: begin addr of i: p
letter, &i; Line
for i ; i max; i
counter counter ; shared: only one
printfs: done
letter;
return NULL;
int mainint argc, char argv
if argc
fprintfstderr "usage: mainfirst loopcount
;
exit;
max atoiargv;
pthreadt p p;
printfmain: begin counter d
counter,
unsigned int &counter;
Pthreadcreate&p NULL, mythread, A;
Pthreadcreate&p NULL, mythread, B;
join waits for the threads to finish
Pthreadjoinp NULL;
Pthreadjoinp NULL;
printfmain: done
counter: d
should: d
counter, max; Line
return ;
Suppose in a run, we get the output. Corresponding to Line we get the output from one
thread as follows:
A: begin addr of i: xfc
Can you infer the output from the other thread corresponding to Line If show
the output. If not, explain why.
This program takes the input from the command line for "max". In this run, we
have max as What is the output corresponding to Line
Does the above program have any problem? If no explain your reasoning. If yes, show
your solution to fix that you can copy and modify the code above with your changes briefly
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
