AcquireLock(L){
while (Fetch_And_Add(L,1)) /*preempt every process right here*/
L = 1;
}
ReleaseLock(L){
L = 0;
}
Then overflow can occur. The range of unsigned int in C is $2^{16}$. This means 65536 processes have to run simultaneously and have to preempt in increasing order of process IDs right at the same point.
The chances of you dying of a bear attack while taking the GATE exam are higher
Option A is correct, but is highly, highly, highly unrealistic.
AcquireLock(L){
while (Fetch_And_Add(L,1)) /* a */
L = 1; /* b */
}
ReleaseLock(L){
L = 0; /* c */
}
Let there be two processes P1 and P2. P1 executes a. Preempt. P2 executes c. Preempt. P1 executes b.
Now, Lock is available, but since the while loop isn't atomic, P1 put L = 1, when actually it should've been 0.
So, B is correct.
Starvation is also clearly possible, depending on which process happens to get the chance to run. But C is wrong because it says "works correctly".