GATE - 2006 | OS | The atomic fetch-and-set x, y instruction unconditionally sets the

GATE - 2006 | OS | The atomic fetch-and-set x, y instruction unconditionally sets the
Posted on 18-02-2022

GATE - 2006 [Operating System]

Question:

The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x n 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?    

A

The implementation may not work if context switching is disabled in P

B

Instead of using fetch-and-set, a pair of normal load/store can be used

C

The implementation of V is wrong

D

The code does not implement a binary semaphore

Solution:

Option (A) is Correct.

A) This is correct because implementation might 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.


B) If we use normal load and store instead of Fetch and Set, then there can be chance that more than one process sees S.value as 0 and then mutual exclusion will not be satisfied. So wrong.


C) Here we are setting S→value to 0, which is correct. This option thats why wrong.


D) Only one process can be in critical section at any time. So this option is wrong.

Thank You