Why is semaphore called a signalling mechanism ?

Hi ppl :),
I came across at many places[skipping references] that semaphores are a signalling mechanism and mutex is mechanism to ensure only one thread/process executes critical section at any given time. And also that semaphores should not be used in place of mutex.

I have following clarifications to ask

[1] Can't we use binary semaphore interchangeably with mutex? I understand that
mutex are explicitly provided to ensure serial execution of critical
critical section, so we should use mutex. Is there any reason other than
this to not to use binary semaphore in place of mutex?

[2] Most of the places show the use of semaphore to allow 'x' number of
threads to enter into the guarded code section; as 'x+1'th thread tries
to enter, it is blocked. In this scenario, where does signalling part
comes into play? Why is it said that they are used for signalling?

Thanks alot for any help :)
While the words mutex and semaphore can be technically defined in whatever way is deemed useful for a particular language/library/system, as basic concepts they are different.

Mutual exclusion is a goal that you want to achieve: exclusive access to a section of code or an object/file/whatever.

A semaphore (which is another word for "flag") is one of the ways to achieve this goal. But a semaphore can be used for other goals.

When used for a mutex a thread will lock it and when it is done the same thread will unlock it. When used to "signal" that some event has occurred, one thread (the producer) will set it and another thread (the consumer) will unset it.

See:
https://stackoverflow.com/questions/4039899/when-should-we-use-mutex-and-when-should-we-use-semaphore
https://stackoverflow.com/questions/62814/difference-between-binary-semaphore-and-mutex
@dutch, the provided links were really useful. Thanks :)
The use of the term semaphore in computer science is very old and predates multithreading.
It is an analogy to train track semaphores, those arm-like signals, that indicate if a train is clear to engage on a track (same function as a traffic light). Since the dawn of computer sciences, they have been used to regulate access to shared resources: memory, communicaton channel, hard storage, printers, ...
Last edited on
Topic archived. No new replies allowed.