OS Menu9>
- OS Home
- Introduction
- CPU Scheduling
- What is Process?
- Process Lifecyle
- Process Control Block
- Process Scheduling
- Context Switching
- CPU Scheduling
- FCFS Scheduling
- SJF (non-preemptive)
- SJF (Preemptive - SRTF)
- Round Robin
- Priority Scheduling
- Convoy Effect
- Scheduler Vs Dispatcher
- Preemptive Vs non
- Preemptive scheduling
- Non preemptive scheduling
- Process Synchronization
- Deadlock
- Popular Algorithms
- Memory Management
- Memory Management Introduction
- Partition Allocation Method
- First Fit
- First Fit (Intro)
- First Fit in C
- First Fit in C++
- First Fit in Python
- First Fit in Java
- Best Fit
- Best Fit (Intro)
- Best Fit in C
- Best Fit in C++
- Best Fit in Java
- Worst Fit
- Worst Fit (Intro)
- Worst Fit in C++
- Worst Fit in C
- Worst Fit in Java
- Worst Fit in Python
- Next Fit
- First fit best fit worst fit (Example)
- Memory Management 2
- Memory Management 3
- Page Replacement Algorithms
- LRU (Intro)
- LRU in C++
- LRU in Java
- LRU in Python
- FIFO
- Optimal Page Replacement algorithm
- Optimal Page Replacement (Intro)
- Optimal Page Replacement Algo in C
- Optimal Page Replacement Algo in C++
- Optimal Page Replacement Algo in Java
- Optimal Page Replacement Algo in Python
- Thrashing
- Belady’s Anomaly
- Static vs Dynamic Loading
- Static vs Dynamic Linking
- Swapping
- Translational Look Aside Buffer
- Process Address Space
- Difference between Segmentation and Paging
- File System
- Off-campus Drive Updates
- Get Hiring Updates
- Contact us
PREPINSTA PRIME
Mutex Vs Semaphore in OS
Mutex Vs Semaphore
Mutex Vs Semaphore in OS On this page, we will discuss about Mutex vs Semaphore in Operating System. Both are used to handle multiple processes or threads trying to access the same resource.
A Mutex is used when only one thread should access a resource at a time. If one thread has the mutex, others have to wait. It is like a lock that only one thread can hold.
A Semaphore can allow more than one thread to access the resource. It uses a counter to keep track of how many threads can enter. When the counter is 0, other threads have to wait.

What is the difference between Mutex and Semaphore in Operating System
Mutex is essentially a locking and releasing mechanism and however, Semaphore is a signalling mechanism. Both are used for Critical section and mutual exclusion problems.
Most people think that Binary Semaphore and Mutex are essentially the same but they are not.
We first recommend reading the post about Semaphores and mutex here before proceeding further.
Differences
Mutex
Example – Imagine mutex as a key to a single toilet. Only one person can be inside the toilet at a time and he would want to lock the toilet and others waiting for toilet access must wait for person already inside to release the toilet. When finished the person gives the key to next person in the queue.
- Mutex is for thread.
- Mutex is essentially atomic and singular in nature.
- Mutex is binary in nature.
- Operations like Lock and Release are possible
- Mutex works in userspace
- Only one thread can acquire a mutex at a time
- Mutex is an object
Semaphore
Example – Imagine a bathroom with 4 identical toilets here, identical keys can open the bathrooms. Here as many as 4 people can use the bathroom simultaneously and they wait and signal one another about the occupancy of the bathrooms
- Semaphore is for processes
- Semaphores are also atomic but not singular in nature
- Semaphores are of two types that are binary and counting
- Semaphores work in kernel space
- Only one process can acquire binary semaphore at a time but multiple processes can simultaneously acquire semaphore in case of counting semaphore
- Semaphore is an integer variable
Prepare for Interview with us:
What is SpinLock ?
While a process is in critical section, any other process which waits to enter critical section must loop continuously in call to acquire. This is a spinlock as the process “Spins” while waiting for the lock to be available.
Advantages –
- No Context switch is required for a process waiting on a lock.
- Good for systems where it’s known that locks are of short duration.
- Employed in Multiprocess system, where one thread can spin on 1 processor while another thread processes its critical section in another processor.
Semaphores –
- It’s a Generalized Mutex.
- Semaphores are integer variable , which is accessed through 2 atomic operations, wait() and signal ().
- It’s a signalling Mechanism.
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.
- Mutex is Binary in nature
- Operations like Lock and Release are possible
- Mutex is for Threads, while Semaphores are for processes.
- Mutex works in user-space and Semaphore for kernel
- Mutex provides locking mechanism
- A thread may acquire more than one mutex
- 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 –
- Wait
- Signal

Semaphore Types
We have two types of semaphores –
- Binary Semaphore –
- Only True/False or 0/1 values
- Counting Semaphore –
- 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)
- Wait decrements the value of semaphore by 1
Signal v(s) or signal(s)
- Signal increments the value of semaphore by 1
Semaphore
- Semaphore can only have positive values
- Before the start of the program, it is always initialised to
- n in Counting semaphore (Where n is the number of processes allowed to enter critical section simultaneously)
- 1 in the case of a binary semaphore

Signal Operations
- Increments semaphore by 1
- Signals that the process has completed its critical section execution
signal(S) { S++; }
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 –
wait(S) { while (S<=0); S--; }
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

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
Final thoughts
Both Mutex and Semaphore are essential synchronization tools in operating systems, but they serve different purposes. A Mutex is a locking mechanism used primarily for threads, allowing only one thread to access a resource at a time. On the other hand, a Semaphore is a signaling mechanism used for processes, which can allow multiple accesses based on its counter. Understanding their differences helps in choosing the right synchronization tool for handling critical sections efficiently.
FAQs
Use a Mutex when you need strict ownership only the thread that locks it can unlock it, ensuring tighter control.
A Counting Semaphore allows multiple concurrent accesses, unlike Mutex which permits only one thread at a time.
It can cause a deadlock, as no other thread will be able to acquire the lock until it’s released.
Technically yes, but it’s not ideal Semaphores lack the built in ownership enforcement that Mutex provides for threads.
Login/Signup to comment