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); // get the memory location of s
do {
atomic_fetch_and_set(x, 1, &y); // set x to 1 and get the old value in y
} while (y == 1); // keep trying until x was 0 before we set it to 1
}

void V (binary_semaphore *s) {
atomic_fetch_and_set(&(s->value), 0, NULL); // set s to 0 and discard old value
}

This implementation ensures that only one process can enter the critical section at a time, as the P function will block until the semaphore value is 0 before setting it to 1. The V function simply sets the semaphore value back to 0, allowing another process to enter the critical section. The use of atomic_fetch_and_set ensures that these operations are performed atomically, without allowing any other process to access the semaphore value in between.
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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