GATE - 2015 | OS | The following two functions P1 and P2 that share a variable B with

GATE - 2015 | OS | The following two functions P1 and P2 that share a variable B with
Posted on 10-02-2022

GATE - 2015 [Operating System]

Question:

The following two functions P1 and P2 that share a variable B with an initial value of 2 execute concurrently.

P1()

{

   C = B – 1;

   B = 2*C; 

}

 

P2()

{

   D = 2 * B;

   B = D - 1;

}

The number of distinct values that B can possibly take after the execution is      

A

3

B

4

C

5

D

6

 

Solution:

Option (A) is Correct

If we execute the P2 process after the P1 process, then B = 3
If we execute the P1 process after the P2 process, then B = 4
If we did preemption between P1 & P2 processes, then B = 2 (Preemption have done from P1 to P2) or B = 3 (Preemption have done from P2 to P1).

So, among 2 & 3 values, only one value will be saved in B. So, the total no. of distinct values that B can possibly take after the execution is 3.

Thank You