Question: Problem 9 . ( 8 points ) : Concurrency and sharing. Consider a concurrent C program with two threads, where each thread execut the following

Problem 9.(8 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 */
cnt=cnt+1;
Suppose that this line of C 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 cnt and othe
will not.
Let Li denote the execution of instruction L by thread i; similarly for Ui and Si. Each of the sequenc
shown below gives a possible ordering of the instructions when the two threads execute. Assuming that cr
is initially zero, what is the value of cnt in memory after each of the sequences completes?
A., cnt =0;,L1,U1,S1,L2,U2,S2, cnt ==
B.cnt=0;,L1,U1,L2,S1,U2,S2,cnt==
C.,cnt=0;,L2,U2,S2,L1,U1,S1, cnt ==
D., cnt =0;,L1,L2,U1,S1,U2,S2, cnt ==The following C program and declarations are part of the next problem. For each part, the three comment
lines
??**LINE1**?
??**LINE2**?
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 -2
void foo
counter ++
printf ("%id", counter):
int maino {
pthread_t tid[2]:
int i :
for
??**LINE1**?
??**LINE2**?
1**LINE3**
}
counter++
counter"+: printf("%g", counter):
(Feel free to remove this page from your exam packet for easy reference.)
Problem 7.(8 points):
This problem tests your understanding of the differences between processes and threads.
Part 1
Suppose the following code replaces the three comment lines in the program on the previous page:
LINE 1
LINE 2
Pthread_create(&tid [i],0, foo, 0):
What is the first number that gets printed on stdout? Circle only one answer.
3
45
Could be elther 3 or 4 or 5
Could be elther 3 or 4
Part 2
Suppose the following code replaces the three comment lines.
LINE 1: Pthread_create (&tid [i],0, foo, 0):
LINE 2: Pthread_join(tid [i], O):
What is the first number that gets printed on stdout? Circle only one answer.
3
345
Could be elther 3 or 4 or 5
Could be elther 3 or 4
 Problem 9.(8 points): Concurrency and sharing. Consider a concurrent C program

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!