Question

A certain computation generates two arrays aa and bb such that a[i]=f(i)a[i]=f(i) for 0≤i<n0 \le i < n and b[i]=g(a[i])b[i] = g(a[i]) for 0≤i<n0 \le i < n. Suppose this computation is decomposed into two concurrent processes XX and YY such that XX computes the array aa and YY computes the array bb. The processes employ two binary semaphores RR and SS, both initialized to zero. The array aa 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<n; i++) {                for (i=0; i<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?

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2013 answer key

Solution

Identify the synchronization pattern needed to enforce strict alternation between process X computing array element a[i] and process Y consuming a[i] to compute b[i].

In this problem:

  • Process XX computes a[i]=f(i)a[i] = f(i) and then calls ExitX(R, S).
  • Process YY calls EntryY(R, S) and then computes b[i]=g(a[i])b[i] = g(a[i]).
  • Semaphores RR and SS are both binary semaphores initialized to 0.
  • Array element a[i]a[i] must be written by XX before it is read by YY. Furthermore, XX must not overwrite a[i]a[i] in the next iteration until YY has finished consuming it.

Let us analyze the implementations in option (C):

ExitX(R, S) {
    P(S);
    V(R);
}
EntryY(R, S) {
    V(S);
    P(R);
}

Tracing iteration i=0i = 0:

  1. Initially, R=0,S=0R = 0, S = 0.
  2. Process XX begins and computes a[0]=f(0)a[0] = f(0). Then XX calls ExitX, where it executes P(S). Since S=0S = 0, process XX blocks.
  3. Process YY calls EntryY and executes V(S). This increments SS and wakes up process XX.
  4. Process YY then executes P(R). Since R=0R = 0, process YY blocks waiting for data to be ready.
  5. Process XX continues and executes V(R), signaling that a[0]a[0] is ready. This unblocks process YY.
  6. Process XX advances to the next iteration i=1i = 1, computes a[1]a[1], and calls ExitX, blocking again on P(S).
  7. Process YY proceeds safely to read a[0]a[0] and compute b[0]=g(a[0])b[0] = g(a[0]). When YY advances to iteration i=1i = 1, it executes EntryY, signals V(S), and waits on P(R).

This handshaking pattern perfectly ensures strict alternation with no race conditions or deadlocks.

In contrast:

  • In option (A), XX executes P(R) and YY executes P(S). Since both RR and SS start at 0, both processes block immediately, resulting in deadlock.
  • In option (B), both ExitX and EntryY do not synchronize the step-by-step lockstep consumption of each element.

Therefore, option (C) is the correct implementation.

Final answer

C

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2013 Computer Science and Information Technology (CS)
Question
39 of 65, printed as CS Q.39
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.