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 in Operating system (OS)
Mutex in OS
Mutex in Operating System (OS) refers to a synchronization primitive used to manage access to shared resources by multiple threads or processes. It ensures that only one thread can access the critical section of code at a time, preventing race conditions and maintaining data consistency.
On this page, we will discuss what a Mutex is in an Operating System and how it helps us. Mutex lock in OS provides code wise functionality for mutual exclusion, which is essential in multi threaded environments.

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
Prepare for Interview with us:
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--; }
Actual Working for both functions together to achieve access of critical section –
// some code wait(s); // critical section code signal(s); // 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

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
Closing Remarks
Mutex in Operating Systems is a fundamental synchronization tool used to manage access to shared resources and prevent race conditions. It works by allowing only one thread to access a critical section at a time using a lock and release mechanism. Unlike semaphores, mutexes are binary in nature and primarily used for thread synchronization in user space. Understanding the distinction between mutexes and semaphores is crucial for implementing efficient concurrency control. Together, these tools help maintain data integrity and stable multi threaded operations.
FAQs
If the mutex is already locked, the thread is blocked until the mutex becomes available again.
No, mutexes are typically used for thread synchronization within the same process, not between different processes.
A critical section is a code block that accesses shared resources and must not be executed by more than one thread at a time.
Yes, if multiple threads wait on each other’s mutexes without releasing, it can lead to a deadlock situation.
Login/Signup to comment