Difference Between Mutex and Semaphores in OS

In this post, we will see what is mutex and semaphores, different examples related to it. Then, we will see difference between mutex and semaphores in os.

Mutex

Mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. When a program is started, a Mutex is created with a unique name. After this, any thread that needs the resource must lock the Mutex from other threads while it is using the resource. The Mutex is set to unlock when data is no longer needed.

Thus, Mutex is a binary variable whose purpose is to provide locking mechanism.

To understand the concept of Mutex, let’s take an example:

Let’s assume there is a one room and these three persons want to use the room. The door is locked and there is only one key. So, at a time only one person can occupy the room. Once he is done, he will give key to another person.

So, one person unlock the door, then, enter into the room and close it. During that time, other persons will be waiting. Now, the person will give key to the next person and next person will enter into the room. So, like this, scenario will proceed.

Tutorialwing operating system Mutex example in operating system

Mutex example

Similarly, mutex is an object that is owned by a thread at a time. Once the thread’s work is done, thread will release the Mutex. Thus, there is ownership in case of Mutex.

Every thread has the same code. It will be like –

Lock (Mutex)

Do Work

Unlock (Mutex)

As the code depicts, when one thread is accessing the resource, it will lock the Mutex object. Now, other threads can’t access the resource. When work is done, it will release the object and other thread can access the resource.

Tutorialwing operating system mutex example

Mutex Example

Semaphore

Semaphore is a signalling mechanism. Semaphore restricts the number of simultaneous users of a shared resource up to a maximum number. Threads can request access to the resource (decrementing the semaphore) and can signal that they have finished using the resource (incrementing the semaphore). It allows number of thread to access shared resources.

Tutorialwing operating system semaphore example

Semaphore example

Let’s say the semaphore S is a variable which value is 2. The value is specifying how many processes simultaneously access the resource. Whenever a process wants to access the resource, at first it has to decrement the value of S. Now, value is 1. So, process P1 can access the resource. Process P2 also can access the resource because value of semaphore is 0 after decrementing it. But now, if P3 wants to access the resource, it can’t because value is -1 after decrementing it. That process has to wait until any process release the resource.

Difference between Mutex and Semaphores

Now, we will see the difference between mutex and semaphore.

  • Mutex is used for thread but semaphore is used for process.
  • Mutex is locking mechanism ownership method. Semaphore is a signaling mechanism.
  • Mutex works in user space but semaphore works in kernel space.

Leave a Reply