FOR FREE MATERIALS

Q. GATE-CS-1995 | Question 19

 

Consider the following program segment for concurrent processing using semaphore operators P and V for synchronization. Draw the precedence graph for the statements S1 to S9. 

 

Code

begin

Cobegin

            begin : S1; V(a); V(b) end;

            begin : P(a); S2; V(c); V(d) end;

            begin : P(c); S4; V(e) end;

            begin : P(d); S5; V(f) end;

            begin : P(e); P(f); S7; V(k) end;

            begin : P(b); S3; V(g); V(h) end;

            begin : P(g); S6; V(i) end;

            begin : P(h); P(i); S8; V(j) end;

            begin : P(j); P(k); S9; end;

Coend

end

 

Answer:

 

We can execute by any sequence depending upon the semaphore variables.

 

Here S1 executes first because S1 does not depend on any statements, it only depends on the semaphore variable.

So, the Precedence Graph as the execution order

 

 

Q. GATE-CS-1993 | Question 22

 

Write a concurrent program using perbegin-parend and semaphores to represent the precedence constraints of the statements S1 to S6, as shown in figure below.

 

 

Answer:

 

Let us consider the semaphore a, b, c, d, e, f, g.

 

begin

perbegin

            begin : S1; V(a); V(b) end;

            begin : P(a); S2; V(c); V(e) end;

            begin : P(b); S3; V(d) end;

            begin : P(f); P(c); S4 end;

            begin : P(g); P(e); P(d); S5 end;

            begin : S6; V(f) V(g) end;

parend

end