GATE / CS ARCHIVE

SIMILAR QUESTIONS / OPERATING SYSTEMS

Translate precedence edges into semaphore synchronization

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

How these are solved

Both problems translate statement precedence into, or out of, semaphore handoffs in parallel branches: match each predecessor signal to the successor wait, preserving fan-out and joins.

5 marks · Descriptive
Report issue

Semaphore precedence graph

Synchronization & Concurrency

Question 19, GATE 1995: Draw the precedence graph of nine concurrent branches (separated here by |): S1;V(a);V(b) | P(a);S2;V(c);V(d) | P(c);S4;V(e) | P(d);S5;V(f) | P(e);P(f);S7;V(k) | P(b);S3;V(g);V(h) | P(g);S6;V(i) | P(h);P(i);S8;V(j) | P(j);P(k);S9. All variables a through k are semaphores. This is the GFG version. GateForum instead has V(c) after S4 and P(j);P(j);P(k) before S9. Initial semaphore values are not explicitly supplied; state the synchronization assumption.
Tap to enlarge
Answer & solution

Answer: With semaphores initially zero: 1→2, 1→3, 2→4, 2→5, 4→7, 5→7, 3→6, 3→8, 6→8, 7→9, 8→9 (no official key was published)

Assume the usual precedence convention: every semaphore initially equals zero, P waits for and consumes one signal, and V supplies one signal. Initialization is essential: positive initial tokens would allow some statements to run before their supposed predecessors. With initially-zero semaphores, map each signal after a statement to the wait before another statement.

The labeled dependency edges are S1−a→S2, S1−b→S3; S2−c→S4, S2−d→S5; S4−e→S7, S5−f→S7; S3−g→S6, S3−h→S8; S6−i→S8; S8−j→S9 and S7−k→S9. This edge list is a complete exact specification of the requested graph. To draw it, put S1 at the top, S2 and S3 below it, S4/S5 below S2, S6 below S3, S7 below S4/S5, S8 below S6, and S9 below S7/S8; include the additional direct S3→S8 edge. That direct edge is transitively redundant because S3→S6→S8 already holds, but faithfully represents semaphore h.

S7 is a join: it cannot execute until both S4 and S5 have completed. Similarly S8 waits for both signals from S3 and S6, and S9 joins the two sides. Statements on different sides need not execute in a fixed order.

Its V(c) after S4 supplies no e token, so S7 is permanently blocked at P(e). Its final branch also waits twice on j, although S8 signals j only once, so S9 blocks at its second P(j), even independently of the missing k signal. Under fair execution, S1,S2,S3,S4,S5,S6,S8 may finish; S7 and S9 do not.

Original paper, page 8

5 marks · Descriptive
Report issue

Semaphore precedence implementation

Synchronization & Concurrency

Question 22, GATE 1993: Implement DAG edges S1→S2,S1→S3,S2→S4,S2→S5,S3→S5,S6→S4,S6→S5 using parbegin/parend and semaphores. Note S6 is a source, not a sink.
Tap to enlarge
Answer & solution

Answer: One zero-initialized semaphore per edge; each task waits on incoming edges and signals outgoing edges. (no official key was published)

Use semaphores e12,e13,e24,e25,e35,e64,e65, each initialized to zero. P(e) waits and decrements atomically; V(e) signals. Run these six branches in one parbegin/parend block:

begin S1; V(e12); V(e13) end
begin P(e12); S2; V(e24); V(e25) end
begin P(e13); S3; V(e35) end
begin P(e24); P(e64); S4 end
begin P(e25); P(e35); P(e65); S5 end
begin S6; V(e64); V(e65) end

For each edge u→v, its semaphore can be signalled only after Su finishes, and Sv cannot start until its wait succeeds. Hence every required precedence is enforced. Distinct edge semaphores prevent one successor from consuming another's notification. They also allow a signal to arrive before its matching wait without being lost.

Only S1 and S6 have no predecessors, so they can begin concurrently. S4 awaits S2 and S6, while S5 awaits S2,S3,S6. In particular, the arrows drawn upward from S6 end at S4 and S5; reversing them would implement a different graph. No extra ordering is imposed between incomparable task executions, although a blocked wait can hold a branch until all its own predecessors finish. With terminating tasks and fair scheduling this acyclic dependency system has no synchronization deadlock.

Original paper, page 13