What is a test-and-set instruction? How can it be used to implement mutual exclusion?
Consider using a fragment of psuedo-assembly language aid you explanation.
Answer:
-A test and set instruction is a method of using the lock-variable solution, but with hardware support (instructions are in ASM and called before entering any critical region). Prior to entering the critical region, it copies the lock variable to a register, sets it to 1 if it was 0 (free), acquire the lock and execute.
If not, another process had it. By being called as ASM instructions, it is guaranteed to be atomic:
enter_region:
tsl register, lock
cmp register, 0
b enter_region ; if non-zero the lock was set, so loop!
b critical_region ; else, the lock was 0, so get in there
leave_region:
mov lock, 0 ; store a 0 in the lock
b caller