GATE / CS ARCHIVE

GATE CSE 1994 / QUESTION 27(a)

Precedence versus control flow in conditional code

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

GATE 1994, Q27(a) Descriptive
Report issue

Synchronization & Concurrency

Question 27(a), GATE 1994: The printed straight-line code is S1: read n; S2: i:=1; S3: if i>n goto next; S4: a(i):=i+1; S5: i:=i+1; S6: next: write a(i). No backward jump is printed. Draw its precedence graph, stating the treatment of conditional execution.
Tap to enlarge
Answer & solution

Answer: Precedence 1→3, 2→3, 3→4, 4→5, 5→6, 3→6; statements 4 and 5 run only on the false branch (no official key was published)

The source calls for a precedence graph but contains a conditional jump, so an ordinary unguarded task DAG cannot by itself specify whether S4 and S5 execute. A faithful control-flow graph is S1→S2→S3, with S3 true→S6 and S3 false→S4→S5→S6. This edge list completely specifies the literal execution graph; there is no loop-back edge.

For potential parallel execution, S1 and S2 are independent initial tasks: the former writes n and the latter writes i. S3 needs both values, giving S1→S3 and S2→S3. On the false branch, keep S3→S4→S5→S6. S4 must precede S5 because it reads the old i both as an index and as the right-side operand; S5 changes i. S6 must see the incremented i on this branch. On the true branch, S6 follows S3 directly without executing S4 or S5. Thus a conservative guarded precedence description has edges 1→3,2→3,3→4,4→5,5→6,3→6, with 4 and 5 guarded by i≤n.

Do not conflate this graph with a pure data-dependence graph, which would omit some control edges and include direct uses of i. Also do not insert a guessed loop. With n≥1 the literal code assigns a(1), increments i to 2, then writes a(2), which it has not initialized. With n<1 it immediately writes a(1), also not initialized here. These are semantic concerns in the printed fragment, not permission to change its instructions. The question lacks enough conventions for a unique unqualified concurrency graph.

Original paper, page 7