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?

Think of this system as two independent mechanisms running side by side: a 'lock' mechanism built from semaphore \(A\), and a 'baton-passing' mechanism built from semaphore \(B\).
The lock mechanism (A): Since \(A\) starts at 1, only one process can be between Wait(A) and Signal(A) at a time. The three processes therefore take turns entering this locked region in some order - first, second, third. Each turn, the process prints a star. The shared counter \(X\) goes 0 to 1 to 2 to 3 across these three turns in strict order, so it is always the process taking the SECOND turn that sees \(X=2\) and, while still holding the lock, prints a dollar sign right after its star and also signals \(B\) for the very first time. Because this happens inside one indivisible locked turn, no other process can squeeze a character between that star and that dollar. The process taking the third turn only ever prints a plain star, and it physically cannot take its turn until the second-turn process has released the lock - which only happens after the dollar was printed. So among the four symbols coming from the lock (star, star, dollar, star), the order star-star-dollar-star is unavoidable; dollar can never slide past the third star.
The baton mechanism (B): \(B\) starts at 0 and only becomes available once the second-turn process signals it (right after printing the dollar). From then on, each process that grabs \(B\) prints a hash and passes \(B\) along by signalling it again, so the three hashes always come out one at a time, never simultaneously, and never before the dollar. However, since releasing the lock (finishing a turn on \(A\)) and picking up the baton (using \(B\)) are handled by different, unsynchronized process actions, the scheduler is free to let the hash-chain run partly or fully before the third star is printed, or interleave the third star anywhere inside that chain.
Testing the four candidate outputs:
'**$*###' - fixed prefix intact, third star slotted in before the hash chain starts. Possible.
'**$#*##' - fixed prefix intact, third star slotted in after just one hash. Possible.
'**$##*#' - fixed prefix intact, third star slotted in after two hashes. Possible.
'***$###' - this would require the dollar to appear only after all three stars, which directly contradicts the fact that the dollar-producing turn must finish (lock released) before the third turn can even begin. Not possible.
Conclusion: options showing '**$*###', '**$#*##' and '**$##*#' are all valid execution outcomes, while '***$###' can never happen.
Consider the following threads, T1, T2, and T3 executing on a single processor, synchronized using three binary semaphore variables, S1, S2, and S3, operated upon using standard wait() and signal(). The threads can be context switched in any order and at any time.

Consider the following pseudocode, where S is a semaphore initialized to 5 in line#2 and counter is a shared variable initialized to 0 in line #1. Assume that the increment operation in line#7 is not atomic.
1. int counter = 0;
2. Semaphore S = init(5);
3. void parop(void)
4. {
5. wait(S);
6. wait(S);
7. counter++;
8. signal(S);
9. signal(S);
10. } If five threads execute the function parop concurrently, which of the following program behavior(s) is/are possible?