GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 141–145 of 185
The following is a code with two threads, producer and consumer, that can run in parallel. Further, S and Q are binary semaphores quipped with the standard P and V operations.
Which of the following is TRUE about the program above?
semaphore S = 1, Q = 0;
integer x;
producer: consumer:
while (true) do while (true) do
P(S); P(Q);
x = produce (); consume (x);
V(Q); V(S);
done done Which of the following is TRUE about the program above?
Answer: (D)
Start with S = 1 and Q = 0. The producer does P(S), produces x, then V(Q). The consumer does P(Q), consumes x, then V(S).
The producer can produce only after it passes P(S). S becomes 1 again only when the consumer finishes consuming and does V(S). The consumer can consume only after the producer does V(Q). So the two strictly alternate: produce, consume, produce, consume. At any time exactly one of the two can go ahead (S = 1 and Q = 0, or S = 0 and Q = 1), or it is already inside its section.
- (A) No deadlock: both are never blocked at the same time, as shown above.
- (B) No starvation: they take turns, so each waits only for the other's one step.
- (C) No item is lost: the producer cannot write a new x until the consumer has consumed the old one.
- (D) True: the value in x is always consumed before the producer makes a new value.
Answer: (D)
The producer can produce only after it passes P(S). S becomes 1 again only when the consumer finishes consuming and does V(S). The consumer can consume only after the producer does V(Q). So the two strictly alternate: produce, consume, produce, consume. At any time exactly one of the two can go ahead (S = 1 and Q = 0, or S = 0 and Q = 1), or it is already inside its section.
- (A) No deadlock: both are never blocked at the same time, as shown above.
- (B) No starvation: they take turns, so each waits only for the other's one step.
- (C) No item is lost: the producer cannot write a new x until the consumer has consumed the old one.
- (D) True: the value in x is always consumed before the producer makes a new value.
Answer: (D)
The P and V operations on counting semaphores, where s is a counting semaphore, are defined as follows:
P(s) : s = s - 1;
if (s < 0) then wait;
V(s) : s = s + 1;
if (s <= 0) then wakeup a process waiting on s; Assume that and the wait and signal operations on binary semaphores are provided. Two binary semaphores and are used to implement the semaphore operations P(s) and V(s) as follows: P(s) : Pb(xb);
s = s - 1;
if (s < 0) {
Vb(xb) ;
Pb(Yb) ;
}
else Vb(xb);
V(s) : Pb(xb) ;
s = s + 1;
if (s <= 0) Vb(Yb) ;
Vb(xb) ; The initial values of xb and yb are respectivelyAnswer: (C)
Here protects the updates of (it works like a lock), and is where a process waits when .
xb must be 1. Both P(s) and V(s) start with . If started at 0, every process would block there and none could ever run, so (A) and (B) are out.
yb must be 0. Take and four processes. P1 makes and P2 makes ; neither is below 0, so neither waits. P3 makes , so it calls and then . This must block P3. With it blocks. With it would pass at once, and a third process would enter although the counting semaphore allows only two. Later, each V(s) with calls and wakes exactly one waiting process.
So the initial values are and .
Answer: (C)
xb must be 1. Both P(s) and V(s) start with . If started at 0, every process would block there and none could ever run, so (A) and (B) are out.
yb must be 0. Take and four processes. P1 makes and P2 makes ; neither is below 0, so neither waits. P3 makes , so it calls and then . This must block P3. With it blocks. With it would pass at once, and a third process would enter although the counting semaphore allows only two. Later, each V(s) with calls and wakes exactly one waiting process.
So the initial values are and .
Answer: (C)
An operating system implements a policy that requires a process to release all resources before making a request for another resource. Select the TRUE statement from the following:
Answer: (B)
The policy says a process must release all its resources before it asks for another one. So a process never holds a resource while it waits for another. This breaks the hold and wait condition, so deadlock cannot occur.
But each time the process needs something new, it gives up everything it holds and must ask again. Other processes may take those resources in the meantime, and the process may wait for them without end. So starvation can occur. Resources may also be used poorly, since the process releases them before it has used them fully.
Answer: (B)
But each time the process needs something new, it gives up everything it holds and must ask again. Other processes may take those resources in the meantime, and the process may wait for them without end. So starvation can occur. Resources may also be used poorly, since the process releases them before it has used them fully.
Answer: (B)
Which of the following is NOT true of deadlock prevention and deadlock avoidance schemes?
Answer: (A)
(A) is NOT true. Deadlock prevention only makes sure that one of the four necessary conditions can never hold. For example, resources are numbered and must be requested in order. Such a rule can refuse a request even when the resulting state is safe. So it is wrong to say that prevention always grants the request when the state is safe.
(B) True. Deadlock avoidance (for example the Banker's algorithm) checks whether the resulting state is safe, and grants the request if it is.
(C) True. Avoidance refuses a request only when the state would become unsafe, but prevention refuses more requests. So avoidance is less restrictive.
(D) True. To test for a safe state, avoidance needs the maximum resource needs of every process in advance.
Answer: (A)
(B) True. Deadlock avoidance (for example the Banker's algorithm) checks whether the resulting state is safe, and grants the request if it is.
(C) True. Avoidance refuses a request only when the state would become unsafe, but prevention refuses more requests. So avoidance is less restrictive.
(D) True. To test for a safe state, avoidance needs the maximum resource needs of every process in advance.
Answer: (A)
If the time-slice used in the round-robin scheduling policy is more than the maximum time required to execute any process, then the policy will
Answer: (C)
Answer is (C).
Round robin works like FCFS, but each process runs only for one time slice at a time. If the time slice is larger than the maximum time any process needs, every process finishes in the first cycle itself. So the policy simply becomes FCFS.