A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution? Correct Answer 2
The correct answer is option 4.
Key Points
[ alt="F1 Raju Madhuri 29.05.2021 D9" src="//storage.googleapis.com/tb-img/production/21/05/F1_Raju_Madhuri_29.05.2021_D9.png" style="width: 207px; height: 143px;">
[ alt="F1 Raju Madhuri 29.05.2021 D10" src="//storage.googleapis.com/tb-img/production/21/05/F1_Raju_Madhuri_29.05.2021_D10.png" style="width: 206px; height: 140px;">
X, W reads X and increment X by 1.
Y, W reads X and decrement by 2.
Start with X, will perform P(S) then S=1, read X=0, X=X+1=1
Then Y will perform P(S) then S=0, read X=0, X=X-2=-2, then store X. V(S), S=1
Then Z will perform P(S) then S=0, read X=-2, X=X-2= -4, then store X, V(S), S=1
Then X will store X, V(S), S=2, X=1
Then W will perform P(S), S=1, read X=1, X=X+1=2, store X, V(S), S=2, X=2
Hence the correct answer is 2.