Mutex vs Semaphore (original) (raw)

Last Updated : 15 Apr, 2026

Mutex and Semaphores are kernel resources that provide synchronization services (also known as synchronization primitives). Synchronization is required when multiple processes are executing concurrently, to avoid conflicts between processes using shared resources.

Mutex

A mutex is different from a binary semaphore, which provides a locking mechanism. It stands for Mutual Exclusion Object. Mutex is mainly used to provide mutual exclusion to a specific portion of the code so that the process can execute and work with a particular section of the code at a particular time.

Mutex

Mutex

**Note: 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.

**Using Mutex

**The **producer-consumer problem: Consider the standard producer-consumer problem. Assume, we have a buffer of 4096-byte length. A producer thread collects the data and writes it to the buffer. A consumer thread processes the collected data from the buffer. The objective is, that both the threads should not run at the same time.

**Solution: 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 the producer, the consumer needs to wait and vice versa. At any point in time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.

Advantages of Mutex

Disadvantages of Mutex

Semaphore

A semaphore is a non-negative integer variable that is shared between various threads. Semaphore works upon signaling mechanism, in this a thread can be signaled by another thread.

Semaphore

Semaphore

**Note: Semaphores are often used for coordinating signaling between threads. Semaphore uses two atomic operations for process synchronization: Wait (P) & Signal (V).

Using Semaphore

**The **producer-consumer problem: Consider the standard producer-consumer problem. Assume, we have a buffer of 4096-byte length. A producer thread collects the data and writes it to the buffer. A consumer thread processes the collected data from the buffer. The objective is, both the threads should not run at the same time.

**Solution: A semaphore is a generalized mutex. instead a single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.

Advantages of Semaphore

Disadvantages of Semaphore

Difference Between Mutex and Semaphore

Mutex Semaphore
A mutex is an object. A semaphore is an integer.
Mutex works upon the locking mechanism. Semaphore uses signaling mechanism.
Operations on mutex: Lock & Unlock Operation on semaphore: Wait & Signal
Mutex does not have any subtypes. Semaphore is of two types: Counting Semaphore & Binary Semaphore
A mutex can only be modified by the process that is requesting or releasing a resource. Semaphore work with two atomic operations (Wait, signal) which can modify it.
If the mutex is locked then the process needs to wait in the process queue and mutex can only be accessed once the lock is released. If the process needs a resource and no resource is free. So, the process needs to perform a wait operation until the semaphore value is greater than zero.

Misconception of Mutex and Semaphore

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 of a mutex would be referred to as a binary semaphore.

While, a semaphore is a signaling mechanism used to control access to shared resources in an operating system. For example, imagine you are downloading a large file on your computer (Task A) while simultaneously trying to print a document (Task B). When the print job is initiated, it triggers a semaphore that checks if the download is complete.