© Thomas Kunz 2000
SCE 574
109
Mutual Exclusion Example
äConsider two processors that compete for access to a shared memory location
äSemaphore is a memory location containing a flag bit that is set if either CPU is using the managed resource
äOnly one CPU is given access at a time
äBUSY: CMP SEMPHR, 80H;Is semaphore set by other CPU
ä JE BUSY          ;Hang in loop if it is
ä MOV SEMPHR,80H ;Else set the set the semaphore
ä ....          ;Do whatever
ä MOV SEMPHR,0      ;clear the semaphore
äProblem
äBoth processors could pass JE and set the semaphore!
äNeed to guarantee mutual exclusion!
äNeed a single indivisible instruction that tests and sets the semaphore
äMust prevent doing test and set with more than one memory access
äIf 2 accesses are required, other processors can sneak between the test and set (If they can, they eventually will)