Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between binary semaphore and mutex

Is there any difference between a binary semaphore and mutex or are they essentially the same?

like image 627
Nitin Avatar asked Sep 15 '08 13:09

Nitin


People also ask

What is the difference between semaphore and binary semaphore?

Using both the semaphore, a process is able to enter the critical section, so a progress is made. A Binary Semaphore is a semaphore whose integer value range over 0 and 1. A counting semaphore is a semaphore that has multiple values of the counter. The value can range over an unrestricted domain.

Why binary semaphore is called mutex?

Multiple number of threads can acquire binary semaphore at a time concurrently. Binary semaphore have no ownership. There is ownership associated with mutex because only owner can release the lock.

What is the difference between a mutex and a semaphore which one would you use to protect access to an increment operation?

A mutex is used when only one thread or process is allowed to access a resource and a semaphore is used when only a certain set limit of threads or processes can access the shared resource. Essentially a mutex is a semaphore where the limit is set to 1.

What is a binary semaphore?

A binary semaphore is restricted to values of zero or one, while a counting semaphore can assume any nonnegative integer value. A binary semaphore can be used to control access to a single resource. In particular, it can be used to enforce mutual exclusion for a critical section in user code.


2 Answers

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:

   - SemTake   - Critical Section   - SemGive

Here is a simple example:

   Thread A                     Thread B    Take Mutex      access data      ...                        Take Mutex  <== Will block      ...    Give Mutex                     access data  <== Unblocks                                   ...                                 Give Mutex 

Binary Semaphore
Binary Semaphore address a totally different question:

  • Task B is pended waiting for something to happen (a sensor being tripped for example).
  • Sensor Trips and an Interrupt Service Routine runs. It needs to notify a task of the trip.
  • Task B should run and take appropriate actions for the sensor trip. Then go back to waiting.
    Task A                      Task B    ...                         Take BinSemaphore   <== wait for something    Do Something Noteworthy    Give BinSemaphore           do something    <== unblocks 

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.

like image 57
Benoit Avatar answered Sep 28 '22 03:09

Benoit


  • A mutex can be released only by the thread that had acquired it.
  • A binary semaphore can be signaled by any thread (or process).

so semaphores are more suitable for some synchronization problems like producer-consumer.

On Windows, binary semaphores are more like event objects than mutexes.

like image 37
Mladen Janković Avatar answered Sep 28 '22 03:09

Mladen Janković