-
The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. Consider the following implementation of P and V functions on a binary semaphore S.
void P (binary_semaphore *S) {
unsigned y;
unsigned *x = & (S -> value);
do {
fetch-and-set x, y;
} while (y);
} void V (binary_semaphore *S) {
s - > value = 0;
}
Which one of the following is true?
-
- The implementation may not work, if context switching is disabled in P
- Instead of using fetch-and-set, a pair of normal load/ store can be used
- The implementation of V is wrong
- The code does not implement a binary semaphore
- The implementation may not work, if context switching is disabled in P
Correct Option: A
Let us talk about the operation P(). It stores the value of s in x, then it fetches the old value of x, stores it in y and sets x as 1. The while loop of a process will continue forever if some other process doesn't execute V() and sets the value of s as 0. If context switching is disabled in P, the while loop will run forever as no other process will be able to execute V().
Option (A) :- This is correct because the implementation may not work if context switching is disabled in P, then process which is currently blocked may never give control to the process which might eventually execute V. So Context switching is must.
Option (B) :-If we use normal load & Store instead of Fetch & Set there is good chance that more than one Process sees S. value as O & then mutual exclusion wont be satisfied. So this option is wrong.
Option (C) :- Here we are setting S –> value to O, which is correct. (As in fetch & Set we wait if value of S –> value is 1. So implementation is correct. This option is wrong.
Option (D) :- I don't see why this code does not implement binary semaphore, only one Process can be in critical section here at a time. So this is binary semaphore. Hence Option D is wrong.