Prerequisite – Process Synchronization
1. Binary Semaphore :
Binary semaphores are semaphores which can assume the values 0 and 1 only. They are used for implementing the locks by using signalling mechanism for achieving mutual exclusion.
Here, if the value of semaphore is 0 it means it is locked so, lock is unavailable.
If the value of semaphore is 1 it means it is unlocked so, lock is available.
2. Mutex :
A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa.
At any point of time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.
Difference between binary semaphore and mutex :
|Its functions based up on signalling mechanism||Its functions based up on locking mechanism|
|The thread which is having higher priority than current thread can also release binary semaphore and take lock.||The thread which has acquired mutex can only release Mutex when it exits from critical section.|
|Semaphore value is changed according to wait () and signal () operations.||Mutex values can be modified just as locked or unlocked.|
|Multiple number of threads can acquire binary semaphore at a time concurrently.||Only one thread can acquire mutex at a time|
|Binary semaphore have no ownership.||There is ownership associated with mutex because only owner can release the lock.|
|They are faster than mutex because any other thread/process can unlock binary semaphore.||They are slower than binary semaphores because only thread which has acquired must release the lock.|
|If you have number of instances for resource it is better to use Binary semaphore.||If you have single instance for resource it is better to use mutex.|