GATE / CS ARCHIVE

GATE CSE 2008 / QUESTION 63

Initializing binary semaphores

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2008 paper

GATE 2008, Q63 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 63, GATE 2008: Counting-semaphore operations are P(s): decrement s and wait if s<0; V(s): increment s and wake one waiter if s≤0. Given binary-semaphore wait/signal Pb,Vb, consider:
P(s): Pb(xb); s=s−1; if(s<0){Vb(xb); Pb(yb);} else Vb(xb);
V(s): Pb(xb); s=s+1; if(s≤0) Vb(yb); Vb(xb);
What initial values should xb,yb have respectively?

(A) 0,0
(B) 0,1
(C) 1,0
(D) 1,1
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The semaphore xb protects the shared count and must start unlocked at 1. The semaphore yb represents the waiting gate and must start at 0, so a P operation with negative count blocks.
2. Thus the required initial values are (1,0), option C. This answers the initialization question, not a proof of the whole implementation.
3. Under ordinary saturating binary-semaphore semantics the shown implementation can lose a wakeup: two P callers decrement to -2 and pause before Pb(yb); two V callers then both signal yb before either waiter consumes it, leaving only one stored signal. A robust implementation needs additional synchronization or a specified wakeup protocol.

Original paper, page 13