Semaphores
0x10. WHAT
0x20. HOW
ALG1. spin semaphore
P(S):
while (S <= 0)
; // busy wait
S--
V(S):
S++ALG2. blocking semaphore
P(S):
acquire(lock)
S--
if (S < 0):
add this thread to queue
sleep()
release(lock)
V(S):
acquire(lock)
S++
if (S <= 0):
wakeup one thread from queue
release(lock)0xF0. WHY not
CONS
- difficult to code
- difficult to prove correctness
- errors are not reproducible
- errors are observed rarely
- requires voluntary cooperation
- single misuse affect entire system