GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Pass semaphore permits to enforce a repeating print order

2 questions solved the same way, newest first. Back to Operating Systems

How these are solved

Both require a predetermined repeating output despite arbitrary interleaving of infinite printing processes. Follow the wait/print/signal edges as a cyclic permit handoff and enable only the required first turn initially.

1 mark · MCQ
Report issue

Synchronization & Concurrency

Question 19, GATE 2022: Three infinite threads run on one CPU with binary semaphores. T1 repeats wait(S3); print C; signal(S2). T2 repeats wait(S1); print B; signal(S3). T3 repeats wait(S2); print A; signal(S1). Which initialization forces BCABC… despite arbitrary context switches? A (S1,S2,S3)=(1,1,1); B (1,1,0); C (1,0,0); D (0,1,1).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

T2 waits on S1 and prints B, then signals S3. T1 waits on S3 and prints C, then signals S2. T3 waits on S2 and prints A, then signals S1. Initialize only S1 to 1, with S2=S3=0, to force B→C→A repeatedly. That is C. This assumes ordinary successful semaphore operations and eventual scheduling of enabled threads.

Track the unique token: (1,0,0) enables only T2. Its wait consumes S1 and its signal creates the S3 token; then T1 creates S2; then T3 restores S1. Context switching can delay progress, but cannot permit a different letter while its semaphore is zero.

Original paper, page 15 · Answer key

2 marks · MCQ
Report issue

Semaphores enforcing alternating output pairs

Synchronization & Concurrency

Question 80, 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 always gives an output starting 001100110011...?
A. W=P(S), X=V(S), Y=P(T), Z=V(T); S=T=1 initially
B. W=P(S), X=V(T), Y=P(T), Z=V(S); S=1,T=0 initially
C. W=P(S), X=V(T), Y=P(T), Z=V(S); S=T=1 initially
D. W=P(S), X=V(S), Y=P(T), Z=V(T); S=1,T=0 initially
Question 80, 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 always gives an output starting 001100110011...?
A. W=P(S), X=V(S), Y=P(T), Z=V(T); S=T=1 initially
B. W=P(S), X=V(T), Y=P(T), Z=V(S); S=1,T=0 initially
C. W=P(S), X=V(T), Y=P(T), Z=V(S); S=T=1 initially
D. W=P(S), X=V(S), Y=P(T), Z=V(T); S=1,T=0 initially
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. The required output demands both pairwise exclusion and alternation starting with P's zeros. Initially only P should have permission: S=1 and T=0.
2. P passes wait(S), prints both zeros, then signals T. Q cannot print any one until this signal; once released it consumes T, prints both ones and signals S.
3. P's next wait(S) cannot pass before Q completes its pair. Inductively the execution consists of 00,11,00,11,..., so B works.
4. In A independent locks allow arbitrary interleaving. C initially lets both processes enter, so even the first pair need not be 00. In D Q waits forever on T because no other process signals T.

Solution: Initially give P permission to run by setting S=1 and block Q with T=0. P waits on S at W, prints both zeroes, and signals T at X. Q waits on T at Y, prints both ones, and signals S at Z. Each process therefore hands permission to the other only after completing its pair, forcing 00,11,00,11,… starting with 00. This is B. Separate locks do not coordinate the processes, and initializing both cross-signalling semaphores to one would permit initial interleaving.

Original paper, page 12