Question: Problem 9 . ( 8 points ) : Concurrency and sharing. Consider a concurrent C program with two threads, where each thread execut the following
Problem points:
Concurrency and sharing. Consider a concurrent C program with two threads, where each thread execut
the following line of code:
Increment the shared global variable cnt
;
Suppose that this line of code compiles to the following assembly language instructions:
At runtime, the operating system kernel will choose some ordering of these instructions. Since we are n
explicitly synchonizing the threads, some of these orderings will produce the correct value for and othe
will not.
Let denote the execution of instruction by thread ; similarly for and Each of the sequenc
shown below gives a possible ordering of the instructions when the two threads execute. Assuming that
is initially zero, what is the value of cnt in memory after each of the sequences completes?
A cnt ; cnt
B;
C; cnt
D cnt ; cnt The following program and declarations are part of the next problem. For each part, the three comment
lines
will be replaced with different fragments of code. For space reasons, we are not checking error return
codes, so assume that all functions return normally.
int counter
void foo
counter
printf id counter:
int maino
pthreadt tid:
int :
for
counter
counter": printfg counter:
Feel free to remove this page from your exam packet for easy reference.
Problem points:
This problem tests your understanding of the differences between processes and threads.
Part
Suppose the following code replaces the three comment lines in the program on the previous page:
LINE
LINE
Pthreadcreate&tid i foo, :
What is the first number that gets printed on stdout? Circle only one answer.
Could be elther or or
Could be elther or
Part
Suppose the following code replaces the three comment lines.
LINE : Pthreadcreate &tid i foo, :
LINE : Pthreadjointid i O:
What is the first number that gets printed on stdout? Circle only one answer.
Could be elther or or
Could be elther or
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
