- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Semaphore and Mutex
In this post, we will understand the difference between a semaphore and a mutex −
Semaphore
It is used in the signalling mechanism.
It is an integer variable.
The ‘wait’ and ‘signal’ operations can be used to modify a semaphore.
If no resources are available, the process requires that a resource that needs to be executed has to ‘wait’.
The process will have to ‘wait’ until the count of the semaphore is higher than 0.
Multiple program threads can be used with a semaphore.
The value of a semaphore can be changed by any process that releases or obtains the resource.
There are different kinds of semaphores: Counting semaphore and Binary semaphore.
The value of a semaphore can be changed using ‘wait’ operation and ‘signal’ operation.
The process is occupied if all resources are used.
The process requesting the resource uses the ‘wait’ operation and blocks itself until the count of the semaphore becomes greater than 1.
Mutex
It is used in locking mechanism.
It is an object.
It is modified by that process (only) that requests or releases a resource.
If the mutex is locked, then the process will have to wait.
The process is kept in a queue when mutex s locked.
This can be accessed only after the mutex has been unlocked.
Multiple program threads can be used in mutex (but not simultaneously).
The lock on the object can be released by the process only, which would have obtained the lock on it.
It doesn’t have any subtypes.
The object is either locked or unlocked.
If the object has already been locked, the process that requests the resources will wait.
It will be queued by the system before the lock has been released.
- Related Articles
- Mutex vs Semaphore
- Semaphore and Mutex in FreeRTOS using Arduino
- Difference Between Semaphore and Monitor in OS
- Race Condition, Critical Section and Semaphore
- Semaphore in C#
- Semaphore in Java
- Deadlock with mutex locks
- How to use Mutex in Golang?
- What is the Mutex class in C#?
- Difference Between & and &&
- Difference between Voltage Drop and Potential Difference
- How semaphore is used to implement mutual exclusion?
- Difference between C# and .Net
- Difference between Process and Thread
- Difference between Concurrency and Parallelism
