The atomic fetch-and-set x, y instruction unconditionally sets memory location…

GATE · 2006 · CS

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

  1. A.

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

  2. B.

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

  3. C.

    The implementation of V is wrong

  4. D.

    The code does not implement a binary semaphore

Attempted by 10 students.

Show answer

Correct answer: A

The worked solution is available to enrolled students.

Video solution available to enrolled students.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…