GATE 2013
80 questions
MCQ 801-mark 38 · 2-mark 42
Questions 41–45 of 80
The following code segment is executed on a processor which allows only register operands in its instructions. Each instruction can have atmost two source operands and one destination operand. Assume that all variables are dead after this code segment.
c = a + b;
d = c * a;
e = c + a;
x = c * c;
if (x > a) {
y = a * a;
}
else {
d = d * d;
e = e * e;
} Suppose the instruction set architecture of the processor has only two registers. The only allowed compiler optimization is code motion, which moves statements from one place to another while preserving correctness. What is the minimum number of spills to memory in the compiled code?Answer: (B)
After c = a + b, the value b is not needed again. The values a and c are still needed. To test x > a, the processor needs x and a in the two registers. But c is also still needed if the else branch runs.
Statements d = c * a and e = c + a are used only in the else branch. Code motion lets us move them into that branch. Then the code is:
c = a + b; x = c * c; if (x > a) { y = a * a; } else { d = c * a; d = d * d; e = c + a; e = e * e; }
At the test, three values are live (a, x and c) and there are only 2 registers. So c is stored to memory once (one spill). Then:
if branch: c is dead, and y = a * a needs only a and y. No more spills.
else branch: c is loaded back, d = c * a and d * d are computed. Then c is loaded again for e = c + a and e * e. Reloading the stored c is not a new spill.
Zero spills is not possible. At the test, a and x must be in registers, but the else branch still needs a value derived from c (c itself, or d and e if they were computed earlier). That value has to be kept in memory, so there is at least one spill.
Answer: (B)
Statements d = c * a and e = c + a are used only in the else branch. Code motion lets us move them into that branch. Then the code is:
c = a + b; x = c * c; if (x > a) { y = a * a; } else { d = c * a; d = d * d; e = c + a; e = e * e; }
At the test, three values are live (a, x and c) and there are only 2 registers. So c is stored to memory once (one spill). Then:
if branch: c is dead, and y = a * a needs only a and y. No more spills.
else branch: c is loaded back, d = c * a and d * d are computed. Then c is loaded again for e = c + a and e * e. Reloading the stored c is not a new spill.
Zero spills is not possible. At the test, a and x must be in registers, but the else branch still needs a value derived from c (c itself, or d and e if they were computed earlier). That value has to be kept in memory, so there is at least one spill.
Answer: (B)
A certain computation generates two arrays a and b such that a[i]=f(i)for and b[i] = g (a[i] )for . 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 initialized to zero. The array a is shared by the two processes. The structures of the processes are shown below.
Which one of the following represents the CORRECT implementations of ExitX and EntryY?
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?
Answer: (C)
Y must read a[i] only after X has written it, and R and S are binary semaphores that start at 0.
Option (C): ExitX is P(S); V(R), and EntryY is V(S); P(R). Y signals S to say it is ready, and X waits on S. After a[i] is written, X signals R, and Y waits on R. So Y reads a[i] only after X wrote it. X can never signal R twice before Y consumes it, because X's next V(R) needs Y's next V(S), and Y does that only after it passed P(R). So no signal is lost and there is no deadlock.
Why the others fail:
- (A): X does P(R) first and Y does P(S) first. Both semaphores are 0, so both block forever (deadlock).
- (B): ExitX is V(R); V(S) and EntryY is P(R); P(S). X can run ahead and do V(R) while R is already 1. A binary semaphore stays at 1, so that signal is lost. Y then waits forever for it.
- (D): ExitX is V(R); P(S). X can again do V(R) on R = 1, for example when Y is preempted just after V(S). One signal is lost and Y blocks forever on its last P(R).
Answer: (C)
Option (C): ExitX is P(S); V(R), and EntryY is V(S); P(R). Y signals S to say it is ready, and X waits on S. After a[i] is written, X signals R, and Y waits on R. So Y reads a[i] only after X wrote it. X can never signal R twice before Y consumes it, because X's next V(R) needs Y's next V(S), and Y does that only after it passed P(R). So no signal is lost and there is no deadlock.
Why the others fail:
- (A): X does P(R) first and Y does P(S) first. Both semaphores are 0, so both block forever (deadlock).
- (B): ExitX is V(R); V(S) and EntryY is P(R); P(S). X can run ahead and do V(R) while R is already 1. A binary semaphore stays at 1, so that signal is lost. Y then waits forever for it.
- (D): ExitX is V(R); P(S). X can again do V(R) on R = 1, for example when Y is preempted just after V(S). One signal is lost and Y blocks forever on its last P(R).
Answer: (C)
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
Answer: (D)
Since S = 2, two processes can be inside the critical section together. To get the largest x, the decrements must be lost:
- W reads x = 0 and waits there.
- Y runs to the end, so x = 2. Then Z runs to the end, so x = 4.
- W now stores 0 + 1 = 1, which overwrites 4.
- X then runs, and x = 2.
Answer: 2
- W reads x = 0 and waits there.
- Y runs to the end, so x = 2. Then Z runs to the end, so x = 4.
- W now stores 0 + 1 = 1, which overwrites 4.
- X then runs, and x = 2.
Answer: 2
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the
P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlock-free order of invoking the P operations by the processes?
Answer: (B)
A deadlock cannot happen when every process asks for the semaphores in the same global order, because then a circular wait cannot form.
(B): X takes b, a, c. Y takes b, c, d. Z takes a, c, d. All of them follow the order b, a, c, d, so there is no deadlock.
The others can deadlock:
- (A): X takes a, Y takes b, Z takes c and d. Then X waits for b (held by Y), Y waits for c (held by Z), and Z waits for a (held by X). Circular wait.
- (C): X takes b, Y takes c, X takes a. Then X waits for c (held by Y) and Y waits for b (held by X).
- (D): X takes a and b, Y takes c. Then X waits for c (held by Y) and Y waits for b (held by X).
Answer: (B)
(B): X takes b, a, c. Y takes b, c, d. Z takes a, c, d. All of them follow the order b, a, c, d, so there is no deadlock.
The others can deadlock:
- (A): X takes a, Y takes b, Z takes c and d. Then X waits for b (held by Y), Y waits for c (held by Z), and Z waits for a (held by X). Circular wait.
- (C): X takes b, Y takes c, X takes a. Then X waits for c (held by Y) and Y waits for b (held by X).
- (D): X takes a and b, Y takes c. Then X waits for c (held by Y) and Y waits for b (held by X).
Answer: (B)
A scheduling algorithm assigns priority proportional to the waiting time of a process. Every process starts with priority zero (the lowest priority). The scheduler re-evaluates the process priorities every T time units and decides the next process to schedule. Which one of the following is TRUE if the processes have no I/O operations and all arrive at time zero?
Answer: (B)
The process that has waited the longest gets the highest priority. Once it has run for T units, its waiting time (its priority) starts again from zero, so it moves to the back of the line. The processes then run one after another in a cycle, just like round robin.
Answer: This algorithm is equivalent to the round-robin algorithm
Answer: This algorithm is equivalent to the round-robin algorithm