top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

what is the difference between mutex and binary semaphore ?

+4 votes
posted Sep 8, 2013 by Sony Mohanty

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social 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 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 ??

+3 votes

As far as I know boot loader is the one who loads OS from ROM to RAM, then why Boot Loader is different as per OS.
And what is boot manager? Is Boot Manager is different as per OS?

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.