GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 81

Preventing odd-length internal output runs

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

GATE 2003, Q81 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 81, GATE 2003: Two concurrent processes use binary semaphores S,T. Process P loops forever: W: [synchronization]; print '0'; print '0'; X: [synchronization]. Process Q loops forever: Y: [synchronization]; print '1'; print '1'; Z: [synchronization]. Synchronization can be placed only at W,X,Y,Z. P(S) means wait/decrement and V(S) means signal. Which ensures the output never contains 0 1^n 0 or 1 0^n 1 with n odd?
A. W=P(S), X=V(S), Y=P(T), Z=V(T); S=T=1
B. W=P(S), X=V(T), Y=P(T), Z=V(S); S=T=1
C. W=P(S), X=V(S), Y=P(S), Z=V(S); S=1
D. W=V(S), X=V(T), Y=P(S), Z=P(T); S=T=1
Question 81, GATE 2003: Two concurrent processes use binary semaphores S,T. Process P loops forever: W: [synchronization]; print '0'; print '0'; X: [synchronization]. Process Q loops forever: Y: [synchronization]; print '1'; print '1'; Z: [synchronization]. Synchronization can be placed only at W,X,Y,Z. P(S) means wait/decrement and V(S) means signal. Which ensures the output never contains 0 1^n 0 or 1 0^n 1 with n odd?
A. W=P(S), X=V(S), Y=P(T), Z=V(T); S=T=1
B. W=P(S), X=V(T), Y=P(T), Z=V(S); S=T=1
C. W=P(S), X=V(S), Y=P(S), Z=V(S); S=1
D. W=V(S), X=V(T), Y=P(S), Z=P(T); S=T=1
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The forbidden patterns mean a run of ones between zeros, or zeros between ones, must have even length. It is sufficient for each process to print its identical pair atomically.
2. C uses the same initially available semaphore at both W and Y. Only one process can enter its two-print critical section, and it releases the mutex only after the pair.
3. Output is thus a concatenation of whole 00 and 11 blocks. Adjacent blocks of the same digit may merge, but their lengths remain multiples of two, proving the desired property.
4. A and B initially allow both processes inside, permitting P's first 0, Q's first 1, P's second 0: a forbidden 010. D does not make P acquire a common mutex and likewise does not ensure whole-pair exclusion. Therefore C.

Solution: Use one common binary semaphore S, initially one, as a mutex around both print statements of either process: wait(S) at W and Y, signal(S) at X and Z. Each critical section then emits either 00 or 11 without interleaving, so any run between opposite digits is a concatenation of equal pairs and has even length. That is C. For example, when independent locks or two initially available cross-signalling permissions let P emit its first zero, Q one one, and P its second zero, the forbidden substring 010 occurs. Mutual exclusion of entire pairs is the needed property, not merely exclusion within each individual process.

Original paper, page 12