GATE 2021 (Session 2)
64 questions
MCQ 33 · MSQ 13 · NAT 181-mark 29 · 2-mark 35
Questions 51–55 of 64
Consider the following multi-threaded code segment (in a mix of C and pseudo-code), invoked by two processes P1 and P2, and each of the processes spawns two threads T1 and T2:
int x = 0; // global
Lock L1; // global
main () {
create a thread to execute foo( ); // Thread T1
create a thread to execute foo( ); // Thread T2
wait for the two threads to finish execution;
print(x);}
foo() {
int y = 0;
Acquire L1;
x = x + 1;
y = y + 1;
Release L1;
print (y);} Which of the following statement(s) is/are correct?One or more options are correct.
Answer: (A), (D)
Let us check each statement:
- P1 and P2 are different processes, so each has its own copy of x. In each process, the two threads add 1 to x while holding the lock so each process prints x = 2.
- y is a local variable of foo, so every thread has its own y that starts at 0 so every thread prints y = 1.
Answer: (A), (D)
- P1 and P2 are different processes, so each has its own copy of x. In each process, the two threads add 1 to x while holding the lock so each process prints x = 2.
- y is a local variable of foo, so every thread has its own y that starts at 0 so every thread prints y = 1.
Answer: (A), (D)
Consider a computer system with multiple shared resource types, with one instance per resource type. Each instance can be owned by only one process at a time. Owning and freeing of resources are done by holding a global lock (L). The following scheme is used to own a resource instance:
function OWNRESOURCE(Resource R)
Acquire lock L // a global lock
if R is available then
Acquire R
Release lock L
else
if R is owned by another process P then
Terminate P, after releasing all resources owned by P
Acquire R
Restart P
Release lock L
end if
end if
end functionWhich of the following choice(s) about the above scheme is/are correct?One or more options are correct.
Answer: (A), (B), (C)
- (A) The owner is terminated instead of making the new process wait, so no process ever waits while holding resources. A circular wait cannot form, so there is no deadlock. True.
- (B) Two processes can keep terminating and restarting each other, and neither makes progress, so livelock is possible. True.
- (C) One process can be terminated again and again, so it may never finish, so starvation is possible. True.
- (D) A resource still has only one owner at any time, because the lock L guards owning and freeing. So mutual exclusion holds. False.
Answer: (A), (B), (C)
- (B) Two processes can keep terminating and restarting each other, and neither makes progress, so livelock is possible. True.
- (C) One process can be terminated again and again, so it may never finish, so starvation is possible. True.
- (D) A resource still has only one owner at any time, because the lock L guards owning and freeing. So mutual exclusion holds. False.
Answer: (A), (B), (C)
If the numerical value of a 2-byte unsigned integer on a little endian computer is 255 more than that on a big endian computer, which of the following choices represent(s) the unsigned integer on a little endian computer?
One or more options are correct.
Answer: (A), (D)
Take the little-endian value as , where is the high byte and is the low byte. The same two bytes in memory, read as big-endian, give .
The difference is . Setting it equal to gives .
- 0x6665: , , so . True
- 0x0100: , , so . True
- 0x0001: , , so . False
- 0x4243: , , so . False
Answer: (A), (D)
The difference is . Setting it equal to gives .
- 0x6665: , , so . True
- 0x0100: , , so . True
- 0x0001: , , so . False
- 0x4243: , , so . False
Answer: (A), (D)
Consider a computer network using the distance vector routing algorithm in its network layer. The partial topology of the network is shown below.
The objective is to find the shortest-cost path from the router R to routers P and Q. Assume that R does not initially know the shortest routes to P and Q. Assume that R has three neighboring routers denoted as X, Y and Z. During one iteration, R measures its distance to its neighbors X, Y, and Z as 3, 2 and 5, respectively. Router R gets routing vectors from its neighbors that indicate that the distance to router P from routers X, Y and Z are 7, 6 and 5, respectively. The routing vector also indicates that the distance to router Q from routers X, Y and Z are 4, 6 and 8 respectively. Which of the following statement(s) is/are correct with respect to the new routing table o R, after updation during this iteration?
The objective is to find the shortest-cost path from the router R to routers P and Q. Assume that R does not initially know the shortest routes to P and Q. Assume that R has three neighboring routers denoted as X, Y and Z. During one iteration, R measures its distance to its neighbors X, Y, and Z as 3, 2 and 5, respectively. Router R gets routing vectors from its neighbors that indicate that the distance to router P from routers X, Y and Z are 7, 6 and 5, respectively. The routing vector also indicates that the distance to router Q from routers X, Y and Z are 4, 6 and 8 respectively. Which of the following statement(s) is/are correct with respect to the new routing table o R, after updation during this iteration?
One or more options are correct.
Answer: (B), (C)
Cost through each neighbour = (R to neighbour) + (neighbour's distance):
- To P: X 3 + 7 = 10, Y 2 + 6 = 8, Z 5 + 5 = 10 8 via Y
- To Q: X 3 + 4 = 7, Y 2 + 6 = 8, Z 5 + 8 = 13 7 via X
So R to P is 8 via Y (not 10), and R to Q is 7 via X (not via Z).
Answer: Distance to Q is 7; next hop to P is Y
- To P: X 3 + 7 = 10, Y 2 + 6 = 8, Z 5 + 5 = 10 8 via Y
- To Q: X 3 + 4 = 7, Y 2 + 6 = 8, Z 5 + 8 = 13 7 via X
So R to P is 8 via Y (not 10), and R to Q is 7 via X (not via Z).
Answer: Distance to Q is 7; next hop to P is Y
Consider the following directed graph:
Which of the following is/are correct about the graph?
Which of the following is/are correct about the graph?
One or more options are correct.
Answer: (A), (B)
Call the rows 1 to 4 from the top. Reading the arrows, the graph has three directed 4-cycles:
- the top-middle square (rows 1 and 2, columns 2 and 3),
- the bottom-left square through S (rows 3 and 4, columns 1 and 2),
- the bottom-right square (rows 3 and 4, columns 3 and 4).
Each of these is a strongly connected component (SCC) and has exactly 4 edges, no others. The remaining 4 vertices (top-left corner, the vertex below it, and the two on the right edge of rows 1 and 2) are single-vertex SCCs. No edge goes back from one cycle to an earlier one, so these are all the SCCs.
(A) True. The graph has cycles, so it has no topological order.
(B) True. S reaches every vertex, so one DFS from S visits all of them. A back edge joins a vertex to its ancestor, so it lies on a cycle, and it can only be inside an SCC. DFS enters each 4-cycle at one vertex and walks round it along 3 tree edges. The 4th edge returns to the entry vertex, so it is a back edge. That gives 1 back edge per cycle, 3 in all.
(C) False. Every graph has SCCs, at least the single vertices.
(D) False. The top-right corner has no outgoing edge, so it cannot reach any other vertex.
Answer: (A), (B)
- the top-middle square (rows 1 and 2, columns 2 and 3),
- the bottom-left square through S (rows 3 and 4, columns 1 and 2),
- the bottom-right square (rows 3 and 4, columns 3 and 4).
Each of these is a strongly connected component (SCC) and has exactly 4 edges, no others. The remaining 4 vertices (top-left corner, the vertex below it, and the two on the right edge of rows 1 and 2) are single-vertex SCCs. No edge goes back from one cycle to an earlier one, so these are all the SCCs.
(A) True. The graph has cycles, so it has no topological order.
(B) True. S reaches every vertex, so one DFS from S visits all of them. A back edge joins a vertex to its ancestor, so it lies on a cycle, and it can only be inside an SCC. DFS enters each 4-cycle at one vertex and walks round it along 3 tree edges. The 4th edge returns to the entry vertex, so it is a back edge. That gives 1 back edge per cycle, 3 in all.
(C) False. Every graph has SCCs, at least the single vertices.
(D) False. The top-right corner has no outgoing edge, so it cannot reach any other vertex.
Answer: (A), (B)