I found this article on mutexes and semaphores really an interesting read.
The author has tried to explain the difference between mutex and semaphores elaborating and explaining the famous Bathroom example quoted here :
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
The author thus concludes that the correct use of a semaphore is for signaling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects.
A mutex is owned by a process. Whereas, semaphore allows one or more processes to share the resource.
mutex_lock(&mutex_obj)
// access global_data
mutex_unlock(&mutex_obj)H
Here, you are locking global data so that only one process can access it. Others will wait in a queue to access this global data.While in semaphore you can define how many concurrent processes can access that resource at a time.
sem(10) // ten process have the concurrent access
sem_lock(&sem_obj)
//access global_data
sem_unlock(&sem_obj)
When one process locks the semaphore the count decrements by 1. So when the count=0, all the processes are using the data.
Difference between binary semaphore and mutex :
A binary semaphore is like a mutex but there is a fundamental difference between a binary semaphore and a mutex in that 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.
Properties of mutex and sempahore :
1. As pointed out by Michael barr, semaphores can also be used to signal from an interrupt service routine (ISR) to a task. Signaling a semaphore is a non-blocking RTOS behavior and thus ISR safe. Because this technique eliminates the error-prone need to disable interrupts at the task level, signaling from within an ISR is an excellent way to make embedded software more reliable by design.
2. In mutex, recursive calling is possible, where as this feature is not available in binary semaphore.Mutex can be recursively locked by same thread so though it has to be released by thread same number of times. In case of binary semaphore it is not possbile.
3. Priority inversion is possible with a mutex. The same is not applicable in case of binary semaphore.With three threads A,B and C, with high , medium and low priority – if A and B are blocked on some event , C gets a chance to run. C while running can enter a critical section and lock a mutex.Meanwhile A and B become ready to run , and if A has to claim the mutex, it has to wait for C to release the mutex.However, if B does not have dependency on the mutex, it can run , as C has lower priority than B, thus C cannot proceed until B is running and A is blocked by a lower priority process running -B.
The risk of priority inversion can be eliminated by changing the operating system internal implementation of mutexes. Of course, this adds to the overhead cost of acquiring and releasing mutexes. However, it is not necessary to change the implementation of semaphores, which do not cause priority inversion when used for signaling. This is a second important reason for having distinct APIs for these two very different RTOS primitives.
And as I dug Operating Systems by William Stallings, I took the following para from Stallings as conclusive:
To begin, the semaphore has a zero or positive value.If the value is positive,that value equals the number of processes that can issue a wait and immediately continue to execute.If the value is zero, either by initialization or because a number of processes equal to the initial semaphore value have issued a wait.the next process to issue a wait is blocked,and the semaphore value goes negative.Each subsequent wait drives the semaphore value further into minus territory.The negative value equals the number of processes waiting to be unblocked.Each signal unblocks one of the waiting processes when the semaphore value is negative.
For both counting semaphores and binary semaphores, a queue is used to hold processes waiting on the semaphore.The question arises of the order in which processes are removed form such a queue.The fairest removal policy is first-in-first-out (FIFO).The process that has been blocked the longest is released from the queue first;a semaphore whose definition includes this policy is called a “Strong Semaphore”.A semaphore that does not specify the order in which procceses are removed from the queue is a “weak semaphore”.
“A concept related to binary semaphore is the mutex.A key difference between the two is that the process that locks the mutex (Sets the value to zero) must be the one to unlock it.In contrast, it is possible for one process to lock a binary semaphore and for another to unlock it”
In some of the literature and in some textbooks, no distinction is made between a mutex and a binary semaphore.However, in practice, a number of operating systems such as Linux , Windows and Solaris offer a mutex facility that conforms to the definition in this book”
Links for references :