Answer: 3
Let the instructions of processes P1 and P2 be labeled as follows:
- P1:
- I1:
C = B - 1
- I2:
B = 2 * C
- P2:
- I3:
D = 2 * B
- I4:
B = D - 1
Since C and D are local variables and B is a shared variable (initially B=2), we can trace all (24)=6 possible concurrent interleavings of these instructions:
- I1→I2→I3→I4:
- I1⟹C=2−1=1
- I2⟹B=2×1=2
- I3⟹D=2×2=4
- I4⟹B=4−1=3
- I3→I4→I1→I2:
- I3⟹D=2×2=4
- I4⟹B=4−1=3
- I1⟹C=3−1=2
- I2⟹B=2×2=4
- I1→I3→I2→I4:
- I1⟹C=2−1=1
- I3⟹D=2×2=4
- I2⟹B=2×1=2
- I4⟹B=4−1=3
- I1→I3→I4→I2:
- I1⟹C=2−1=1
- I3⟹D=2×2=4
- I4⟹B=4−1=3
- I2⟹B=2×1=2
- I3→I1→I2→I4:
- I3⟹D=2×2=4
- I1⟹C=2−1=1
- I2⟹B=2×1=2
- I4⟹B=4−1=3
- I3→I1→I4→I2:
- I3⟹D=2×2=4
- I1⟹C=2−1=1
- I4⟹B=4−1=3
- I2⟹B=2×1=2
The set of distinct values that B can take after execution is {2,3,4}. Thus, there are 3 distinct values.