GATE 2014 (Session 2)
65 questions
MCQ 42 · NAT 231-mark 30 · 2-mark 35
Questions 41–45 of 65
Consider the procedure below for the Producer-Consumer problem which uses semaphores:
Which one of the following is TRUE?
semaphore n = 0;
semaphore s = 1;
void producer() void consumer()
{ {
while(true) while(true)
{ {
produce(); semWait(s);
semWait(s); semWait(n);
addToBuffer(); removeFromBuffer();
semSignal(s); semSignal(s);
semSignal(n); consume();
} }
} }Which one of the following is TRUE?
Answer: (C)
In the consumer, semWait(s) comes before semWait(n).
Say the buffer is empty (n = 0) and the consumer gets s first. The consumer then waits on n and still holds s. The producer now waits on semWait(s), so it can never signal n. Deadlock.
Answer: Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty
Say the buffer is empty (n = 0) and the consumer gets s first. The consumer then waits on n and still holds s. The producer now waits on semWait(s), so it can never signal n. Deadlock.
Answer: Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty
Three processes A, B and C each execute a loop of 100 iterations. In each iteration of the loop, a process performs a single computation that requires CPU milliseconds and then initiates a single I/O operation that lasts for milliseconds. It is assumed that the computer where the processes execute has sufficient number of I/O devices and the OS of the computer assigns different I/O devices to each process. Also, the scheduling overhead of the OS is negligible. The processes have the following characteristics:
The processes A, B, and C are started at times 0, 5 and 10 milliseconds respectively, in a pure time sharing system (round robin scheduling) that uses a time slice of 50 milliseconds. The time in milliseconds at which process C would complete its first I/O operation is ___________.
The processes A, B, and C are started at times 0, 5 and 10 milliseconds respectively, in a pure time sharing system (round robin scheduling) that uses a time slice of 50 milliseconds. The time in milliseconds at which process C would complete its first I/O operation is ___________.
Answer: 1000
Round robin, quantum 50 ms. CPU needed before the first I/O: A 100, B 350, C 200.
- 0-50 A, 50-100 B, 100-150 C, 150-200 A (A finishes, goes to I/O)
- 200-250 B, 250-300 C, 300-350 B, 350-400 C
- 400-450 B, 450-500 C (C has had 200 ms)
C begins I/O at 500 and ends it at 500 + 500 = 1000 ms.
Answer: 1000
- 0-50 A, 50-100 B, 100-150 C, 150-200 A (A finishes, goes to I/O)
- 200-250 B, 250-300 C, 300-350 B, 350-400 C
- 400-450 B, 450-500 C (C has had 200 ms)
C begins I/O at 500 and ends it at 500 + 500 = 1000 ms.
Answer: 1000
A computer has twenty physical page frames which contain pages numbered 101 through 120. Now a program accesses the pages numbered 1, 2, ..., 100 in that order, and repeats the access sequence THRICE. Which one of the following page replacement policies experiences the same number of page faults as the optimal page replacement policy for this program?
Answer: (D)
Pages 1 to 100 are accessed in a cycle, three times, and only 20 frames are available. Take pages 101 to 120 as dead, since they are never used again, so they are replaced first.
LRU and FIFO: in a cycle longer than the memory, the page used (or loaded) longest ago is the page needed soonest. They throw it out every time, so every access faults: faults.
Optimal: it evicts the page whose next use is farthest away. In a cycle, that is the page just used. After the first pass, pages 1 to 19 and 100 stay in memory. Each of passes 2 and 3 then has 20 hits and 80 faults, so the total is faults.
MRU: it evicts the most recently used page, which is exactly the page optimal evicts here. So it makes the same choices and gives the same 260 faults.
LIFO: it evicts the page loaded last, and a hit does not change the load order. At the start of pass 2 it evicts page 100 instead, so it gets 262 faults, slightly worse than optimal.
Answer: (D)
LRU and FIFO: in a cycle longer than the memory, the page used (or loaded) longest ago is the page needed soonest. They throw it out every time, so every access faults: faults.
Optimal: it evicts the page whose next use is farthest away. In a cycle, that is the page just used. After the first pass, pages 1 to 19 and 100 stay in memory. Each of passes 2 and 3 then has 20 hits and 80 faults, so the total is faults.
MRU: it evicts the most recently used page, which is exactly the page optimal evicts here. So it makes the same choices and gives the same 260 faults.
LIFO: it evicts the page loaded last, and a hit does not change the load order. At the start of pass 2 it evicts page 100 instead, so it gets 262 faults, slightly worse than optimal.
Answer: (D)
For a C program accessing x[i][j][k], the following intermediate code is generated by a compiler. Assume that the size of an integer is 32 bits and the size of a character is 8 bits.
t0 = i * 1024
t1 = j * 32
t2 = k * 4
t3 = t1 + t0
t4 = t3 + t2
t5 = x[t4]
Which one of the following statements about the source code for the C program is CORRECT?
t0 = i * 1024
t1 = j * 32
t2 = k * 4
t3 = t1 + t0
t4 = t3 + t2
t5 = x[t4]
Which one of the following statements about the source code for the C program is CORRECT?
Answer: (A)
Step for t2: k 4 so one element takes 4 bytes (int).
Step for t1: j 32 one row of the last dimension is 32 B, which is 8 ints so the third dimension is 8.
Step for t0: i 1024 1024 / 32 = 32 rows so the second dimension is 32.
So x is declared as int x[ ][32][8]. This matches int x[32][32][8].
Answer: int x[32][32][8]
Step for t1: j 32 one row of the last dimension is 32 B, which is 8 ints so the third dimension is 8.
Step for t0: i 1024 1024 / 32 = 32 rows so the second dimension is 32.
So x is declared as int x[ ][32][8]. This matches int x[32][32][8].
Answer: int x[32][32][8]
Let be the encoding of a Turing machine as a string over ={0,1}.
Let L = { |M is a Turing machine that accepts a string of length 2014}.
Then, L is
Let L = { |M is a Turing machine that accepts a string of length 2014}.
Then, L is
Answer: (B)
Recursively enumerable: Run M on every string of length 2014 at the same time (dovetailing). If M accepts any one of them, accept.
Undecidable: This is a non-trivial property of the language of M. So Rice's theorem says it is undecidable.
Answer: undecidable but recursively enumerable
Undecidable: This is a non-trivial property of the language of M. So Rice's theorem says it is undecidable.
Answer: undecidable but recursively enumerable