CS FundamentalsModerateGATE CS (PYQ pattern)

A counting semaphore S starts at 3 and is implemented so that wait() decrements S and blocks the caller if S becomes negative, while signal() increments S and wakes one blocked thread if S <= 0 afterwards. Threads T1 to T6 each execute wait(S) once and enter the work section when wait returns. Later T1 and T2 leave with signal(S). What are S, the number of blocked threads and the number inside the work section?

  1. A. S = -1, 1 blocked, 5 inside
  2. B. S = 0, 0 blocked, 4 inside
  3. C. S = -1, 1 blocked, 3 inside
  4. D. S = 1, 0 blocked, 2 inside

Correct answer: C. S = -1, 1 blocked, 3 inside

Six waits take S from 3 to -3: three threads enter, three block. Each signal raises S (-2, then -1) and wakes one blocked thread. Now S = -1, so 1 thread still waits; inside = 6 started - 2 left - 1 blocked = 3, which is the semaphore's limit. 5 inside forgets that T1 and T2 left.

More Operating Systems questions

Exams that ask this topic