GATE / CS ARCHIVE

GATE CSE 2010 / QUESTION 45

Binary-semaphore print count

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

GATE 2010, Q45 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 45, GATE 2010: Three concurrent processes use binary semaphores initially S0=1,S1=0,S2=0:
P0: while(true) { wait(S0); print '0'; release(S1); release(S2); }
P1: wait(S1); release(S0);
P2: wait(S2); release(S0);
How many times will P0 print '0'?
Options: (A) At least twice; (B) Exactly twice; (C) Exactly thrice; (D) Exactly once.
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

P0 consumes the initial S0 permit and prints once, then enables P1 and P2. Each of those processes releases S0 exactly once. If both releases occur before P0 next waits on S0, binary saturation coalesces them into one permit, allowing only one further print: total 2. If P0 consumes the first released permit before the other process releases S0, it receives two separate permits and prints three times total. Thus 2 or 3 prints are possible, so the only correct choice is A, at least twice. This assumes ordinary progress/eventual scheduling; an adversarial scheduler that permanently starves runnable processes defeats any eventual-print guarantee. A counting semaphore would instead give exactly three.

The distinction between binary and counting semaphores changes the answer. A binary semaphore stores at most one available permit; release on an already available semaphore does not accumulate a second permit. P1 and P2 have no loops, so they cannot supply unbounded future permits even though P0 keeps releasing S1 and S2. That bounds the total prints above by three.

Original paper, page 11