Question
Consider three concurrent processes P1, P2 and P3 as shown below, which access a shared variable D that has been initialized to 100.
P1 P2 P3
: : :
: : :
D = D + 20 D = D - 50 D = D + 10
: : :
: : :
The processes are executed on a uniprocessor system running a time-shared operating system. If the minimum and maximum possible values of D after the three processes have completed execution are X and Y respectively, then the value of Y - X is _____________.
Official answer and solution
- Official answer
- 80
- From
- Official GATE 2019 answer key
Solution
Analyze the possible race conditions and lost updates on variable D to determine the minimum and maximum possible final values.
Each high-level statement D = D + C on a shared variable involves three machine-level steps:
- Read
Dinto local registerR = D - Compute
R = R + C - Store
D = R
Initially, . The three updates are:
- : add
- : subtract
- : add
Finding Maximum Value :
To maximize the final value, we want the subtraction by to be overwritten (lost update), while the additions by and take effect:
- reads , then gets preempted.
- reads .
- resumes and writes ().
- resumes and writes (), overwriting 's decrement.
- runs to completion: reads , adds , writes .
Hence, the maximum possible value is .
Finding Minimum Value :
To minimize the final value, we want the additions by and to be overwritten by 's subtraction:
- reads , computes , and is preempted before storing.
- runs to completion: reads , writes .
- runs to completion: reads , writes .
- resumes and writes its stored value into .
Hence, the minimum possible value is .
Therefore, the difference is:
80
Assuming sequential execution without interleaving, which yields Y - X = 0.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2019 Computer Science and Information Technology (CS)
- Question
- 33 of 65, printed as CS Q.23
- 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.