GATE 2007
184 questions
MCQ 1841-mark 184
Questions 131–135 of 184
Synchronization in the classical readers and writers problem can be achieved through use of semaphores. In the following incomplete code for readers-writers problem, two binary semaphores mutex and wrt are used to obtain synchronization
The values of S1, S2, S3, S4, (in that order) are
wait (wrt)
writing is performed
signal (wrt)
wait (mutex)
readcount = readcount + 1
if readcount = 1 then S1
S2
reading is performed
S3
readcount = readcount - 1
if readcount = 0 then S4
signal (mutex) The values of S1, S2, S3, S4, (in that order) are
Answer: (C)
wrt keeps writers out, and mutex protects readcount.S1 =
wait (wrt): when readcount becomes 1, this is the first reader. It locks out writers for the whole time any reader is reading.S2 =
signal (mutex): the update of readcount is done, so release mutex before reading. Other readers can then enter and read at the same time.S3 =
wait (mutex): reading is over, so take mutex again before changing readcount.S4 =
signal (wrt): when readcount becomes 0, this is the last reader. It lets a writer in. The final signal (mutex) then releases mutex.Answer: (C)
Processes P1 and P2 use critical_flag in the following routine to achieve mutual exclusion. Assume that critical_flag is initialized to FALSE in the main program.
Consider the following statements.
i.It is possible for both P1 and P2 to access critical_region concurrently.
ii.This may lead to a deadlock.
Which of the following holds?
get_exclusive_access ( )
{
if (critical _flag == FALSE) {
critical_flag = TRUE ;
critical_region () ;
critical_flag = FALSE;
}
} Consider the following statements.
i.It is possible for both P1 and P2 to access critical_region concurrently.
ii.This may lead to a deadlock.
Which of the following holds?
Answer: (C)
(i) is true. Say P1 finds
(ii) is false. A process that finds the flag TRUE does not wait. It skips the
Answer: (C)
critical_flag == FALSE and a context switch happens before it runs critical_flag = TRUE. P2 also finds the flag FALSE, so both go into critical_region(). The test and the set are two separate steps, so this can happen.(ii) is false. A process that finds the flag TRUE does not wait. It skips the
if and goes on, so nobody blocks on the flag. A process that enters the region sets the flag to FALSE again when it leaves. So the flag cannot stay TRUE with nobody inside, and no deadlock can occur.Answer: (C)
Two processes, P1 and P2, need to access a critical section of code. Consider the following synchronization construct used by the processes:
Here, wants1 and wants2 are shared variables, which are initialized to false. Which one of the following statements is TRUE about the above construct?
/* P1 */ /* P2 */
while (true) { while (true) {
wants1 = true; wants2 = true;
while (wants2==true); while (wants1==true);
/* Critical /* Critical
Section */ Section */
wants1=false; wants2=false;
} }
/* Remainder section */ /* Remainder section */Here, wants1 and wants2 are shared variables, which are initialized to false. Which one of the following statements is TRUE about the above construct?
Answer: (D)
Deadlock is possible. P1 sets
Mutual exclusion holds. A process keeps its own flag true from before its test until it leaves the critical section. P1 enters only after seeing
Bounded waiting holds for the processes that do get through: once P2 has set
Not strict alternation. There is no
Answer: (D)
wants1 = true, then P2 sets wants2 = true. Now each spins in its while loop forever, because each sees the other's flag true. No process makes progress.Mutual exclusion holds. A process keeps its own flag true from before its test until it leaves the critical section. P1 enters only after seeing
wants2 == false, which means P2 has not yet raised its flag. If P1 is in the critical section, wants1 stays true, so P2 cannot pass its test until P1 leaves. The same holds the other way round.Bounded waiting holds for the processes that do get through: once P2 has set
wants2 = true, P1 cannot enter again before P2 does, so the other process enters at most once ahead of it.Not strict alternation. There is no
turn variable. P1 can enter again and again while P2 stays in its remainder section (wants2 stays false). So (C) is wrong. (A) is wrong because mutual exclusion holds. (B) is wrong for the reason above. The construct has the deadlock but keeps mutual exclusion.Answer: (D)
A single processor system has three resource types X, Y and Z, which are shared by three processes. There are 5 units of each resource type. Consider the following scenario, where the column alloc denotes the number of units of each resource type allocated to each process, and the column request denotes the number of units of each resource type requested by a process in order to complete execution. Which of these processes will finish LAST?
Answer: (C)
Allocated in total: X = , Y = , Z = . There are units of each type, so
Available .
Request vectors: P0 , P1 , P2 .
- Only P1 can run now: . P0 needs 1 X and P2 needs 1 X, but X = 0. P1 finishes and releases , so Available .
- P0: , so it can run. P2: needs 2 Y but only 1 is free, so it must wait. P0 finishes and releases , so Available .
- P2: , so it runs and finishes.
The order is P1, P0, P2, with no deadlock. So P2 finishes last.
Answer: (C)
Available .
Request vectors: P0 , P1 , P2 .
- Only P1 can run now: . P0 needs 1 X and P2 needs 1 X, but X = 0. P1 finishes and releases , so Available .
- P0: , so it can run. P2: needs 2 Y but only 1 is free, so it must wait. P0 finishes and releases , so Available .
- P2: , so it runs and finishes.
The order is P1, P0, P2, with no deadlock. So P2 finishes last.
Answer: (C)
An operating system uses Shortest Remaining Time first (SRT) process scheduling algorithm. Consider the arrival times and execution times for the following processes:
Process Execution time Arrival time
P1 20 0
P2 25 15
P3 10 30
P4 15 45 What is the total waiting time for process P2?Answer: (B)
Shortest Remaining Time first: whenever a process arrives or finishes, the process with the least remaining time runs.
- 0 to 20: P1 runs. P2 arrives at 15, when P1 has 5 ms left against 25 ms for P2, so P1 is not preempted and finishes at 20.
- 20 to 30: P2 (burst 25 ms) runs 10 ms, leaving 15 ms.
- t = 30: P3 arrives with 10 ms, less than the 15 ms P2 has left, so P3 preempts P2 and runs 30 to 40.
- 40 to 55: P2 resumes. P4 arrives at 45 with 15 ms, while P2 has 10 ms left, so P2 is not preempted and finishes at 55.
- 55 to 70: P4 runs.
Waiting time = completion time arrival time burst time.
For P2: ms. This matches the chart: P2 waited from 15 to 20 (5 ms) and from 30 to 40 (10 ms), 15 ms in all.
Answer: (B)