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

The atomic fetch-and-set x, y instruction unconditionally sets the memory lcoation x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location and x. Consider the follwoing implementation of P and V functions on a binary semaphore S.
void P(binary_semaphore *S) {
unsigned y;
unsinged *x=&(S value);
do {
fetch-and-set x,y;
} while (y);
}
void V (binary_semphore *){
S value =0;
}
Which one of the following is true?

A
The code does not implement a binary semaphore
No worries! We‘ve got your back. Try BYJU‘S free classes today!
B
Instead of using fetch-and-set, a pair normal load/store can be used
No worries! We‘ve got your back. Try BYJU‘S free classes today!
C
The implementation of V is wrong
No worries! We‘ve got your back. Try BYJU‘S free classes today!
D
The implementatioin may not work if context switching is disabled in P
Right on! Give the BNAT exam to get a 100% scholarship for BYJUS courses
Open in App
Solution

The correct option is D The implementatioin may not work if context switching is disabled in P
The P and V function is as follows:
  1. void P(binary-semaphore*S){
  2. unsigned y;
  3. unsigned *x=&(S value);
  4. do {
  5. fetch-and-set x,y;
  6. } while (y);
  7. }
  8. void V(binary-semaphore *S){
  9. S=0;
  10. }
fetch-and-set instruciton always sets the memory location x =1 and fetches the old value of x into y. The binary semphore * S takes only two values either 0 and 1. When we initiliaze S = 0 then in statement 3 his value will be start at location x and fetch-and-set instruction changes the value of x =0 to x =1 and y beocmes 0. If there are more thean two processes and context switching between processes is disabled in P then this implementation doesn't work properly and can't synchronize the processes.

flag
Suggest Corrections
thumbs-up
0
similar_icon
Similar questions
Join BYJU'S Learning Program
similar_icon
Related Videos
thumbnail
lock
Semaphores 2
OTHER
Watch in App
Join BYJU'S Learning Program
CrossIcon