3.2k views
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{} & \text{} \\ \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 | 3.2k views

$3$ distinct values $\{2,3,4\}$

$P1-P2: B = 3$
$P2-P1: B = 4$
$P1-P2-P1: B = 2$
by Active (5k points)
edited by
0
How the value 2 came,pls explain
+6

execute P1 -

C = B - 1;

now c =1

let suppose now P2 executing   because of concurrent process-

P2(){
D = 2 * B;
B = D - 1;
} 

now B= 3

after this P1 executes second instruction-

B = 2 * C;

which is B= 2*1=2

+2
We should also add the case where P2-P1-P2 . Yes the final value comes out to be B = 3 , which is already present , but still 4 cases to check in total right ?
0
is it a hit and trial  process to find out the number of distinct values .Is  there is no sure shot way which can be applied to n processes?
0
p1

c=1

b=1

p2

d=2

b=1????????it is right or wrong and why?plz tell this??????

Guys, i am asking that 1 is also possible na???? but  not given in option its fine just for knowledge i am asking so tell me yes or no plz?????
0
how p1 - p2 =2??

i am thinking

in p1= (B = 2)

in p2=(B=3)

so p1-p2 = 2 - 3 = -1??

+1
Ankit

form p1                                                                                                     from p2

(1) C=B-1 i.e 2-1=1                                    AFTER THAT                              D=2*2=4

B=4-1=3

(2)B=2*1=2
0
@sunidhi 1 is not possible as when p1 executes c=1 then b will be 2 not 1 as (b=2*c;) is given!

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.

by Boss (13.7k points)
0
Is trying out all combinations the only way to solve such problems?
+5
If number of combinations are less like 6,then we can solve like this as there is 100% surety of getting correct answer.
0
Yes this method is really good
+1
Why did we consider these Operations as atomic. Whereas, I've done some questions where we break one instruction into 3 parts (read, operate, execute). For example -

Initially, X=0.

P1(){

X++;

X++;

}

P2{

X++;

X++;

}

Minimum value of X after both functions get executed is 2 (possible only if we break X++ as

2. X++

3. Write(X) )

### 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}.

by Active (3.8k points)
0
hi,

i am confused with last format i.e., p2-p1-p2

p2{b=3} p1{b=4} p2{b=7(bcz from the previous process execution b=4,d=4*2,b=d-1 ie.,8-1=7)so how it will be b having only 3 distinct numbers please answer for this!!!!!!!!!!!!!!!!!!!!!!!
0
Here P2 P1 P2 actually means P2 line1, P1 line1, P1 line2, P2 line2 ..

i.e  [P2: D],  premption, [ P1: C, B] ,preemption, [P2: B]

By this u will get 3
Since no while loops are present, only 2 values are taken by B.

P1- P2 : 3

P2 - P1: 4