• edited by
24,324 views
70 70 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______________________.

7 Answers

Best answer
77 77 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
70 70 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 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}.

1 1 vote

Observe closely, In both P1 and P2 writing to B is happening only at second(final) statement.


Case I: P2 value get's overwritten by P1 $\Rightarrow$ Only P1

It's like in effect only P1 executes. With B=2 as initial value, final value will be 2.

Case II: P1 value gets overwritten by P2 $\Rightarrow$ Only P2

With B=2 as initial value final value will be 3.

Case III: P2 executes first and writes the value which is read by P1 $\Rightarrow$ P2 then P1

P2 writes 3 as in Case II. With B=3 P1 writes final value 4.

Case IV: P1 executes first and writes the value which is read by P2 $\Rightarrow$ P1 then P2

P1 writes value 2 as in Case I which is same as initial value. So as in Case II P2 writes final value 3 with initial value 2.


So in effect there are only 3 possible values which are {2,3,4}. Answer: 3

0 0 votes
Since no while loops are present, only 2 values are taken by B.

P1- P2 : 3

P2 - P1: 4

Answer: 2
2 flags:
✌ Edit necessary (DKDriftKing)
✌ Low quality (saismrutiranjan18)
Answer:
Position:
Show:

Related questions

79 79 votes
11 answers 11 answers
34.7k
34.7k views
Misbah Ghaya asked Feb 13, 2015
34,726 views
Consider a disk pack with a seek time of $4$ milliseconds and rotational speed of $10000$ rotations per minute (RPM). It has $600$ sectors per track and each sector can s...
183 183 votes
17 answers 17 answers
67.5k
67.5k views
Misbah Ghaya asked Feb 13, 2015
67,542 views
Consider a uniprocessor system executing three tasks $T_{1}, T_{2}$ and $T_{3}$ each of which is composed of an infinite sequence of jobs (or instances) which arrive peri...
56 56 votes
7 answers 7 answers
43.7k
43.7k views
Misbah Ghaya asked Feb 13, 2015
43,724 views
Suppose the following disk request sequence (track numbers) for a disk with $100$ tracks is given:$45, 20, 90, 10, 50, 60, 80, 25, 70.$Assume that the initial position of...
39 39 votes
2 answers 2 answers
24.9k
24.9k views
Misbah Ghaya asked Feb 13, 2015
24,870 views
Consider a main memory with five-page frames and the following sequence of page references: $\text{3, 8, 2, 3, 9, 1, 6, 3, 8, 9, 3, 6, 2, 1, 3}$. Which one of the followi...