CS FundamentalsModerateHCL · Tech Mahindra pattern

Two processes must run statements S1 (in P1) and S2 (in P2) such that S2 never executes before S1. Using one semaphore `sync`, which is correct?

  1. A. sync = 1; P1: wait(sync); S1; signal(sync); P2: S2;
  2. B. sync = 1; P1: S1; signal(sync); P2: wait(sync); S2;
  3. C. sync = 0; P1: wait(sync); S1; P2: S2; signal(sync);
  4. D. sync = 0; P1: S1; signal(sync); P2: wait(sync); S2;

Correct answer: D. sync = 0; P1: S1; signal(sync); P2: wait(sync); S2;

With sync = 0, P2's wait blocks until P1 has done S1 and signalled. Initialising sync to 1 lets P2 pass wait immediately, so S2 could run first.

More Operating Systems questions

Exams that ask this topic