Skip to main content

GATE CSE 2023Operating System › Process Synchronization & ConcurrencyVerified

GATE CSE 2023 PYQ: Consider the two functions incr \textsf{incr} incr and decr \textsf{decr} decr shown below. incr (){ decr…

Consider the two functions incr\textsf{incr} and decr\textsf{decr} shown below. 

  1. incr(){ decr(){
  2. wait(s); wait(s);
  3. X = X+1; X = X-1;
  4. signal(s); signal(s);
  5. } }

There are 5 threads each invoking incr\textsf{incr} once, and 3 threads each invoking decr\textsf{decr} once, on the same shared variable X\mathrm{X}. The initial value of X\mathrm{X} is 10.10.

Suppose there are two implementations of the semaphore s,\text{s}, as follows:

I-1:  s\textbf{I-1:} \;\mathrm{s} is a binary semaphore initialized to 1.1.
I-2:  s\textbf{I-2:}\; \mathrm{s} is a counting semaphore initialized to 2.2.

Let V1, V2\text{V1, V2} be the values of X\text{X} at the end of execution of all the threads with implementations I-1, I-2,\textbf{I-1, I-2}, respectively.

Which one of the following choices corresponds to the minimum possible values of V1, V2,\text{V1, V2}, respectively?

  1. A
    15,7
  2. B
    7,7
  3. C
    12,7
    Correct answer
  4. D
    12,8

Answer

(C) 12 , 7 12,7 12 , 7

Approach

Analyze the two semaphore implementations separately. I-1 is a binary semaphore with initial value 1, so the critical sections of incr and decr are mutually exclusive; all operations serialize and the final value is deterministic. I-2 is a counting semaphore with initial value 2, so up to two threads may be inside their critical sections at once; this allows read-modify-write interleavings and lost updates, so we construct the worst-case schedule to minimize X.

The binary semaphore I-1 serializes the operations, so V1 = 12. The counting semaphore I-2 allows interleaved read-modify-write operations; by delaying decrement writes, all increments can be overwritten and V2 can be reduced to 7. Therefore the minimum possible values are 12 and 7, matching option C.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.