Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

What is the relationship between negative thinking and depression?

Answered: 1 week ago

Question

Explain the pages in white the expert taxes

Answered: 1 week ago

Question

Formulate strategies and recommendations for action on HRM issues.

Answered: 1 week ago