top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

what is the difference between mutex and binary semaphore ?

+4 votes
7,341 views
what is the difference between mutex and binary semaphore ?
posted Sep 8, 2013 by Sony Mohanty

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. in Summary only two state u are having the mutex or not having the mutex.

Where as A semaphore is a generalized mutex. A semaphore can be held by the multiple consumer (say process/thread) at the same time where as producer may need to wait for the release of it from all of them.
this explanation holds good for mutex and counting semaphore .. but my main doubt is regarding the use of binary semaphores vs mutex
Sorry I missed the word binary, someone has already explained this which is good explanation.

3 Answers

+5 votes

1) A binary semaphore can be a Mutex but a Mutex can never be binary semaphore. This simply means that a binary semaphore can be used as Mutex, but a Mutex can never exhibit the functionality of binary semaphore.

2) No one owns binary semaphores, whereas Mutex are owned and the owner is held responsible for them. This is an important distinction from a debugging perspective.

3) In case the of Mutex, the thread that owns the Mutex is responsible for freeing it. However, in the case of binary semaphores, this condition is not required.
Any other thread can signal to free the binary semaphore by using the sem_post()function.

4) Another difference that would matter to developers is that binary semaphores are system-wide and
remain in the form of files on the filesystem, unless otherwise cleaned up. Mutex are process-wide and get cleaned up automatically when a process exits.

5) The nature of binary semaphores makes it possible to use them in synchronizing related and unrelated process, as well as between threads. Mutex can be used
only in synchronizing between threads and at most between related processes (the pthread implementation of the latest kernel comes with a feature
that allows Mutex to be used between related process).

6) According to the kernel documentation, Mutex are lighter when compared to binary semaphores. What this means is that a program with binary semaphore usage has
a higher memory footprint when compared to a program having Mutex.

7) From a usage perspective, Mutex has simpler semantics when compared to binary semaphores.

Mutex can not be used for unrelated process.

Semaphore is signaling mechanism
mutex is locking mechanism

answer Oct 14, 2013 by Vikas Upadhyay
+2 votes

Mutex
Mutex 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.

answer Sep 8, 2013 by anonymous
I am sorry that i am not very clear with the explanation,
are you intending the following
1. in case of mutex, the process holding the mutex will return error to the requesting process.
2.In case of binary semaphore will the requesting process be queued to be invoked later once the task holding the semaphore releases it.
yes precisely.
+1 vote

Both used for thread synchronization. Mutex can be used between only two threads. Suppose in your program there is one producer and one consumer. where writer thread takes lock on mutex then it write and unlock. that time other can't be proceed. Binary semaphore is also used for thread synchronization. Semaphore is generally an unsigned integer. Binary semaphore has value (either 0 and 1) . suppose in your program there are 4 threads. out of 4 threads one thread writes data on the shared memory/data structure and others 3 read the data. In that case binary semaphore is useful since there is no locking system between read threads. Write thread decrement the value and do the write operation and again increment the semaphore value. while writing other 3 thread will check the binary semaphore value which was 0 so they will not perform read operation within that period.

answer Sep 8, 2013 by Vimal Kumar Mishra
Similar Questions
+5 votes

Which one is better for which situation ??

0 votes

Please explain with real life example?

+7 votes

What is the difference in behavior between mutex in linux and mutex in VxWorks ??

+2 votes

Here my doubt is about acquire mutex lock.

Here pthread_mutex_t lock ; is also a global variable shared to threads. Accessing of this global variable (lock) will it be shame as accessing of other global variables ? If same, then don't we face same problem what we will face for other global variables ? if not ,how this is discriminated from other global variables ?

...