You are currently not logged in and your progress will not be saved. Register or Log in

Semaphores

In its simplest form, a semaphore is merely a plain variable that is changed, indicating the status of the common resource. Semaphores can be seen as a resource-sharing mechanism, where you have a finite instance of a resource that you want to manage access for multiple threads. They are more of a signaling mechanism used to control access to a given number of instances of a resource.

Semaphores have the following properties:

  • At initialization, you set an initial count (greater than 0) and a maximum limit.
  • “Give” will increment the semaphore count unless the count is already at the maximum limit, in which case the signal will not increment. “Give” can be done from any thread or ISR.
  • “Take” will decrement the semaphore count unless the semaphore is unavailable (count at zero). Any thread that is trying to take a semaphore that is unavailable needs to wait until some other thread makes it available (by giving the semaphore). “Take” can be done only in threads and not in ISR (since ISRs should not block on anything).
  • There is no ownership of semaphores. This means a semaphore can be taken by one thread and can be given by any thread. It is not necessary that the thread that has taken the semaphore is the one to give it.
  • The thread taking the semaphore is NOT eligible for priority inheritance since the taking thread does not own the semaphore and any other thread can give the semaphore.

The figure below shows the semaphore properties

Semaphore