The main difference between Mutex and Semaphore is that the mutex is a locking mechanism, while the semaphore is a signaling mechanism.
When multiple processes access shared data simultaneously, it can cause data inconsistency. It is necessary to maintain the order of executing the processes to maintain data consistency. Furthermore, the critical section is a code segment that accesses shared variables or shared resources. When multiple processes execute in the critical section, it can cause data inconsistency on shared variables. Hence, this is called a critical section problem. Therefore, it is necessary to synchronize the processes to avoid the critical section problem. Two software solutions for process synchronization are mutex and semaphore.
Key Areas Covered
1. What is Mutex
-Definition, Functionality
2. What is Semaphore
-Definition, Functionality
3. Difference Between Mutex and Semaphore
-Comparison of key differences
Key Terms
Data Consistency, Mutex, Semaphore
What is Mutex
Mutex or mutual exclusion object is a locking mechanism. The process should acquire the lock before entering the critical section. After executing in the critical section, the process releases the lock. Likewise, only one process executes in the critical section at a time. When there are multiple processes, only one process will execute in the critical section at a time; not simultaneously.
Refer below code.
do{
acquire lock
critical section
release lock
remainder section
} while (true)
At the beginning of executing a program, it requests the system to create a mutex. When the process wants to execute in the critical section, it occupies the lock. The operation is called acquire(). After completing the execution of the critical section, the process releases the lock. The operation is called release(). Then another process can acquire the lock. When a process has the lock, the other processes cannot execute in the critical section. Therefore, those processes have to wait in the queue until the mutex is unlocked.
What is Semaphore
Semaphore is an integer variable (S). Two atomic operations, wait() and signal(), help to modify the value of the semaphore. When a specific process changes the semaphore value, another process cannot modify the semaphore value simultaneously.
The code of wait and signal are as follows.
wait(S){
while(s<=0);
s–;
}
signal(s){
s++;
}
Moreover, there are two types of semaphore; binary semaphore and counting semaphore. In a binary semaphore, the integer value can change between 0 to 1. If a process wants to access the critical section, it performs the wait() operation. Then, it decreases the semaphore value from 1 to 0. When exiting the critical section, it performs signal() operation on the semaphore. Thus, it will increase the value to 1.
In counting semaphores, the value can change in an unrestricted domain. If a process requires to execute in the critical section, it a () operation and decrements the semaphore value by 1. When exiting the critical section, it performs signal() operation and increments the semaphore value by one.
In both situations, the semaphore value 0 indicates that all resources are in use. If a process requires to obtain the semaphore and the semaphore value is 0, then the processes perform wait() until the semaphore value becomes greater than 0.
Difference Between Mutex and Semaphore
Definition
Mutex is a program object which allows multiple processes take turns to share the same resource. In contrast, semaphore is a variable used to control access to a common resource by multiple processes in a concurrent system such as a multitasking operating system. Thus, this is the fundamental difference between mutex and semaphore.
Mechanism
Importantly, the main difference between mutex and semaphore is that the mutex is a locking mechanism, while the semaphore is a signaling mechanism.
Type
Besides, the mutex is an object whereas semaphore is an integer variable.
Categorization
Also, mutex has no categorization, while semaphores are categorized as binary and counting semaphores.
Releasing
Furthermore, if the mutex is locked, the process that requests the lock waits until the system releases the lock. On the other hand, if the semaphore value is 0, the process perform wait() operation until the semaphore becomes greater than 0.
Operations
Moreover, a process uses acquire() and release() to access and release the mutex, whereas process use wait() and signal() to modify semaphore. Hence, this is another difference between a mutex and semaphore.
Conclusion
In brief, mutex and semaphore are two mechanisms to synchronize the functionality of multiple processes. The main difference between mutex and semaphore is that the mutex is a locking mechanism, while the semaphore is a signaling mechanism. Thus, the semaphore is more sophisticated than mutex.
References:
1.“Process Synchronization.” Studytonight, Available here.
2.“What Is Mutex (Mutual Exclusion Object)? – Definition from WhatIs.com.” SearchNetworking, Available here.
3.“Semaphore (Programming).” Wikipedia, Wikimedia Foundation, 1 May 2019, Available here.
Image Courtesy:
1.”Three processes” By M.usman.14 – Own work (CC BY-SA 4.0) via Commons Wikimedia
Leave a Reply