GATE Exam  >  GATE Questions  >  Fetch_And_Add(X,i) is an atomic Read-Modify-W... Start Learning for Free
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
  • a)
    fails as L can overflow
  • b)
    fails as L can take on a non-zero value when the lock is actually available
  • c)
    works correctly but may starve some processes
  • d)
    works correctly without starvation
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that rea...
1. Acquire lock (L) {
2. While (Fetch_And_Add(L, 1))
3. L = 1.
}
4. Release Lock (L) {
5. L = 0;
6. }
Let P and Q be two concurrent processes in the system currently executing as follows
P executes 1,2,3 then Q executes 1 and 2 then P executes 4,5,6 then L=0 now Q executes 3
by which L will be set to 1 and thereafter no process can set
L to zero, by which all the processes could starve.
View all questions of this test
Most Upvoted Answer
Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that rea...
Explanation:

The given code implements a busy-wait lock using Fetch_And_Add(X, i) operation. Let's analyze the code and understand the behavior of the lock.

AcquireLock(L):
- The AcquireLock function uses a while loop to repeatedly check the value of the lock variable L.
- Inside the while loop, the Fetch_And_Add(L, 1) operation is used to atomically increment the value of L and return the old value.
- If the old value of L is non-zero, it means that the lock is already acquired by another process, so the loop continues.
- If the old value of L is zero, it means that the lock is available and the current process acquires the lock by setting L to 1 and exits the loop.

ReleaseLock(L):
- The ReleaseLock function simply sets the value of L to 0, indicating that the lock is released and available for other processes.

Explanation of the given answer (Option B):
The given code fails as L can take on a non-zero value when the lock is actually available. Let's understand why this is the case.

- Consider a scenario where two processes P1 and P2 are trying to acquire the lock simultaneously.
- Initially, the value of L is 0, indicating that the lock is available.
- Both P1 and P2 start executing the AcquireLock function.
- Let's say P1 executes the Fetch_And_Add(L, 1) operation first and gets the old value of L as 0.
- P1 sets L to 1 and acquires the lock.
- Now, P2 executes the Fetch_And_Add(L, 1) operation and gets the old value of L as 1 (as set by P1).
- P2 enters the while loop since the old value of L is non-zero.
- P2 sets L to 1, but the lock is already acquired by P1.
- P2 continues to execute the while loop, repeatedly setting L to 1.
- As a result, P2 is stuck in a busy-wait loop, unable to acquire the lock even though it is actually available.

Therefore, the given code fails as L can take on a non-zero value when the lock is actually available, leading to incorrect behavior and potential starvation of processes.
Explore Courses for GATE exam

Similar GATE Doubts

Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer?
Question Description
Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer?.
Solutions for Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer?, a detailed solution for Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? has been provided alongside types of Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value ofmemory location X, increments it by the value i, and returns the old value of X. It is used in thepseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variableinitialized to 0. The value of 0 corresponds to lock being available, while any non-zero valuecorresponds to the lock being not available.AcquireLock(L){while (Fetch_And_Add(L,1))L = 1;}ReleaseLock(L){L = 0;}This implementationa)fails as L can overflowb)fails as L can take on a non-zero value when the lock is actually availablec)works correctly but may starve some processesd)works correctly without starvationCorrect answer is option 'B'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev