Multiple choice

A certain computation generates two arrays a and b such that a [i] = f (i) for 0 $\le$ i < n and b [i] = g (a [i]) for 0 $\le$ i < n. Suppose this computation is decomposed into two concurrent processes X and Y such that X computes the array a and Y computes the array b. The processes employ two binary semaphores R and S, both initialised to zero. The array a is shared by the two processes. The structures of the processes are shown below.

Process X; 
Process Y;
private i;
private i;
for (i = 0; i &lt; n; i + +) {                            
  for (i = 0; i &lt; n; i + +) {
         a [i] = f (i);
         EntryY (R, S);
         ExitX (R, S); 
         b [i] = g (a [i]);
    }
}

Which one of the following represents the CORRECT implementations of ExitX and EntryY?

  1. ExitX (R, S) {
       P (R) ;
       V (S) ;
    }
    EntryY (R, S) {
       P (S);
       V (R);
    }
    
  2. ExitX(R, S) { 
      V (R); 
      V (S);
    }
    EntryY (R, S) {
      P (R);
      P (S);
    }
    
  3. ExitX (R, S) {
      P (S);
      V (R);
    }
    EntryY (R, S) {
      V (S);
       P (R);
    }
    
  4. ExitX (R, S) {
      V (R);
      P (S);
    }
    EntryY (R, S) {
      V (S);
      P (R);
    }
    
Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Process X produces a[i] and signals completion. Process Y consumes a[i] and must wait for X to produce it. ExitX signals completion by V(R) and V(S) (signal both semaphores). EntryY waits for X's signal by P(R) then P(S) (wait on both semaphores). This ensures Y only accesses a[i] after X has written it. Option B implements this correctly - V(R), V(S) in ExitX and P(R), P(S) in EntryY ensures proper synchronization.