Suppose we want to synchronize two concurrent processes and using binary semaphores and . The code for the processes and is shown below.
| Process P: | Process Q: |
|---|---|
while(1){
W:
print '0';
print '0';
X:
}
|
while(1){
Y:
print '1';
print '1';
Z:
}
|
Synchronization statements can be inserted only at points and
Which of the following will always lead to an output staring with ?
View worked solution
Answer: B
To obtain the output sequence 001100110011..., we need strict alternation between Process (which prints 00) and Process (which prints 11), starting with Process .
Analyzing Option B:
- Initial State: , .
- Step 1: Process can proceed because .
- At , decrements to .
- It prints
00. - At , increments to .
- Step 2: Now and .
- If tries to run again, it blocks at because .
- Process can now proceed because .
- At , decrements to .
- It prints
11. - At , increments to .
- Step 3: Now and .
- If tries to run again, it blocks at because .
- Process can run again.
This creates a strict alternating sequence: , which guarantees the output 001100110011....
Eliminating Other Options:
- Option A: Since and are independent and both initialized to , can run repeatedly without letting run, or vice versa, leading to outputs like
000000.... - Option C: Since both and are initially , Process could execute first, producing an output starting with
11...instead of00.... - Option D: Process only signals itself ( at ) and Process only signals itself ( at ). Since starts at , will block forever, and will run infinitely, producing
000000....
Free practice · Your answers on this page are not saved.
