edited by
30,092 views
106 votes
106 votes

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 an 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;
}

ReleaseLock(L){ 
   L = 0;
}

This implementation

  1. fails as $L$ can overflow
  2. fails as $L$ can take on a non-zero value when the lock is actually available
  3. works correctly but may starve some processes
  4. works correctly without starvation
edited by

8 Answers

4 votes
4 votes
AcquireLock(L){
   while (Fetch_And_Add(L,1)) /*preempt every process right here*/
      L = 1;
}

ReleaseLock(L){ 
   L = 0;
}

Then overflow can occur. The range of unsigned int in C is $2^{16}$. This means 65536 processes have to run simultaneously and have to preempt in increasing order of process IDs right at the same point.
The chances of you dying of a bear attack while taking the GATE exam are higher

 

Option A is correct, but is highly, highly, highly unrealistic.


AcquireLock(L){
   while (Fetch_And_Add(L,1)) /* a */
      L = 1;                  /* b */
}

ReleaseLock(L){ 
   L = 0;                    /* c */
}

 

Let there be two processes P1 and P2. P1 executes a. Preempt. P2 executes c. Preempt. P1 executes b.
Now, Lock is available, but since the while loop isn't atomic, P1 put L = 1, when actually it should've been 0.

 

So, B is correct.


Starvation is also clearly possible, depending on which process happens to get the chance to run. But C is wrong because it says "works correctly".


 

1 votes
1 votes

Answer (B)
Take closer look the below while loop.

     while (Fetch_And_Add(L,1))
               L = 1;  // A waiting process can be here just after 
                       // the lock is released, and can make L = 1.

Consider a situation where a process has just released the lock and made L = 0. Let there be one more process waiting for the lock, means executing the AcquireLock() function. Just after the L was made 0, let the waiting processes executed the line L = 1. Now, the lock is available and L = 1. Since L is 1, the waiting process (and any other future coming processes) can not come out of the while loop.

The above problem can be resolved by changing the AcuireLock() to following.

  AcquireLock(L){
         while (Fetch_And_Add(L,1))
         { // Do Nothing }
   }

 

Source: https://www.geeksforgeeks.org/operating-systems-set-17/

0 votes
0 votes
B.

When initially L=0....So when P1 perform AcquireLock function and Lock value is 1.The P2 perform AcquireLock function but L=1....So P2 wait for P1 release lock.

After Some time P1 perform ReleaseLock function but when P1 store the value L=0,it preempted.So CS is free but value of L=1 and also lock is available but P1 is preempted and L=1 so no process can enter in CS.

So,We can say that solution is fail as L can take on a non-zero value when the lock is actually available.

0 votes
0 votes

Consider the below scenario for option B

P1P2
Acquire lock = 0 and execute   while loop, while loop breaks and process goes to Critical Section
Preempts--->
Process P2 comes to action and sees the value of Lock as 1. 
Executes while loop setting value of L to 2. Since while condition is true and there is no semicolon after it, it will now execute next statement.
As it is about to move value 1 to Lock, before moving the value
<<<-------Preempts
Process P1 comes out of critical section and makes value of Lock = 0 , now lock is available.
Preempts------>>>>
Now lock is available, but now process P2 executes L=1 and sets lock to 1.
Now this while loop is stuck.
 Now no other process can set the value of lock to 0 and it will go in Infinite loop

Answer:

Related questions

47 votes
47 votes
6 answers
4
Arjun asked Sep 26, 2014
19,655 views
Consider the $3$ processes, $P1, P2$ and $P3$ shown in the table. $$\small \begin{array}{|c|c|c|} \hline \textbf{Process} & \textbf{Arrival Time} & \textbf{Time Units Req...