Consider three concurrent processes P1, P2 and P3 as shown below, which access a shared variable D that has been initialized to 100. P1: D=D+20. P2: D=D-50. P3: D=D+10. The processes are executed on a uniprocessor system running a time-shared operating system, with each read-modify-write being non-atomic. 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
Explanation
MAXIMUM (Y): lose P2's subtraction entirely by having both additions apply on top of each other with P2's write squeezed in between and then overwritten: P1 reads 100; P2 reads 100, writes 50 (based on its own read); P1 (using its earlier stale read of 100) writes 120, discarding P2's update; P3 reads 120, writes 130. Y=130. MINIMUM (X): lose BOTH additions by having P2 read the ORIGINAL 100 early, then P1 and P3 both apply in sequence (100→120→130), and finally P2 writes its stale result 100-50=50 LAST, overwriting everything. X=50 (this is also the theoretical floor, since P2's own read value can never be below the initial 100 given only additions could have preceded it). Y-X = 130-50 = 80.
Source: GATE CS 2019 (Set-2) (2019)
This question is part of a 15-day revision cycle — sign in and Project Forge will bring it back the day before you'd forget it.