Semaphore interface
Semaphore is an interface to generally describe synchronization mechanism. This means it can have different implementations.
It Provides A way to block a no. of processes (sleep), A way to unblock / wakeup > 1 or more sleeping process.
Interface
S is a semaphore.
We have 2 functions which interact with it.
Wait(S)
- Conditional if S <= 0, blocks (go to sleep), until it is more than 0 than we release.
- Decrement S
Signal(S) / Up(S) / v(S)
- Increments S
- Wakes up 1 sleeping process if any
- This operation never blocks
Reminder: The above specifies the behaviour, not the implementation.
Simple implementation
Example scenario
Semaphore state interaction example
Semaphore with critical section example