Process Synchronization · Operating System
41 questions · asked in 23 of 27 years (2000–2026)
MCQ 34 · MSQ 4 · NAT 31-mark 22 · 2-mark 19
2000Questions per year2026
Questions 26–30 of 41
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)
The wait and signal operations of a monitor are implemented using semaphores as follows. In the following,
x is a condition variable,
mutex is a semaphore initialized to 1,
x_sem is a semaphore initialized to 0,
x_count is the number of processes waiting on semaphore x_sem, initially 0,
next is a semaphore initialized to 0,
next_count is the number of processes waiting on semaphore next, initially 0.
The body of each procedure that is visible outside the monitor is replaced with the following:
Each occurrence of x.wait is replaced with the following:
Each occurrence of x.signal is replaced with the following:
For correct implementation of the monitor, statements E1 and E2 are, respectively,
x is a condition variable,
mutex is a semaphore initialized to 1,
x_sem is a semaphore initialized to 0,
x_count is the number of processes waiting on semaphore x_sem, initially 0,
next is a semaphore initialized to 0,
next_count is the number of processes waiting on semaphore next, initially 0.
The body of each procedure that is visible outside the monitor is replaced with the following:
P(mutex);
...
body of procedure
...
if (next_count > 0)
V(next);
else
V(mutex); Each occurrence of x.wait is replaced with the following:
x_count = x_count + 1;
if (next_count > 0)
V(next);
else
V(mutex);
------------------------------------------------------------ E1;
x_count = x_count - 1;
Each occurrence of x.signal is replaced with the following:
if (x_count > 0)
{
next_count = next_count + 1;
------------------- E2;
P(next);
next_count = next_count - 1;
} For correct implementation of the monitor, statements E1 and E2 are, respectively,
Answer: (D)
In x.wait, x_count is increased, then the process gives up the monitor with V(next) or V(mutex), and later x_count is decreased. x_count counts the processes waiting on x_sem, so the process must sleep on x_sem in between. So E1 is P(x_sem).
In x.signal, if x_count > 0, a process is waiting on x_sem. The signaller must wake it with V(x_sem), which is E2. Then P(next) makes the signaller wait until the woken process leaves the monitor or waits again. The signaller counts itself in next_count, and the exit code wakes it with V(next).
The other options are wrong:
(A) E2 = V(next) wakes a process waiting on next, not the one waiting on x_sem.
(B) E1 = V(next) does not make the waiter sleep on x_sem, and E2 = P(x_sem) would block the signaller instead of waking a waiter.
(C) E1 = P(next) makes the waiter sleep on next, not on x_sem.
Answer: (D)
In x.signal, if x_count > 0, a process is waiting on x_sem. The signaller must wake it with V(x_sem), which is E2. Then P(next) makes the signaller wait until the woken process leaves the monitor or waits again. The signaller counts itself in next_count, and the exit code wakes it with V(next).
The other options are wrong:
(A) E2 = V(next) wakes a process waiting on next, not the one waiting on x_sem.
(B) E1 = V(next) does not make the waiter sleep on x_sem, and E2 = P(x_sem) would block the signaller instead of waking a waiter.
(C) E1 = P(next) makes the waiter sleep on next, not on x_sem.
Answer: (D)
Consider the solution to the bounded buffer producer/consumer problem by using general semaphores S, F, and E. The semaphore S is the mutual exclusion semaphore initialized to 1. The semaphore F corresponds to the number of free slots in the buffer and is initialized to N. The semaphore E corresponds to the number of elements in the buffer and is initialized to 0.
Which of the following interchange operations may result in a deadlock?
I. Interchanging Wait (F) and Wait (S) in the Producer process
II. Interchanging Signal (S) and Signal (F) in the Consumer process
Which of the following interchange operations may result in a deadlock?
I. Interchanging Wait (F) and Wait (S) in the Producer process
II. Interchanging Signal (S) and Signal (F) in the Consumer process
Answer: (A)
I. Interchanging and in the Producer. Take a full buffer, so . The producer does first and succeeds, so it holds . Then it blocks on . The consumer can free a slot only after , but the producer holds and the consumer waits for it. Neither can move, so a deadlock can happen.
II. Interchanging and in the Consumer. A Signal never blocks, so changing the order of two Signals cannot cause a deadlock. It only changes which waiting process wakes first. A producer woken by may have to wait briefly for , and the consumer releases next. So no deadlock.
Answer: (A)
II. Interchanging and in the Consumer. A Signal never blocks, so changing the order of two Signals cannot cause a deadlock. It only changes which waiting process wakes first. A producer woken by may have to wait briefly for , and the consumer releases next. So no deadlock.
Answer: (A)
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems