processoperating-systemdata-synchronization

How test-and-set instruction satisfies (or does not satisfy) conditions for critical section approach


I was looking at process synchronization and came across test-and-set instruction

boolean testAndSet (boolean *target)
{  
     boolean rv = *target;
     *target = true;
     return rv;
}

main()
{
      do
      {
          while( testAndSet( &lock ));

          //critical section
          lock = false;

          //remainder section
       }while(true);

 }

This executes atomically (i.e. whenever a function call to testAndSet occurs no interrupt is handled till the function returns).

Now I understand how this eliminates mutual exclusion (since the waiting process gets stuck at the while loop if another process is executing it's critical section). But how does it satisfy Progress condition, and more importantly, how does it not satisfy bounded-buffer condition? Any help would be appreciated..


Solution

  • For progress lets say, PO is in the critical section and P1,P2 and P3 are waiting. As soon as PO leaves, it sets lock to false and just after that the next process exits the while condition and enters the critical section. For bounded waiting, I am not sure of but if lets say, P4 with a high priority comes and repeatedly requests to enter the critical section. Then, P1,P2,P3 will never get the chance to enter the section. Hence, they'll wait infinitely.