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
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.
} 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.
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
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.
Importantly, the main difference between mutex and semaphore is that the mutex is a locking mechanism, while the semaphore is a signaling mechanism.
Besides, the mutex is an object whereas semaphore is an integer variable.
Also, mutex has no categorization, while semaphores are categorized as binary and counting semaphores.
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.
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.
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.
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.