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.


S is a semaphore.

We have 2 functions which interact with it.


  1. Conditional if S <= 0, blocks (go to sleep), until it is more than 0 than we release.
  2. Decrement S

Signal(S) / Up(S) / v(S)

  1. Increments S
  2. Wakes up 1 sleeping process if any
  3. This operation never blocks

Reminder: The above specifies the behaviour, not the implementation.

Simple implementation

Semaphore data structure

Example scenario

Semaphore state interaction example

Semaphore with critical section example

Types of semaphores

General / counting semaphore

Binary semaphore

Properties of semaphore

Semaphore invariant