Mutex in Operating system (OS)

Mutex in OS

Let us have a look on what mutex in an Operating System is and how it helps us –


Mutex in Operating System

Mutex lock in OS is essentially a variable that is binary nature that provides code wise functionality for mutual exclusion. At times, there maybe multiple threads that may be trying to access same resource like memory or I/O etc. To make sure that there is no overriding. Mutex provides a locking mechanism.

Only one thread at a time can take the ownership of a mutex and apply the lock. Once it done utilising the resource and it may release the mutex lock.

Mutex Highlights

Mutex is very different from Semaphores, please read Semaphores or below and then read the difference between mutex and semaphores here.

  1. Mutex is Binary in nature
  2. Operations like Lock and Release are possible
  3. Mutex is for Threads, while Semaphores are for processes.
  4. Mutex works in user-space and Semaphore for kernel
  5. Mutex provides locking mechanism
  6. A thread may acquire more than one mutex
  7. Binary Semaphore and mutex are different

Semaphores in OS

While mutex is a lock (wait) and release mechanism. Semaphores are signalling mechanisms that signal to processes the state of the Critical section in OS and grant access to the critical section accordingly.

Semaphores use the following methods to control access to critical section code –

  1. Wait
  2. Signal

Semaphore Types

We have two types of semaphores –

  1. Binary Semaphore
    1. Only True/False or 0/1 values
  2. Counting Semaphore
    1. Non-negative value

Semaphore Implementation

Wait and Signal are two methods that are associated with semaphores. While some articles are represented as wait(s) or signal(s) however in some blogs are represented as p(s) for wait and v(s) for signal

Wait p(s) or wait(s)

  1. Wait decrements the value of semaphore by 1

Signal v(s) or signal(s)

  1. Signal increments the value of semaphore by 1


  1. Semaphore can only have positive values
  2. Before the start of the program, it is always initialised to
    1. n in Counting semaphore (Where n is the number of processes allowed to enter critical section simultaneously)
    2. 1 in the case of a binary semaphore
Semaphore – 1

Signal Operations

  • Increments semaphore by 1
  • Signals that the process has completed its critical section execution

Wait Operations

  • Wait operation decrements the value of semaphore S if S is a positive number
  • Else if S is 0 or negative then code gets stuck at while loop as it keeps implementing infinitively
  • The semi-colon after while forces while loop definitively if S is 0 or negative
  • Thus the code doesn’t move ahead in hopes that the value of S will increase because of some other signal operation elsewhere

Code Logic for Incrementing – Decrementing value of Semaphore –

    while (S<=0);


Actual Working for both functions together to achieve access of critical section –

    // some code


    // critical section code


    // remainder code

The eventual goal is to protect the critical section code using wait and signal operations.

You can visualize the whole operation on how the Semaphore system works with the example below

Mutex Semaphore Binary

For Counting Semaphore

For Counting Semaphore we initialise the value of semaphore as the number of concurrent access of critical sections we want to allow.

The eventual goal is to protect the critical section code using wait and signal operations.

You can visualize the whole operation on how the Semaphore system works with the example below

Operating System Process Synchornization

      Read More

    1. Process Synchronization
    2. Critical Section
    3. Inter-Process Communication
    4. UEFI(Unified Extensible Firmware Interface) and how is it different from BIOS
    5. Mutex
    6. Semaphore
    7. Mutex vs. Semaphore
    8. Atomic Operations in OS
    9. Peterson’s Algorithm for Mutual Exclusion (Only important for Cisco and Arista Networs)
      1. Java
      2. C
      3. Python
    10. Peterson’s Algorithm for Critical Section Problem (Only important for Cisco and Arista Networs)
    11. Readers-Writers Problem