Mutex and Semaphores in Operating system.

Effective Synchronization Techniques: A Look at Mutexes and Semaphores

Mutex

Mutex is a specific kind of binary semaphore that is used to provide a locking mechanism. It stands for Mutual Exclusion.

Mutex is mainly used to provide mutual exclusion to the critical section so that the process can execute and work with the critical section for a particular amount of time.

Mutex uses priority inheritance mechanism to avoid priority inversion issue.

The priority inheritance mechanism keeps higher-priority processes in the blocked state for the minimum possible time. However, this cannot avoid the priority inversion problem, but it can reduce its effect up to an extent.

Mutex helps in solving the famous Producer-Consumer problem

Advantages

  1. No race condition arises, as there is only one process in the critical section at a time.

  2. Data remains consistent and it helps in maintaining integrity.

  3. It's a very simple locking mechanism.

Disadvantages

  1. If after entering into the critical section, the thread sleeps no other thread can enter into the critical section. This can lead to starvation.

  2. Implementation of mutex is a time taking process which can lead to wastage of CPU cycle.

Semaphores

Semaphores are like normal Integer variables which are used to coo-ordinate between the activities of multiple processes.

Semaphore works upon signaling mechanism, in this a thread can be signaled by another thread.

semaphores are implemented using two operations-

wait(P):-It simply tells the process weather it can enter into the critical section or not.

if the semaphore value is greater than or equal to 1, means the process can access the critical section.

whenever a process enters into a critical section the value of semaphore is reduced by one.

if the semaphore value is equal to 0, means no process can enter into the critical section.

wait(S){
while(S<=0);   // busy waiting
S--;
}

signal(V):After the completion of the process in the critical section, the value of semaphore is increased by 1.

this means that some resource has been deallocated by the process.

There are two types of semaphores Binary semaphores and counting semaphores.

Advantages

  1. Multiple threads can access the critical section at the same time.

  2. Semaphores are machine-independent.

  3. Only one process can access the critical section at a time, however, multiple threads are allowed.

Disadvantages

  1. It has issue of priority inversion

  2. Semaphore’s operation (Wait, Signal) must be implemented in the correct manner to avoid deadlock.

  3. Semaphores are used for more complex synchronization problems.

Key Differences

  1. Synchronization Scope:

    • Mutexes provide mutual exclusion for critical sections, ensuring only one thread can execute a particular piece of code at a time.

    • Semaphores control access to a resources, allowing a specified number of concurrent accesses.

  2. Ownership and Responsibility:

    • Mutexes have ownership associated with it, meaning the thread that locks the mutex must be the one to unlock it.

    • Semaphores do not have ownership, and any thread can signal or wait on the semaphore.

  3. Implementation Complexity:

    • Mutexes are simpler to implement for single access control.

    • Semaphores are used for more complex synchronization problems, like producer-consumer scenarios.

  4. Usage Patterns:

    • Mutexes are generally used to protect critical sections.

    • Semaphores are used to manage resources and to solve more complex synchronization problems.

MISCONCEPTION

There is an ambiguity between binary semaphore and mutex. We might have come across that a mutex is a binary semaphore. But it is not! The purposes of mutex and semaphore are different. Maybe, due to similarity in their implementation a mutex would be referred to as a binary semaphore.

A mutex is a locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process) can acquire the mutex. It means there is ownership associated with a mutex, and only the owner can release the lock (mutex).

Semaphore is a signaling mechanism (“I am done, you can carry on” kind of signal).there is no ownership associated with it.