Semaphores (cont.)
Three problems with the previous slide’s wait() and signal():
- busy waiting is inefficient
- doesn’t guarantee bounded waiting
- “++” and “--” operations aren’t necessarily atomic!
Solution: use system calls semget() and semop() (… see next slide)
The following pseudocode protects a critical section:
wait( &s );
/* critical section */
signal( &s );
/* remainder section */
What happens if S is initially 0? What happens if S is initially 8?