Option B TSL instruction
Semaphore is a OS implementation of synchronization,
TestAndSet is a special machine instruction to provided hardware solution to critical section problem.
The definition of the TestAndSet () instruction.
boolean TestAndSet(boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv;
}
Mutual-exclusion implementation with TestAndSet ()
do {
while (TestAndSet(&lock)) ;
//do nothing
//critical section
lock = FALSE;
// remainder section
} while (TRUE);
The important characteristic of this instruction is that it is executed atomically. Thus, if two TestAndSet () instructions are executed simultaneously (each on a different CPU), they will be executed sequentially in some arbitrary order. If the machine supports the TestAndSet () instruction, then we can implement mutual exclusion by declaring a Boolean variable lock, initialized to false.