Skip to content
Official PYQ · 2013

GATE CS 2013 Q34 — Concurrency and Synchronization

Explain with AI

A shared variable xx, initialized to zero, is operated on by four concurrent processes W,X,Y,ZW, X, Y, Z as follows. Each of the processes WW and XX reads xx from memory, increments by one, stores it to memory, and then terminates. Each of the processes YY and ZZ reads xx from memory, decrements by two, stores it to memory, and then terminates. Each process before reading xx invokes the PP operation (i.e., wait) on a counting semaphore SS and invokes the VV operation (i.e., signal) on the semaphore SS after storing xx to memory. Semaphore SS is initialized to two. What is the maximum possible value of xx after all processes complete execution?

Choose one option
View worked solution

Answer: D

Since the counting semaphore SS is initialized to 22, at most two processes can enter their critical sections concurrently. This concurrency allows for "lost updates" when two processes read the same value of xx before either writes it back.

To maximize the final value of xx, we want the decrement operations of YY and ZZ to be overwritten (lost) by the increment operations of WW and XX. We can achieve this with the following sequence starting with x=0x = 0 and S=2S = 2:

  1. Phase 1 (Overwriting YY's decrement):
    • YY and WW enter the critical section (SS becomes 00).
    • YY reads x=0x = 0.
    • WW reads x=0x = 0.
    • YY decrements xx to −2-2, writes x=−2x = -2, and exits (SS becomes 11).
    • WW increments its local copy (0+1=10 + 1 = 1), writes x=1x = 1, and exits (SS becomes 22).
  2. Phase 2 (Overwriting ZZ's decrement):
    • ZZ and XX enter the critical section (SS becomes 00).
    • ZZ reads x=1x = 1.
    • XX reads x=1x = 1.
    • ZZ decrements xx to −1-1, writes x=−1x = -1, and exits (SS becomes 11).
    • XX increments its local copy (1+1=21 + 1 = 2), writes x=2x = 2, and exits (SS becomes 22).

After all processes terminate, the maximum possible value of xx is 22.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions