Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the…

GATE · 2012 · CS · Computer Science & IT

Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value of memory location X, increments it by the value i, and returns the old value of X. It is used in the pseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variable initialized to 0. The value of 0 corresponds to lock being available, while any non-zero value corresponds to the lock being not available.

AcquireLock(L){
    while (Fetch_And_Add(L,1))
        L = 1;
}

ReleaseLock(L){
    L = 0;
}

This implementation

  1. A.

    fails as L can overflow

  2. B.

    fails as L can take on a non-zero value when the lock is actually available

  3. C.

    works correctly but may starve some processes

  4. D.

    works correctly without starvation

Attempted by 268 students.

Sign up free to check your answer

Sign up free

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…