So I've got this so far to try and wrap my head around the abstract idea of a semaphore:

A semaphore is a variable or abstract datatype (commonly an integer), used to control access to a common resource by multiple processes in a concurrent system.

I'm not sure that this definition fully encompasses the full idea of semaphores.

As well as this, I'm not sure how we could use them to implement mutual exclusion.

Any notes to help me understand this concept is appreciated!

  • 2
    $\begingroup$ That's not a definition. It's a description of purpose that many things fulfill, and it's not the only purpose a semaphore serves. Please look up a precise definition of the exact thing you're working with. $\endgroup$
    – Raphael
    Commented Apr 29, 2017 at 9:39
  • $\begingroup$ Anyway, what have you tried and where did you get stuck? $\endgroup$
    – Raphael
    Commented Apr 29, 2017 at 9:40
  • $\begingroup$ @Raphael I know about the wait and post methods but i'm more interested in figuring out the idea behind using semaphores, rather than actually implementing them $\endgroup$
    – rshah
    Commented Apr 29, 2017 at 9:41
  • 1
    $\begingroup$ If that's the only sentence you read so far, of course you wouldn't have a good handle on it. Did you try google.com/search?q=semaphore instead? There are so many descriptions and so many examples of so many cases in so many languages. I mean, there's even one right over on SO on the first page of results. $\endgroup$
    – Jason C
    Commented Apr 29, 2017 at 14:18

1 Answer 1


A semaphore is a counter, that counts the number of processes that have access to a resource. If a resource can service n processes, n is decremented each time a process accesses the resource, and increments it when the process no longer needs the resource. When n reaches zero, the access of the resource is denied.

A real life example will make things clear: imagine a public toilet with 5 toilets (n=5). At the entrance there is a guard (the semaphore). If some one wants to enter the guard looks at n. if n > 0 the guard let the visitor in and decrements n. If someone comes out n is incremented. Only if n=0 the guard will block the entrance until someone leaves.

Consider a binary semaphore, where n = 1. This semaphore works as a flag which is activated when the resource is in use or not, and can be used to protect a critical section. When a process wants to enter the critical section, it first looks at the ‘flag’ to find out if the critical section is being used by another process. If not, the process enters and raises the flag, preventing the other processes to enter. The flag is unset when the process leaves, and therefore ensures mutual exclusion.

  • 1
    $\begingroup$ @rshah: thanks for the edit. English isn't my first language. $\endgroup$
    – EL Dendo
    Commented Apr 29, 2017 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.