edited by
12,171 views
48 votes
48 votes
The following two functions $P1$ and $P2$ that share a variable $B$ with an initial value of $2$ execute concurrently.
$$\begin{array}{|l|l|}\hline  \text{P1() \{ } & \text{P2()\{} \\  \text{ C = B - 1;} & \text{ D = 2 * B; } \\  \text{ B = 2 * C;} & \text{ B = D - 1;} \\ \text{\}} & \text{\}} \\\hline \end{array}$$
The number of distinct values that $B$ can possibly take after the execution is______________________.
edited by

6 Answers

Best answer
57 votes
57 votes

$3$ distinct values $\{2,3,4\}$
$$\begin{array}{|ll | l| }\hline & \text{P1() \{ } & \text{P2()\{} \\  a. &\text{ C = B - 1;} & \text{ D = 2 * B; } \\b.&  \text{ B = 2 * C;} & \text{ B = D - 1;} \\& \text{\}} & \text{\}}\\\hline \end{array}$$

  1. $P1_{a}\to P1_b \to P2_{a} \to P2_b \implies B = 3$
  2. $P2_a\to P2_b \to P1_a \to P1_b  \implies B = 4$
  3. $P1_a \to P2_a \to P2_b \to P1_b \implies B = 2$
edited by
52 votes
52 votes

Let P1(){ A, B } and  P2(){ 1, 2 }

Now possible sequence and value of variables B, C and D are as follow -->

  1. A B 1 2   {3,1,4}
  2. A 1 B 2   {3,1,4}
  3. A 1 2 B   {2,1,4}
  4. 1 A B 2   {3,1,4}
  5. 1 A 2 B   {2,1,4}
  6. 1 2 A B   {4,2,4}

So Number of distinct values that B can possibly take after the execution is 3 {2,3,4}.

PS: Little bit long but error free method. 

5 votes
5 votes

Order Of Execution : B  value

P1-P2: B = 3
P2-P1: B = 4
P1-P2-P1: B = 2
P2-P1-P2: B = 3

Total distinct values  {2,3,4}.

Answer:

Related questions

31 votes
31 votes
2 answers
4