Question

Consider a non-negative counting semaphore SS. The operation P(S)P(S) decrements SS, and V(S)V(S) increments SS. During an execution, 20 P(S)P(S) operations and 12 V(S)V(S) operations are issued in some order. The largest initial value of SS for which at least one P(S)P(S) operation will remain blocked is _____.

Official answer and solution
Official answer
7
From
Official GATE 2016 answer key

Solution

Find the maximum initial semaphore value that permits at least one P(S)P(S) operation to remain blocked in the worst-case order.

Let S0S_0 be the initial value of the counting semaphore SS (S0≥0S_0 \ge 0).

During execution, 20 P(S)P(S) and 12 V(S)V(S) operations are issued in some order. Each successful P(S)P(S) operation decrements SS by 1. Each V(S)V(S) operation either increments SS by 1 or unblocks one waiting process.

The total number of available resources across the entire execution is the initial value plus all increments from V(S)V(S):

Total available=S0+12\text{Total available} = S_0 + 12

To maximize the number of blocked operations, consider an order where all 20 P(S)P(S) operations are issued before any V(S)V(S) operation:

  1. The first S0S_0 operations of P(S)P(S) succeed without blocking, leaving S=0S = 0.
  2. The remaining 20−S020 - S_0 operations of P(S)P(S) find S=0S = 0 and become blocked.
  3. Then the 12 V(S)V(S) operations are issued, unblocking at most 12 waiting P(S)P(S) operations.
  4. The number of P(S)P(S) operations that remain blocked is:
Remaining blocked=(20−S0)−12=8−S0\text{Remaining blocked} = (20 - S_0) - 12 = 8 - S_0

For at least one P(S)P(S) operation to remain blocked:

8−S0≥1  ⟹  S0≤78 - S_0 \ge 1 \implies S_0 \le 7

If S0=7S_0 = 7, then 20−7=1320 - 7 = 13 operations block initially, and 12 V(S)V(S) operations unblock 12 of them, leaving exactly 1 operation blocked.
If S0≥8S_0 \ge 8, at most 12 operations block initially, and the 12 V(S)V(S) operations will unblock all of them, so no operation remains blocked.
Thus, the largest initial value of SS is 7.

Final answer

7

Common trap

Setting 20−S0−12=020 - S_0 - 12 = 0 to get 8, which is the smallest value ensuring NO operations remain blocked, rather than the largest value where AT LEAST ONE remains blocked.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
59 of 65, printed as CS Q.49
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.