CameraIcon
CameraIcon
SearchIcon
MyQuestionIcon
MyQuestionIcon
1
You visited us 1 times! Enjoying our articles? Unlock Full Access!
Question

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 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;
}
Release Lock (L){
L=0;
}
This implementation

A
fails as L can take on a non-zero value when the lock is actually available
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
B
works correctly but may starve some processes
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
fails as L can overflow
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
works correctly without starvation
No worries! We‘ve got your back. Try BYJU‘S free classes today!
Open in App
Solution

The correct option is A fails as L can take on a non-zero value when the lock is actually available
Assume P1 executes until while condition and preempts before executing L = 1. Now P2 executes all statements, hence L = 0. Then P1 without checking L it makes L = 1 by executing the statement where it was preempted.
It takes a non-zero value (L=1) when the lock is actually available (L=0)

flag
Suggest Corrections
thumbs-up
0
similar_icon
Similar questions
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Closure Property of Rational Numbers
MATHEMATICS
Watch in App
Join BYJU'S Learning Program
CrossIcon