GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Trace a counter-triggered semaphore gate

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

How these are solved

Both have a mutex-protected arrival counter, an initially closed second semaphore signaled at a specified count, and a wait/signal handoff that lets later work proceed once the threshold is reached.

2 marks · MSQ
Report issue

Semaphore constraints on print order

Synchronization & Concurrency

Question 51, GATE 2026: Three processes each execute: wait(A); print(*); X=X+1; if X==2 then {print($); signal(B);}; signal(A); wait(B); print(#); signal(B). Binary semaphores start A=1,B=0 and shared X=0. Each line is atomic. Which outputs are possible? A **$*###; B **$#*##; C **$##*#; D ***$###.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B, C

Givens and goal

Three processes each execute: wait(A); print(*); X=X+1; if X==2 then {print($); signal(B);}; signal(A); wait(B); print(#); signal(B). Binary semaphores start A=1,B=0 and shared X=0. Each line is atomic. Which outputs are possible? A $*###; B **$#*##; C **$##*#; D *$###.

Approach

Find invariant ordering constraints, then exhibit schedules achieving each candidate.

Step-by-step solution

1. A protects the star and increment section. The first entrant prints a star and changes X from 0 to 1. The second prints another star and changes X to 2, then prints $ before releasing A.
2. The third entrant cannot print its star until that release. Therefore the dollar must occur immediately after the second star among these star/dollar events, and ***$### is impossible. Preempting the second process before $ does not help: it still holds A.
3. B initially blocks all hash printing. It is first signaled only after the dollar; each process that prints a hash then signals B for another. Thus no hash can precede $, while each process’s own star must precede its hash.
4. For A, let the first two processes complete their A sections, then let the third complete its A section before allowing any hash sections. For B, after the first two A sections allow one of those processes to print a hash, then allow the third star, then the remaining hashes.
5. For C, let both first and second processes print their hashes after $, delaying the third A section until afterward. The third then prints its star followed by its hash. These realize all A,B,C exactly.
6. Therefore select A,B,C. The invariant and the explicit schedules together establish both impossibility and feasibility; a single simulated scheduling order would not suffice.

Original paper, page 37 · Answer key

2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 34, GATE 2020: Each of n processes runs this once, with shared count=0, semaphores a=1,b=0, and P not using count:
P;
wait(a); count=count+1;
if(count==n) signal(b);
signal(a); wait(b); signal(b);
Q;
What is guaranteed? A no Q before all P finish; B at most two in Q; C P mutually exclusive; D at most n−1 in P.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Worked explanation

Before any process can signal b, some process must observe count=n while holding a. Since increments are protected and occur after P, count is exactly the number of completed P sections that have passed the increment. Thus the first b signal proves every P has completed. The subsequent wait(b);signal(b) passes a token along the waiting processes; it gates entry but does not keep b held during Q. A process may enter Q and another may immediately enter too, so no mutual-exclusion or two-process bound follows. P is outside both waits, so every process can be in P simultaneously. This is a one-use barrier, not a reusable barrier because count is never reset.

Derivation and answer

Semaphore a serializes count increments after each process finishes P. Semaphore b starts 0 and is signaled for the first time only when count=n, so no process can pass wait(b) before all have finished P. Each passing process signals b to release the next waiter. The signal occurs before Q, so Q is not mutually excluded or limited to two entrants. Thus this is a barrier, A.

Original paper, page 11 · Answer key