Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  The atomic fetch-and-set x, y instruction unc... Start Learning for Free
The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphore
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?
  • a)
    The implementation may not work if context switching is disabled in P.
  • b)
    Instead of using fetch-and-set, a pair of normal load/store can be used
  • c)
    The implementation of V is wrong
  • d)
    The code does not implement a binary semaphore
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
The atomic fetch-and-set x, y instruction unconditionally sets the mem...
Let us talk about the operation P(). It stores the value of s in x, then it fetches the old value of x, stores it in y and sets x as 1. The while loop of a process will continue forever if some other process doesn't execute V() and sets the value of s as 0. If context switching is disabled in P, the while loop will run forever as no other process will be able to execute V().
View all questions of this test
Most Upvoted Answer
The atomic fetch-and-set x, y instruction unconditionally sets the mem...
&(s->value);
do {
fetch_and_set(x, 1, y);
} while (y);
}

void V (binary_semaphore *s) {
s->value = 0;
}

In this implementation, the P function first gets the address of the value field of the binary semaphore using the "&" operator. Then, it uses a do-while loop to repeatedly call the fetch_and_set function with arguments x (the address of the value field), 1 (the new value to be set), and y (a variable to store the old value). The fetch_and_set function atomically sets the value field to 1 and returns the old value. The loop continues as long as the old value (stored in y) is not 0, indicating that the semaphore was already locked. Once the loop exits, the P function has successfully acquired the semaphore.

The V function simply sets the value field of the binary semaphore to 0, indicating that it is now available for other processes to acquire.

Overall, this implementation ensures that only one process can acquire the binary semaphore at a time, preventing multiple processes from accessing a shared resource simultaneously.
Explore Courses for Computer Science Engineering (CSE) exam

Similar Computer Science Engineering (CSE) Doubts

Top Courses for Computer Science Engineering (CSE)

The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer?
Question Description
The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? for Computer Science Engineering (CSE) 2024 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer?.
Solutions for The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
Here you can find the meaning of The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer?, a detailed solution for The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? has been provided alongside types of The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphorevoid 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?a)The implementation may not work if context switching is disabled in P.b)Instead of using fetch-and-set, a pair of normal load/store can be usedc)The implementation of V is wrongd)The code does not implement a binary semaphoreCorrect answer is option 'A'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Explore Courses
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