Question: III. Consider the following program: int count = 1; //shared variable since its global void twiddledee() { int i=0; //for part b this will be
III. Consider the following program:
int count = 1; //shared variable since its global
void twiddledee() {
int i=0; //for part b this will be global and shared
for (i=0; i<2; i++) {
count = count*3; //assume count read from memory once
}
}
void twiddledum() {
int i=0; // for part b, this will be global and shared
for(i=0; i<2; i++) { count = count - 1;}
}
void main() {
thread_fork(twiddledee); //new thread starts twiddledee
twiddledum(); //main thread calls twiddledum
thread_join(); //waits until all the threads get here
print count; }
a) What are all the values that could be printed in main?
b) Repeat part 1 considering that i is also a shared variable (Bonus 3 points tricky!)
c) Describe a potential schedule of execution (i.e., how the threads interleave running) under the original assumptions where i is not shared that will result in the value printed out being equal to -3.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
