Skip to content
Official PYQ · 2023

GATE CS 2023 Q28 — Concurrency and Synchronization

Explain with AI

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

incr(){             decr(){
    wait(s);            wait(s);
    X = X+1;            X = X-1;
    signal(s);          signal(s);
}                   }    

There are 55 threads each invoking incr\textsf{incr} once, and 33 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?

Choose one option
View worked solution

Answer: C

To find the minimum possible values of XX at the end of execution for both implementations:

Case I-1: Binary Semaphore ss initialized to 11

  • A binary semaphore initialized to 11 ensures strict mutual exclusion. Only one thread can enter the critical section at any given time.
  • Therefore, all 55 increments and 33 decrements will execute sequentially in some order.
  • The final value of XX will always be: V1=10+5−3=12V1 = 10 + 5 - 3 = 12

Case I-2: Counting Semaphore ss initialized to 22

  • Since s=2s = 2, up to two threads can enter the critical section concurrently, allowing race conditions.
  • To minimize the final value V2V2, we can construct a schedule where the effects of the increment threads are overwritten:
    1. A decrement thread D1D_1 enters the critical section, reads X=10X = 10 into its local register, and is preempted before writing.
    2. Since ss was 22 initially and is now 11, another thread can enter. All 55 increment threads (I1I_1 to I5I_5) execute sequentially one after another. XX becomes 1515.
    3. D1D_1 resumes, decrements its local value of 1010 to 99, and writes X=9X = 9 back to memory (overwriting the value 1515).
    4. The remaining two decrement threads D2D_2 and D3D_3 execute sequentially, reducing XX from 9→8→79 \to 8 \to 7.
  • Thus, the minimum possible value is V2=7V2 = 7.

Therefore, the minimum values of V1V1 and V2V2 are 1212 and 77, respectively.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions