Consider three processes P1, P2, and P3 running identical code, as shown in…
2026
Consider three processes P1, P2, and P3 running identical code, as shown in the pseudocode below. A and B are two binary semaphores initialized to 1 and 0, respectively. X is a shared variable initialized to 0. Each line in the pseudocode is executed atomically
Pseudocode of P1, P2, and P3
Wait(A);
Print(*);
X = X + 1;
If (X == 2)
{
Print($);
Signal(B);
}
Signal(A);
Wait(B);
Print(#);
Signal(B);
Assume that any of the three processes can start to execute first and context switching can happen between these processes at any arbitrary time and in any arbitrary order.
Which of the following patterns is/are possible to be generated as an outcome of the execution of these three processes?
- A.
**$*###
- B.
**$#*##
- C.
**$##*#
- D.
***$###
Attempted by 8 students.
Show answer & explanation
Correct answer: A, B, C
Now check each option
A: $###
✔ Possible
P1 →
*(X=1)P2 →
*(X=2) →$P3 runs later → prints
*then ###
**B: $*###
✔ Possible
P1, P2 →
**→$P3 →
*then ###
**C: $##*#
✔ Possible (this is the tricky one)
Sequence:
P1 →
*P2 →
*→$P2 immediately continues → prints
#P2 signals → another
#P3 (delayed earlier) now runs → prints
*last
#
So * can appear between #
**D: *$###
❌ NOT possible
Why?
For this to happen:
3 stars must happen before
$
But
$occurs exactly at X == 2
So it must appear immediately after 2nd star, not after 3rd