Question
A certain computation generates two arrays and such that for and for . Suppose this computation is decomposed into two concurrent processes and such that computes the array and computes the array . The processes employ two binary semaphores and , both initialized to zero. The array 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?
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 computes and then calls
ExitX(R, S). - Process calls
EntryY(R, S)and then computes . - Semaphores and are both binary semaphores initialized to 0.
- Array element must be written by before it is read by . Furthermore, must not overwrite in the next iteration until 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 :
- Initially, .
- Process begins and computes . Then calls
ExitX, where it executesP(S). Since , process blocks. - Process calls
EntryYand executesV(S). This increments and wakes up process . - Process then executes
P(R). Since , process blocks waiting for data to be ready. - Process continues and executes
V(R), signaling that is ready. This unblocks process . - Process advances to the next iteration , computes , and calls
ExitX, blocking again onP(S). - Process proceeds safely to read and compute . When advances to iteration , it executes
EntryY, signalsV(S), and waits onP(R).
This handshaking pattern perfectly ensures strict alternation with no race conditions or deadlocks.
In contrast:
- In option (A), executes
P(R)and executesP(S). Since both and start at 0, both processes block immediately, resulting in deadlock. - In option (B), both
ExitXandEntryYdo not synchronize the step-by-step lockstep consumption of each element.
Therefore, option (C) is the correct implementation.
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.