When only one process can access a shared resource at a time (mutual exclusion), we need a semaphore with only two states: locked and unlocked.
A binary semaphore can only take values 0 (locked) and 1 (unlocked), used for mutual exclusion of critical sections.
- Initialize semaphore to 1 (unlocked)
- Process enters critical section:
wait(S)→ S becomes 0 (locked) - Other processes calling
wait(S)block (S becomes negative) - Process exits:
signal(S)→ S becomes 1 (unlocked), wake up one waiter
- Only two values: 0 (locked) and 1 (unlocked)
- Provides mutual exclusion (mutex)
- Initialized to 1 for mutex use
- Can be used as a lock/mutex
- Built from: Semaphore, Critical Section
- Builds into: Mutex, Race Condition
- Related: Counting Semaphore
- Contrasts with: Counting Semaphore (one bit vs N values)
- Busy-waiting implementation wastes CPU (better to block/sleep)
- Must be acquired and released by same process
- Forgetting signal() causes deadlock