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 21–25 of 41
The following program consists of 3 concurrent processes and 3 binary semaphores. The semaphores are initialized as S0=1, S1=0, S2=0.
How many times will process P0 print '0'?
| Process P0 | Process P1 | Process P2 |
| | |
How many times will process P0 print '0'?
Answer: (A)
P0 prints '0' once at first, because S0 = 1. Then it releases S1 and S2. After that, P1 and P2 each release S0 one time.
- Case 1: both releases happen before P0 waits again. Binary S0 stays at 1 P0 prints one more time (2 times in all).
- Case 2: P0 takes the first release before the second one comes P0 prints two more times (3 times in all).
So P0 prints at least twice.
Answer: At least twice
- Case 1: both releases happen before P0 waits again. Binary S0 stays at 1 P0 prints one more time (2 times in all).
- Case 2: P0 takes the first release before the second one comes P0 prints two more times (3 times in all).
So P0 prints at least twice.
Answer: At least twice
Consider the methods used by processes P1 and P2 for accessing their critical sections whenever needed, as given below. The initial values of shared boolean variables S1 and S2 are randomly assigned.
Which one of the following statements describes the properties achieved?
| Method used by P1 | Method used by P2 |
| |
Which one of the following statements describes the properties achieved?
Answer: (A)
P1 goes into its critical section only when S1 S2. After that, it makes the two values equal. P2 goes in only when S1 = S2. After that, it makes the two values unequal.
- P1 and P2 can never be inside together so mutual exclusion holds.
- The two must take turns strictly. If it is P1's turn but P1 does not want to enter, P2 waits forever so there is no progress.
Answer: Mutual exclusion but not progress
- P1 and P2 can never be inside together so mutual exclusion holds.
- The two must take turns strictly. If it is P1's turn but P1 does not want to enter, P2 waits forever so there is no progress.
Answer: Mutual exclusion but not progress
The enter_CS() and leave_CS() functions to implement critical section of a
process are realized using test-and-set instruction as follows:
Now consider the following statements:
I. The above solution to CS problem is deadlock-free
II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV More than one process can enter CS at the same time.
Which of the above statements is TRUE?
void enter_CS(x)
{
while test-and-set(x) ;
}
void leave_CS(x)
{
x=0;
}
In the above solution, x is a memory location associated with the CS and is nitialized to 0.Now consider the following statements:
I. The above solution to CS problem is deadlock-free
II. The solution is starvation free.
III. The processes enter CS in FIFO order.
IV More than one process can enter CS at the same time.
Which of the above statements is TRUE?
Answer: (A)
x = 0 means the critical section (CS) is free. A process calls test-and-set, which returns the old value of x and sets x to 1 in one indivisible step. It enters when it sees 0 and spins while it sees 1.
- I is true. If no process is in the CS, x is 0, so some waiting process gets 0 and enters. A process inside leaves and sets x = 0. So the processes cannot all be stuck waiting: the solution is deadlock-free.
- II is false. Nothing decides which spinning process wins when x becomes 0, so one unlucky process may lose again and again (starvation).
- III is false. There is no queue, so the processes do not enter in the order they came.
- IV is false. Test-and-set is atomic, so only one process can see 0, and mutual exclusion holds.
So only I is true.
Answer: (A)
- I is true. If no process is in the CS, x is 0, so some waiting process gets 0 and enters. A process inside leaves and sets x = 0. So the processes cannot all be stuck waiting: the solution is deadlock-free.
- II is false. Nothing decides which spinning process wins when x becomes 0, so one unlucky process may lose again and again (starvation).
- III is false. There is no queue, so the processes do not enter in the order they came.
- IV is false. Test-and-set is atomic, so only one process can see 0, and mutual exclusion holds.
So only I is true.
Answer: (A)
The following is a code with two threads, producer and consumer, that can run in parallel. Further, S and Q are binary semaphores quipped with the standard P and V operations.
Which of the following is TRUE about the program above?
semaphore S = 1, Q = 0;
integer x;
producer: consumer:
while (true) do while (true) do
P(S); P(Q);
x = produce (); consume (x);
V(Q); V(S);
done done Which of the following is TRUE about the program above?
Answer: (D)
Start with S = 1 and Q = 0. The producer does P(S), produces x, then V(Q). The consumer does P(Q), consumes x, then V(S).
The producer can produce only after it passes P(S). S becomes 1 again only when the consumer finishes consuming and does V(S). The consumer can consume only after the producer does V(Q). So the two strictly alternate: produce, consume, produce, consume. At any time exactly one of the two can go ahead (S = 1 and Q = 0, or S = 0 and Q = 1), or it is already inside its section.
- (A) No deadlock: both are never blocked at the same time, as shown above.
- (B) No starvation: they take turns, so each waits only for the other's one step.
- (C) No item is lost: the producer cannot write a new x until the consumer has consumed the old one.
- (D) True: the value in x is always consumed before the producer makes a new value.
Answer: (D)
The producer can produce only after it passes P(S). S becomes 1 again only when the consumer finishes consuming and does V(S). The consumer can consume only after the producer does V(Q). So the two strictly alternate: produce, consume, produce, consume. At any time exactly one of the two can go ahead (S = 1 and Q = 0, or S = 0 and Q = 1), or it is already inside its section.
- (A) No deadlock: both are never blocked at the same time, as shown above.
- (B) No starvation: they take turns, so each waits only for the other's one step.
- (C) No item is lost: the producer cannot write a new x until the consumer has consumed the old one.
- (D) True: the value in x is always consumed before the producer makes a new value.
Answer: (D)
The P and V operations on counting semaphores, where s is a counting semaphore, are defined as follows:
P(s) : s = s - 1;
if (s < 0) then wait;
V(s) : s = s + 1;
if (s <= 0) then wakeup a process waiting on s; Assume that and the wait and signal operations on binary semaphores are provided. Two binary semaphores and are used to implement the semaphore operations P(s) and V(s) as follows: P(s) : Pb(xb);
s = s - 1;
if (s < 0) {
Vb(xb) ;
Pb(Yb) ;
}
else Vb(xb);
V(s) : Pb(xb) ;
s = s + 1;
if (s <= 0) Vb(Yb) ;
Vb(xb) ; The initial values of xb and yb are respectivelyAnswer: (C)
Here protects the updates of (it works like a lock), and is where a process waits when .
xb must be 1. Both P(s) and V(s) start with . If started at 0, every process would block there and none could ever run, so (A) and (B) are out.
yb must be 0. Take and four processes. P1 makes and P2 makes ; neither is below 0, so neither waits. P3 makes , so it calls and then . This must block P3. With it blocks. With it would pass at once, and a third process would enter although the counting semaphore allows only two. Later, each V(s) with calls and wakes exactly one waiting process.
So the initial values are and .
Answer: (C)
xb must be 1. Both P(s) and V(s) start with . If started at 0, every process would block there and none could ever run, so (A) and (B) are out.
yb must be 0. Take and four processes. P1 makes and P2 makes ; neither is below 0, so neither waits. P3 makes , so it calls and then . This must block P3. With it blocks. With it would pass at once, and a third process would enter although the counting semaphore allows only two. Later, each V(s) with calls and wakes exactly one waiting process.
So the initial values are and .
Answer: (C)
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems