posts - 218, comments - 222, trackbacks - 68

My Links

News




I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net) and born in Bangladesh.
I work for Ocean Informatics Pty Ltd as a Senior Developer - Analyst.
I am also co-founder and core developer of Pageflakes (acquired by LiveUniverse) www.pageflakes.com
and most recently created SmartCodeGenerator

My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET
Smart Code Generator .NET: Usage Overview
Smart Code Generator .NET: Architectural Overview
Smart Code Generator .NET: using with NAnt and Cassini

Archives

Free Programming Language Training

Difference between Mutex and Semaphore

Source: http://koti.mbnet.fi/niclasw/MutexSemaphore.html

Mutex vs. Semaphore, what is the difference?

The Toilet Example  (c) Copyright 2005, Niclas Winquist ;)

Mutex:


Is a key to a toilet. One person can have the key - occupy the toilet - at the time. When finished, the person gives (frees) the key to the next person in the queue.

Officially: "Mutexes are typically used to serialise access to a section of  re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section."
Ref: Symbian Developer Library

(A mutex is really a semaphore with value 1.)

Semaphore:

Is the number of free identical toilet keys. Example, say we have four toilets with identical locks and keys. The semaphore count - the count of keys - is set to 4 at beginning (all four toilets are free), then the count value is decremented as people are coming in. If all toilets are full, ie. there are no free keys left, the semaphore count is 0. Now, when eq. one person leaves the toilet, semaphore is increased to 1 (one free key), and given to the next person in the queue.

Officially: "A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore)."
Ref: Symbian Developer Library

Print | posted on Friday, June 09, 2006 4:59 AM |

Feedback

Gravatar

# re: Difference between Mutex and Semaphore

A mutex is owned by a thread/process. So once a thread locks it, then other threads/processes will either spin or block on the mutex. Whereas, semaphore allows one or more threads/processes to share the resource.

pthread_mutex_lock(&mutex_obj)
global_data;
pthread_mutex_unlock(&mutex_obj)

here, you are locking global data so that only one thread/process can access it. Others will wait in a turnstile forming a Q.
while in semaphore you can define that how many concurrent threads/processes can access that resource at a time.
so if a psuedo code is like this

sema(10) // ten threads/process have the concurrent access.

sema_lock(&sema_obj)
global_data
sema_unlock(&sema_obj)

then max 10 threads/processes will access it.
when one process locks the semaphore the counts decrements by 1. So when the count=0, all the processes are using the data.

So you can see that a binary semaphore is like a mutex.
9/6/2006 2:05 AM | Senthil Kumar N
Gravatar

# re: Difference between Mutex and Semaphore

semaphores are different it is nothing but the cumulative flags for the resources, whenever any resource is busy then the count in the semaphore has been dicremented this will happened till it decrement to zero. the consumers required any resouce wait till the count in the semaphore has been incremented.
10/7/2006 6:40 AM | vishwa
Gravatar

# re: Difference between Mutex and Semaphore

Perhaps somebody can confirm, my understanding comes mostly from observation. It seems that there is fundamental difference between a binary semaphore and a mutex in that a mutex is tied to the thread that takes it. ie, only the thread picking it up can let it go. A binary semaphore can be released by other threads. Consider I/O examples, We pickup a semaphore to wait for some kind of I/O, when our Rx, task picks up the byte, the Rx task releases the same semaphore. This cannot be done with a mutex. Can someone please confirm this since the answer to the question is still not completely clear.
11/27/2006 9:31 PM | Kartik Aiyer
Gravatar

# re: Difference between Mutex and Semaphore

By the explanation of Niclas Winquist, we can say mutex is equivalent to binary semaphore.
12/14/2006 5:33 AM | Prashanth
Gravatar

# re: Difference between Mutex and Semaphore

from the above description, I think if i make the key number =0 in semaphore then it becomes mutex. So also semaphore description is not clear.
1/22/2007 8:43 AM | M. Neehar
Gravatar

# re: Difference between Mutex and Semaphore

Kartik is right about the difference between mutex and binary semaphore. But this difference exist for perticuler "type" of mutex. If we create a mutex object with type as "errorcheck" then mutex lock will be unlokced by only the thread which locks it. API is given below:
pthread_mutex_t mutex=PTHREAD_ERRORCHECK_MUTEX_INTIALISER_NP
lock created by muxtex object will check that if same thread has locked it or not.
1/29/2007 2:19 AM | Tej
Gravatar

# re: Difference between Mutex and Semaphore

As I understand, Mutex is the "policy' to get mutual execution and semaphore is the "mechanism" to acheive mutual execution.Please correct me if I am wrong
1/29/2007 11:26 PM | Neetu
Gravatar

# re: Difference between Mutex and Semaphore

Everything is nice. but can any body explain, in an application, on what basis we chose one of these semaphore and mutex..
1/30/2007 8:06 AM | Prashanth
Gravatar

# re: Difference between Mutex and Semaphore

As both are used for synchrnization...but wat is the main difference in between these two is still not clear
3/9/2007 10:01 AM | Chhavi
Gravatar

# re: Difference between Mutex and Semaphore

And, in keeping with your toilet metaphor, a deadlock is a bad case of constipation.
3/15/2007 2:22 PM | Some Dude
Gravatar

# re: Difference between Mutex and Semaphore

Read Fundamentals of Operating Systems, Third Edition by A.M. Lister, Page 19. Great book and all of you guys need to read this. "Mutex is the name of a Semaphore".
3/28/2007 3:14 AM | Lister
Gravatar

# re: Difference between Mutex and Semaphore

I think mutex is nothing but semaphore with value 0
3/30/2007 9:24 AM | sukant
Gravatar

# re: Difference between Mutex and Semaphore

there is a big difference of change in priority for the mutex and semaphore. the way there are initilized are different.
5/7/2007 11:18 PM | ARUN
Gravatar

# re: Difference between Mutex and Semaphore

nice example

Thanks
5/11/2007 7:33 PM | some dude
Gravatar

# re: Difference between Mutex and Semaphore


i had a doubt that whenever we post the semaphore it is actually unlocking the semaphore
i feel , that means the same thread is actually unlocking the semaphore ???
please explan
5/15/2007 7:31 PM | vijay
Gravatar

# re: Difference between Mutex and Semaphore

We actually mean a semaphore is nothing but a counting semaphore and by mutex we actually mean a mutex semaphore. A mutex is essentially a binary semaphore. You can replace any Mutex with a Semaphore of MaxCount 1.

Mutexes are usually more efficient than binary semaphores.

Mutex has an owner concept, i mean unlocking a mutex can be only done by the thread that locked (or, equivalently, "owns") the mutex.



5/28/2007 10:40 PM | Biki
Gravatar

# re: Difference between Mutex and Semaphore

5ytruyytitttttttttttttttttttttttiiu
6/10/2007 7:41 PM | sarit
Gravatar

# re: Difference between Mutex and Semaphore

I am just concluding the discussion.
theoretically, Mutex & Binary Semaphore are same but practically, mutex can be release by the task who acquired it (because that task has got ownership after acquiring it). In case of binary semaphore (or semaphore) can be release by any other task also, does not matter who acquired it.
7/2/2007 4:29 AM | Rajiv Nigam
Gravatar

# re: Difference between Mutex and Semaphore

One difference is owner ship. WHile a locked mutex should be released by the same thread WHile a thread can post to the semaphore
7/26/2007 3:51 PM | R. Chandra Sekhar
Gravatar

# re: Difference between Mutex and Semaphore

I am not sure, but, I think one more difference between Mutex and Semaphoe is

We acquire a mutex lock when we need to access global data to make only one thread
access the data in multithreaded environment.

We acquire a semaphore lock when we need to access global data to make only one
process or restricted number of process can access the data in multiprocess environment.

Thanks
Sudhakar
9/11/2007 8:13 PM | Sudhakar
Gravatar

# re: Difference between Mutex and Semaphore

To my mind the real difference between a semaphore and a mutex is that a semaphore is the structure proposed by Hoare many years ago as a general mechanism structure whereas a mutex is a special case of a mutual exclusion lock. You can make a mutex from a semaphore but not a semaphore from a mutex.

Semaphores have the other feature of being named so that they operate between processes. Mutexes are limited to serializing the threads within one process.

Another form of mutual exclusion lock is a spinlock which is basically the process of polling a test and set machine instruction. It is used for very fine grained locking where the overhead of the polling is either irrelevant, such as with a multiple processor machine or where the polling overhead is less than the mutex setup overhead or where a very small latency is critical.

A mutex comes at a surisingly high cost. You might sacrifice up to 5-10% of performance to the locking overhead.
9/19/2007 8:43 AM | Johns
Gravatar

# re: Difference between Mutex and Semaphore

Ur answer is really good. Even non computer science student also underastand with ur example .so,keep on answer......!
10/17/2007 6:05 PM | sivashankar
Gravatar

# re: Difference between Mutex and Semaphore

Plz all of you puts yur hands together


1/10/2008 12:57 PM | Ash
Gravatar

# re: Difference between Mutex and Semaphore

-->A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).
-->Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.
1/21/2008 11:24 PM | vijay
Gravatar

# re: Difference between Mutex and Semaphore


i have doubt on difference between semaphore and mutex?
1/21/2008 11:26 PM | vijay
Gravatar

# re: Difference between Mutex and Semaphore

good example
thanx
4/4/2008 9:31 PM | mahesh
Gravatar

# re: Difference between Mutex and Semaphore

A very nice example followed by a great discussion!
It solved all the doubts I had.
Thanks a ton!
4/6/2008 9:48 AM | Anuprit
Gravatar

# re: Difference between Mutex and Semaphore

Mutex is a binary semaphore
4/6/2008 9:58 PM | pradeep
Gravatar

# re: Difference between Mutex and Semaphore


Good Example....

Thanks
Shashidhara HN
5/14/2008 6:45 PM | Shashidhara HN
Gravatar

# re: Difference between Mutex and Semaphore

You got semaphore in my mutex.
No YOU got mutex in MY semaphore.
5/15/2008 7:18 AM | Harry Burnett Reese
Gravatar

# re: Difference between Mutex and Semaphore

To hold the mutual exclusion we use mutex and offcourse to serialized the execution we use semaphore.Both semaphore and mutex are integer variables.
5/16/2008 6:55 PM | Yasir Imtiaz khan
Gravatar

# re: Difference between Mutex and Semaphore

here I stand in that vast crowd of hungry philosophers shiveringly awaiting the announcement of the switching of the great big mutex blasting from the speakers on the wall. All around me tiny semaphores are counting up and down. Outscht!! Who has given forks to theese hungry guys? Fortunately they do not need the key to the toilet, because they did not eat for days.
5/27/2008 10:34 PM | John Michael Duillont
Gravatar

# re: Difference between Mutex and Semaphore

How are Mutex and Semaphores implemented to control the resources?
What resources can be locked using a semaphore?
5/30/2008 4:38 PM | Priyanka
Gravatar

# re: Difference between Mutex and Semaphore

Differences between Mutex and Binary Semaphore.
Mutex is actually an extension of binary semaphore with some extra features. If we are not using the extra features, this can be considered as a binary semaphore.
Differences are
1. If Mutex is acquired by some process, only that process can unlock it, where as a binary semaphore can be unlocked by any other process

2. In mutex, recursive calling is possible, where as this feature is not available in binary semaphore.

3. Priority inversion is possible with a mutex semaphore. The same is not applicable in case of binary semaphore
6/11/2008 5:18 PM | Akhil Chandran
Gravatar

# re: Difference between Mutex and Semaphore

Sorry guys, last one was Priority inheritance!! Sorry for the mistake!!
6/11/2008 5:40 PM | Akhil Chandran
Gravatar

# re: Difference between Mutex and Semaphore

In windows OS the mutex has few additinol features over a binary semaphore .
A mutex protect a thread from getting deadlocked due to reentrancy. This is achieved because the same thread can acquire the mutex any number of times. It should also release it the same number of times it has been acquired. Also APC delivery to the thread are disabled when the mutex is acquired.However Special Kernel Mode APC's can still be delivered to a thread even after mutex is acquired.This is where reentrant acqusition of mutex can happen.
6/12/2008 3:31 AM | Rajeev
Gravatar

# re: Difference between Mutex and Semaphore

Hai,

thanks for providing the information between Mutex and Semaphore

Ramesh Nakarikanti
6/26/2008 3:47 PM | Ramesh Nakarikanti
Gravatar

# re: Difference between Mutex and Semaphore

the difference between mutex and binary semaphore. But this difference exist for perticuler "type" of mutex. If we create a mutex object with type as "errorcheck" then mutex lock will be unlokced by only the thread which locks it. API is given below:
pthread_mutex_t mutex=PTHREAD_ERRORCHECK_MUTEX_INTIALISER_NP
lock created by muxtex object will check that if same thread has locked it or not
6/30/2008 8:18 PM | umesh
Gravatar

# re: Difference between Mutex and Semaphore

Nice answers i got the difference..
7/15/2008 7:22 PM | babu
Gravatar

# re: Difference between Mutex and Semaphore

Thanks for your valuable inputs guys. it clarified my doubts.
Really thanks for every one.

Regards,
Santha
8/7/2008 7:02 PM | Santharam V
Gravatar

# re: Difference between Mutex and Semaphore

Thanks so much!!^-^
8/14/2008 4:00 PM | Nicholas
Gravatar

# re: Difference between Mutex and Semaphore

As somebody already stated, semaphores are general solution that is widely used e.g for IPC (vide systemV semaphores implementation) while mutex is (basically binary semaphore) used for thread synchronization and as already stated there mutex is associated with it's owner.
8/25/2008 5:22 AM | GiM
Gravatar

# re: Difference between Mutex and Semaphore

A semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore), and can signal that they have finished using the resource (incrementing the semaphore).
-->Mutexes are typically used to serialise access to a section of re-entrant code that cannot be executed concurrently by more than one thread. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section
8/26/2008 5:25 PM | sahadev
Gravatar

# re: Difference between Mutex and Semaphore

• Re: How to check if a mutex is still locked?
... Shared resource should only be handled inside critical regions ... particular mutex mentioned in the initial message. ... lock the_super_mutex ... unlock the_super_mutex ...
(comp.programming.threads)
• Pthreads: how to insure mutex is unlocked when a thread dies.
... Anyway, I was planning to have this thread lock a mutex when it starts, ... unlock it when it's done, e.g., when the application notifies it to ... I'm concerned that if the thread dies before the application notifies ...
(comp.programming.threads)
• Re: double-checked locking in C
... Except that threads may never lock the mutex. ... guarantee they'll ever see the initialization. ... you don't call 'lock' or 'unlock' if the initialization ...
(comp.programming.threads)
• Re: Terminology?
... static lock ... you just mean mutex, see below. ... lock, a spinlock, a semaphore, etc... ...
(comp.programming.threads)
Re: [PATCH 1/19] MUTEX: Introduce simple mutex implementation
... > a mutex was a sensible implementation tradeoff. ... are really counting semaphores, ... >> acceptable patch that introduces a separate data structure. ... > general semaphore, because a mutex has stronger invariants. ...
8/26/2008 5:34 PM | Sahadev Tarei
Gravatar

# re: Difference between Mutex and Semaphore

Thanks guys... i learnt Mutex & Semaphore
8/28/2008 3:46 AM | Saravana
Gravatar

# re: Difference between Mutex and Semaphore

hi thanks
9/10/2008 6:29 AM | chandra
Gravatar

# re: Difference between Mutex and Semaphore

Thanks, you explained the difference, the simplest way possible!
9/16/2008 7:43 PM | Mortaza Doulaty
Gravatar

# re: Difference between Mutex and Semaphore

found similar information on the net...thought of sharing here!!

http://www.netrino.com/node/202
9/24/2008 7:18 AM | Abhik
Gravatar

# re: Difference between Mutex and Semaphore


Above link seems to be not working

http://www.netrino.com/node/202 9/24/2008
page not found .
10/5/2008 3:50 PM | friend
Gravatar

# re: Difference between Mutex and Semaphore

From experience and comments it seems the most important distinction to make is that a mutex protects a particular section of code run by a single thread (thus limiting it to 1 holder), while a semaphore protects access to a resource, however that resource is managed.

11/24/2008 5:35 PM | wtg

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 5 and type the answer here:

Powered by: