GATE 2006
191 questions
MCQ 1911-mark 191
Questions 146–150 of 191
Consider the solution to the bounded buffer producer/consumer problem by using general semaphores S, F, and E. The semaphore S is the mutual exclusion semaphore initialized to 1. The semaphore F corresponds to the number of free slots in the buffer and is initialized to N. The semaphore E corresponds to the number of elements in the buffer and is initialized to 0.
Which of the following interchange operations may result in a deadlock?
I. Interchanging Wait (F) and Wait (S) in the Producer process
II. Interchanging Signal (S) and Signal (F) in the Consumer process
Which of the following interchange operations may result in a deadlock?
I. Interchanging Wait (F) and Wait (S) in the Producer process
II. Interchanging Signal (S) and Signal (F) in the Consumer process
Answer: (A)
I. Interchanging and in the Producer. Take a full buffer, so . The producer does first and succeeds, so it holds . Then it blocks on . The consumer can free a slot only after , but the producer holds and the consumer waits for it. Neither can move, so a deadlock can happen.
II. Interchanging and in the Consumer. A Signal never blocks, so changing the order of two Signals cannot cause a deadlock. It only changes which waiting process wakes first. A producer woken by may have to wait briefly for , and the consumer releases next. So no deadlock.
Answer: (A)
II. Interchanging and in the Consumer. A Signal never blocks, so changing the order of two Signals cannot cause a deadlock. It only changes which waiting process wakes first. A producer woken by may have to wait briefly for , and the consumer releases next. So no deadlock.
Answer: (A)
Barrier is a synchronization construct where a set of processes synchronizes globally i.e. each process in the set arrives at the barrier and waits for all others to arrive and then all processes leave the barrier. Let the number of processes in the set be three and S be a binary semaphore with the usual P and V functions. Consider the following C implementation of a barrier with line numbers shown on left.
Which one of the following rectifies the problem in the implementation?
void barrier (void) {
1: P(S);
2: process_arrived++;
3. V(S);
4: while (process_arrived !=3);
5: P(S);
6: process_left++;
7: if (process_left==3) {
8: process_arrived = 0;
9: process_left = 0;
10: }
11: V(S);
}The variables process_arrived and process_left are shared among all processes and are initialized to zero. In a concurrent program all the three processes call the barrier function when they need to synchronize globally.Which one of the following rectifies the problem in the implementation?
Answer: (B)
The flaw.
(B) fixes it. If the first process of a new call waits until
(A) does not fix it. It removes the
(C) is wrong. A process spinning at line 4 with context switch disabled never lets the others run and arrive, so the first call already deadlocks.
(D) is wrong. A private
Answer: (B)
process_arrived is reset to 0 only by the last process to leave (process_left == 3). A fast process can leave, call barrier again, and run lines 1 to 3 while the other two have not left yet. It raises process_arrived to 4 and spins at line 4 for ever, because 4 is not 3. When the other two finally leave, process_left becomes 3 and both counters are reset to 0, which also wipes out that early arrival. The three processes can then reach at most 2 arrivals, so all of them spin at line 4: deadlock.(B) fixes it. If the first process of a new call waits until
process_arrived is 0, it cannot count itself in before the previous round has been reset. So process_arrived never goes above 3 and the reset is not lost.(A) does not fix it. It removes the
process_left count, so nothing makes a process wait until the others have left. A fast process can still come back and change process_arrived while the others are still at line 4, and the barrier is still broken.(C) is wrong. A process spinning at line 4 with context switch disabled never lets the others run and arrive, so the first call already deadlocks.
(D) is wrong. A private
process_left reaches only 1 in each process, never 3, so process_arrived is never reset. In the second call it goes past 3 and the processes wait for ever.Answer: (B)
Barrier is a synchronization construct where a set of processes synchronizes globally i.e. each process in the set arrives at the barrier and waits for all others to arrive and then all processes leave the barrier. Let the number of processes in the set be three and S be a binary semaphore with the usual P and V functions. Consider the following C implementation of a barrier with line numbers shown on left.
The above implementation of barrier is incorrect. Which one of the following is true?
void barrier (void) {
1: P(S);
2: process_arrived++;
3. V(S);
4: while (process_arrived !=3);
5: P(S);
6: process_left++;
7: if (process_left==3) {
8: process_arrived = 0;
9: process_left = 0;
10: }
11: V(S);
}The variables process_arrived and process_left are shared among all processes and are initialized to zero. In a concurrent program all the three processes call the barrier function when they need to synchronize globally.The above implementation of barrier is incorrect. Which one of the following is true?
Answer: (B)
(B) is true. Let
(A) is false.
(C) is false. Lines 6 to 10 change shared variables. Without
(D) is false. With two processes the same trace works: one process comes back early, raises
Answer: (B)
p1, p2, p3 reach the barrier. After line 2 has run for all three, process_arrived = 3 and the loop at line 4 lets them through. Say p1 runs the whole function: process_left = 1, and no reset yet. It calls barrier again, and at line 2 it makes process_arrived = 4. At line 4 it is stuck, since 4 is not 3. Now p2 and p3 finish the first call. process_left becomes 3, so both counters are reset to 0. They call barrier again, and process_arrived reaches only 2. No process can pass line 4. This is a deadlock.(A) is false.
S is used correctly: P(S) and V(S) wrap each update of the shared counters. The fault is in the logic of the counters, not in the semaphore being binary.(C) is false. Lines 6 to 10 change shared variables. Without
S, two processes could run process_left++ together and lose an update, so they must stay in a critical section.(D) is false. With two processes the same trace works: one process comes back early, raises
process_arrived past 2, and the reset leaves the counter too low for the barrier to open again.Answer: (B)
The atomic feth-and-set x,y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x . Consider the following implementation of P and V functions on a binary semaphore S.
void P(binary_semaphore*S){
unsigned y;
unsigned*x =& (S->value);
do {
fetch-and-set x,y;
} while(y);
}
void V (binary_semphore*S){
S_>value = 0;
} Which one of the following is true?Answer: (A)
fetch-and-set sets S->value to 1 and returns the old value in y. So P loops (busy-waits) until it reads 0 (semaphore free) and leaves it as 1 (taken). V sets it to 0.(A) is true. Take one processor. If context switching is disabled inside
P and the semaphore is already taken, the process spins in the loop and is never taken off the CPU. The process that holds the semaphore never runs, so it never calls V. The spinning goes on for ever.(B) is false. A normal load followed by a store is not atomic. Two processes could both load 0 and both store 1, and both would enter, so mutual exclusion breaks.
(C) is false. Setting
S->value = 0 in V is right, because 0 means free for the test in P.(D) is false. Only one process gets past
P until V is called, so this does work as a binary semaphore (with busy waiting).Answer: (A)
Consider the following snapshot of a system running n processes. Process i is holding instances of a resource R, for . Currently, all instances of R are occupied. Further, for all i , process i has placed a request for an additional , instances while holding the instances it already has, There are exactly two processes p and q such that ==0. Which one of the following can serve as a necessary condition to guarantee that the system is not approaching a deadlock?
Answer: (B)
All instances of R are occupied, so nothing is free now. and ask for nothing more (), so both can finish and release their units. Then the free units are .
Any other process needs more units. If even the smallest of these is more than , no other process can ever proceed, and they are in deadlock. So we must have . This is a necessary condition, and it is option (B). It only guarantees that the system can move on from its present state, not that no deadlock arises later.
The other options are not necessary. Take three processes with , and a third process with . After and finish, units are free, so can finish and there is no deadlock. Yet in this case (so C fails) and (so D fails). Next take , : there is no deadlock, but (so A fails).
Answer: (B)
Any other process needs more units. If even the smallest of these is more than , no other process can ever proceed, and they are in deadlock. So we must have . This is a necessary condition, and it is option (B). It only guarantees that the system can move on from its present state, not that no deadlock arises later.
The other options are not necessary. Take three processes with , and a third process with . After and finish, units are free, so can finish and there is no deadlock. Yet in this case (so C fails) and (so D fails). Next take , : there is no deadlock, but (so A fails).
Answer: (B)