GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 19

Semaphore precedence graph

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

GATE 1995, Q19 5 marks · Descriptive
Report issue

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