Consider the following portions of two different programs running at the same ti
ID: 3815914 • Letter: C
Question
Consider the following portions of two different programs running at the same time on four processors in a symmetric multicore processor(SMP). Assume that before this code is run, both x and y are 0.
Core 1 : X = 2;
Core 2 : Y = 2;
Core 3 : W = X + Y + 1;
Core 4 : Z = X + Y;
What are the possible resulting values of W, X, Y and Z? For each possible outcome, explain how we might arrive at those values. You will need to examine all possible interleaving of instructions.
How could you make the execution more deterministic so that only one set of values is possible?
Explanation / Answer
There are mainly 4 outcomes which may take place in the processor execution.
Initial values of the variables are
X=0, Y=0
The different cases of outcome might be due to values of X and Y only as all other results are depends on the values of X and Y. So the situations arised due to values of X and Y are
Case 1:
In this case values of x and y may be treated as
X=0 and Y=0
in this case
X=0,
Y=0,
W=1,
Z=0.
Results are due to values of X and Y are not updated and reffered by the other variables.
We can use wait -notify or synchronization mechanisma to resolve the issue.
Case 2:
Values of the variables might be reffered as
X=0,
Y=2
Dependent variables will be as
X=0
Y=2
W=3
Z=2
Use some synchroniztion techniques to keep the consistency of the data.
Case 3:
Values of the variables might be reffered as
X=2,
Y=0
Dependent variables will be as
X=2
Y=0
W=3
Z=2
Use some synchroniztion techniques to keep the consistency of the data.
Case 4:
Values of the variables might be reffered as
X=0,
Y=2
Dependent variables will be as
X=2
Y=2
W=5
Z=4
this is proper execution by the processor as correct values of the variables are reffered. Here no need of the any synchronization.
- Here dependent variables should refer the values of the variables once they are properly updated. Here Core 1 and Core 2 should update first then only core 3 and core 4 should refer to it.
-It can be achieved by synchronization methods or by removing the data dependancies here.