GATE 2004
199 questions
MCQ 1991-mark 199
Questions 136–140 of 199
The semaphore variables full, empty and mutex are initialized to 0, n and 1, respectively. Process P1 repeatedly adds one item at a time to a buffer of size n, and process P2 repeatedly removes one item at a time from the same buffer using the programs given below. In the programs, K, L, M and N are unspecified statements.
P1
P2
The statements K, L, M and N are respectively
P1
while (1) {
K;
P(mutex);
Add an item to the buffer;
V(mutex);
L;
} P2
while (1) {
M;
P(mutex);
Remove an item from the buffer;
V(mutex);
N;
} The statements K, L, M and N are respectively
Answer: (D)
empty counts free slots (starts at ). full counts items in the buffer (starts at 0).P1 is the producer. It needs a free slot, so it waits on
empty: K = P(empty). After adding an item there is one more item, so L = V(full).P2 is the consumer. It needs an item, so it waits on
full: M = P(full). After removing an item there is one more free slot, so N = V(empty).Other options: in (A) and (B) the producer waits on
full, which is 0 at the start, so it would block at once. In (C) the consumer waits on empty, which is , so it could remove an item from an empty buffer.Answer: (D)
Consider two processes P1 and P2 accessing the shared variables X and Y protected by two binary semaphores Sx and Sy respectively, both initialized to 1. P and V denote the usual semaphore operators, where P decrements the semaphore value, and V increments the semaphore value. The pseudo-code of P1 and P2 is as follows:
P1
P2
P1
While true do {
L1 : ................
L2 : ................
X = X + 1;
Y = Y - 1;
V(Sx);
V(Sy);
}P2
While true do {
L3 : ................
L4 : ................
Y = Y + 1;
X = Y - 1;
V(Sy);
V(Sx);
} In order to avoid deadlock, the correct operators at L1 , L2 , L3 and L4 are respectively.Answer: (D)
Deadlock needs a circular wait. It can be avoided if both processes take the two semaphores in the same order.
(D) P1 does
(A) P1 takes
(B) P1 takes
(C) P1 does
Answer: (D)
(D) P1 does
P(Sx), P(Sy) and P2 does P(Sx), P(Sy). Whichever process gets Sx first, the other blocks at its first P(Sx) and holds nothing. So the first process can take Sy, finish, and release both. Then the other goes on. No circular wait can form.(A) P1 takes
Sy then Sx, P2 takes Sx then Sy. Say P1 takes Sy and P2 takes Sx. Each now waits for the semaphore the other holds: deadlock.(B) P1 takes
Sx then Sy, P2 takes Sy then Sx. Opposite orders again: P1 holds Sx and wants Sy, P2 holds Sy and wants Sx: deadlock.(C) P1 does
P(Sx) twice. Sx is binary, so the second P(Sx) blocks P1 on a semaphore it already holds. P1 waits for ever. P(Sy) twice in P2 does the same.Answer: (D)
In a certain operating system, deadlock prevention is attemped using the following scheme. Each process is assigned a unique timestamp, and is restarted with the same timestamp if killed. Let be the process holding a resource R, be a process requesting for the same resource R, and T() and T() be their timestamps respectively. The decision to wait or preempt one of the processes is based on the following algorithm.
if T(Pr) < T(Ph) then
kill Pr
else wait Which one of the following is TRUE?
Answer: (A)
Deadlock-free: a process waits only when its timestamp is larger than the holder's: . The requester is always the younger one. So every wait edge goes from a larger timestamp to a smaller one. A cycle of waiting processes would need timestamps that keep getting smaller and then return to the start, which is impossible. So no deadlock.
Not starvation-free: when , the requesting process is killed, even though it is the older one. It restarts with the same timestamp. If a younger process holds the resource each time it asks, it is killed again. New processes keep getting larger timestamps, so they can keep holding the resource whenever this old process asks, and it may never complete.
Answer: (A)
Not starvation-free: when , the requesting process is killed, even though it is the older one. It restarts with the same timestamp. If a younger process holds the resource each time it asks, it is killed again. New processes keep getting larger timestamps, so they can keep holding the resource whenever this old process asks, and it may never complete.
Answer: (A)
Consider the following set of processes, with the arrival times and the CPU-burst times given in milliseconds.
What is the average turnaround time for these processes with the preemptive shortest remaining processing time first (SRPT) algorithm?
What is the average turnaround time for these processes with the preemptive shortest remaining processing time first (SRPT) algorithm?
Answer: (A)
SRPT always runs the process with the shortest remaining time, and a new arrival can pre-empt.
- : only is present, so it runs.
- : arrives with 3. has 4 left, so pre-empts it.
- : arrives with 3. has 2 left, so it continues.
- : ends. arrives with 1, so it is the shortest and runs. Waiting are (4 left) and (3).
- : ends. (3) is shorter than (4), so runs to 8.
- : runs to 12.
Completion times: , , , .
Turnaround time = completion time minus arrival time:
, , , .
Average = ms.
Answer: (A)
- : only is present, so it runs.
- : arrives with 3. has 4 left, so pre-empts it.
- : arrives with 3. has 2 left, so it continues.
- : ends. arrives with 1, so it is the shortest and runs. Waiting are (4 left) and (3).
- : ends. (3) is shorter than (4), so runs to 8.
- : runs to 12.
Completion times: , , , .
Turnaround time = completion time minus arrival time:
, , , .
Average = ms.
Answer: (A)
A disk has 200 tracks (numbered 0 through 199). At a given time, it was servicing the request of reading data from track 120, and at the previous request, service was for track 90. The pending requests (in order of their arrival) are for track numbers.
30 70 115 130 110 80 20 25.
How many times will the head change its direction for the disk scheduling policies SSTF(Shortest Seek Time First) and FCFS (First Come First Serve)?
30 70 115 130 110 80 20 25.
How many times will the head change its direction for the disk scheduling policies SSTF(Shortest Seek Time First) and FCFS (First Come First Serve)?
Answer: (C)
The head arrives at 120 from 90, so it is moving up. The pending requests are 30, 70, 115, 130, 110, 80, 20, 25.
SSTF (nearest first):
- From 120: 115 is 5 away, 130 is 10 away, so go to 115. The head turns here (up, then down).
- From 115: 110 is 5 away, so go to 110.
- From 110: 130 is 20 away, 80 is 30 away, so go to 130. The head turns here (down, then up).
- From 130: 80 is 50 away and it is the nearest, so go to 80. The head turns here (up, then down).
- From 80: 70 (10), then 30 (40), then 25 (5), then 20 (5). All of these are downwards.
Order: . Direction changes at 120, 110 and 130, so 3 changes.
FCFS (in the order of arrival):
Up to 120, down to 30, up to 130, down to 20, up to 25. The direction changes at 120, 30, 130 and 20, so 4 changes.
So SSTF has 3 changes and FCFS has 4.
Answer: (C)
SSTF (nearest first):
- From 120: 115 is 5 away, 130 is 10 away, so go to 115. The head turns here (up, then down).
- From 115: 110 is 5 away, so go to 110.
- From 110: 130 is 20 away, 80 is 30 away, so go to 130. The head turns here (down, then up).
- From 130: 80 is 50 away and it is the nearest, so go to 80. The head turns here (up, then down).
- From 80: 70 (10), then 30 (40), then 25 (5), then 20 (5). All of these are downwards.
Order: . Direction changes at 120, 110 and 130, so 3 changes.
FCFS (in the order of arrival):
Up to 120, down to 30, up to 130, down to 20, up to 25. The direction changes at 120, 30, 130 and 20, so 4 changes.
So SSTF has 3 changes and FCFS has 4.
Answer: (C)