Question
The following two functions P1 and P2 that share a variable B with an initial value of 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 _____.
Official answer and solution
- Official answer
- 3
- From
- Official GATE 2015 answer key
Solution
Trace all valid instruction interleavings of concurrent processes P1 and P2 sharing variable B initialized to 2.
Let initial .
Process P1 statements:
- :
- :
Process P2 statements:
- :
- :
Tracing possible execution sequences:
-
then ():
: .
: .
: .
: .
Final . -
then ():
: .
: .
: .
: .
Final . -
:
: .
: .
: .
: .
Final . -
:
: .
: .
: .
: .
Final . -
:
: : .
: .
: .
Final . -
:
: : .
: .
: .
Final .
The distinct final values that can take are .
The count of distinct values is 3.
3
Testing only the two strictly sequential executions (yielding 3 and 4) and overlooking the interleaved case where P1 overwrites B last to leave it at 2.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 1
- Question
- 19 of 65, printed as CS Q.9
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.