Is there any difference between a binary semaphore and mutex or are they essentially the same?
Difference between binary semaphore and mutex
binary-semaphoremutexoperating systemsemaphore
Related Topic
- The difference between a process and a thread
- Python – How to safely create a nested directory in Python
- Java – mutex and semaphore in Java ? What is the main difference
- The difference between lock, mutex and semaphore
- Linux – When should we use mutex and when should we use semaphore
- Semaphore vs. Monitors – what’s the difference
Best Answer
They are NOT the same thing. They are used for different purposes!
While both types of semaphores have a full/empty state and use the same API, their usage is very different.
Mutual Exclusion Semaphores
Mutual Exclusion semaphores are used to protect shared resources (data structure, file, etc..).
A Mutex semaphore is "owned" by the task that takes it. If Task B attempts to semGive a mutex currently held by Task A, Task B's call will return an error and fail.
Mutexes always use the following sequence:
Here is a simple example:
Binary Semaphore
Binary Semaphore address a totally different question:
Note that with a binary semaphore, it is OK for B to take the semaphore and A to give it.
Again, a binary semaphore is NOT protecting a resource from access. The act of Giving and Taking a semaphore are fundamentally decoupled.
It typically makes little sense for the same task to so a give and a take on the same binary semaphore.