edited by
28,060 views
104 votes
104 votes

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 $S$.

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? 

  1. The implementation may not work if context switching is disabled in $P$ 
  2. Instead of using fetch-and –set, a pair of normal load/store can be used 
  3. The implementation of $V$ is wrong
  4. The code does not implement a binary semaphore
edited by

6 Answers

Best answer
106 votes
106 votes
  1. Answer :- This is correct because the implementation may not work if context switching is disabled in $P$ , then process which is currently blocked may never give control to the process which might eventually execute $V$. So Context switching is must !
     
  2. If we use normal load & Store instead of Fetch & Set there is good chance that more than one Process sees S.value as $0$ & then mutual exclusion wont be satisfied. So this option is wrong.
     
  3.  Here we are setting $S \rightarrow $ value to $0$, which is correct. (As in fetch & Set we wait if value of $S \rightarrow $ value is $1$. So implementation is correct. This option is wrong.
     
  4. I don't see why this code does not implement binary semaphore, only one Process can be in critical section here at a time. So this is binary semaphore & Option (D) is wrong
     
edited by
50 votes
50 votes

CASE 1 : If initial value of s=0

then the fetch & set operation will make it 1 & one process will enter into critical section. Now if any other process will try to enter in to CS it will stuck into while loop as y=1.

When the process leave the CS, it will make the value of S to 0 which will give chance to other process to execute CS. Here, the thing to be noted is P() incrementing the value of semaphore & V() decrementing the value of semaphore variable. so this is reverse implementation of P & V. but they are working fine. so Option C & D is wrong.

CASE 2 : If initial value of S= 1

then every process will stuck into while loop & no process will be able to execute CS, so here we require some other process to come & execute v() which require context switching. If context switching is disabled then any process try to enter in CS will stuck into loop. so option A is correct.

Option B : If load & store is used & taking preemption into consideration(these are not atomic operation so preemption may occur) mutual exclusion can be dissatisfied.

 

edited by
12 votes
12 votes

It is kind of TSL instruction. Someone correct me if I am wrong.

The key point of this question is "The atomic fetch-and-set x,y instruction sets the memory location x to 1 and the old value of x into y". It means following (understand the code snippet first) 

CODE SNIPPET:

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;

UNDERSTAND HERE IN FOLLOWING STEPS:

1. Suppose s.value==0 initially. and &(s->value)==100. 

2. unsigned *x=&(s->value) will assign 100 to x.

3.  fetch-and-set x,y;     <- this statement will set value of location stored in x to 1 i.e, s.value=1. (REMEMBER BINARY SEMAPHORE'S STRUCTURE CONSISTS VALUE AND QUEUE FIELD ) and older value of location stored in x will be stored in y i.e, y=0.

I think this is one of the main difficulties of the question for many. Now you can solve the question.

edited by
1 votes
1 votes

Say, old value of x is 0 and not 1, bcz if old value of x=1 then why ques says that fetches old value of x and set x=1.
So, suppose x=0 initially.

Code for P:
----------------------------------------
Line 1: void P (binary_semaphore *s) {
Line 2: unsigned y;
Line 3: unsigned *x = &(s->value);
Line 4: do {
Line 5: fetch-and-set x, y;
Line 6: } while (y);
Line 7: }

Code for V:
----------------------------------------
Line 8: void V (binary_semaphore *s) {
Line 9: S->value = 0;
Line 10: }

So, since given that Context switching disabled in function P, so lines from 1 to 7 will execute without any pre-emption.

So, suppose a process P1 came, execute lines 1 to 5, now at line 5 it set x=1 and fetch y=0(bcz old value of x=0), and now it execute line 6 (while(0))--->false. 
Suppose this process is inside Critical section. 

Now suppose process P2 came, and it fetches value of y=1(bcz when process p1 execute fetch-and-set it sets x=1). So, now process P2 stuck in while loop-> while(1)-----> busy waiting.

Now, suppose P1 process execute its V code and make S.value=0.
Now, control came back to process P2. So now P2 execute line 5.
Now, key thing to note here is that->

fetches the old value of x in y without allowing any intervening access to the memory location x.
bcz, x contains memory location which contains S->value, and since memory access not allowed hence x contains old value which was 1 (when P2 first time executed fetch-and-set and set x=1)
so, it means even there is no one in Crirical section, still P2 is in busy waiting.

So, context switching is must in P.

Answer:

Related questions

8.5k
views
4 answers
42 votes
go_editor asked Apr 24, 2016
8,512 views
Barrier is a synchronization construct where a set of processes synchronizes globally i.e., each process in the set arrives at the barrier and waits for all others to arr...
18.6k
views
3 answers
70 votes
Rucha Shelke asked Sep 26, 2014
18,647 views
Barrier is a synchronization construct where a set of processes synchronizes globally i.e., each process in the set arrives at the barrier and waits for all others to arr...
17.0k
views
2 answers
58 votes
Rucha Shelke asked Sep 26, 2014
16,950 views
Consider the following snapshot of a system running $n$ processes. Process $i$ is holding $x_i$ instances of a resource $R$, $ 1\leq i\leq n$ . Currently, all instances ...
32.9k
views
7 answers
60 votes
Rucha Shelke asked Sep 26, 2014
32,859 views
Consider three processes, all arriving at time zero, with total execution time of $10$, $20$ and $30$ units, respectively. Each process spends the first $\text{20%}$ of e...