Mutex and semaphore are used for
providing mutual exclusion property, and these are Kernel resources to provide synchronization
services.
Now comes to differences:
- Mutex job is mainly to provide Mutual Exclusion and Semaphore is mainly used for resource sharing.
- Mutex serializes the access to critical section of code i.e. only one process/thread can access this code at a time. (Single Resource Manager)
- Semaphore looks like Mutex if the semaphore count/value is equal to 1 since if one thread acquires lock then other thread trying to access the critical section should wait until the 1st thread releases the acquired lock. But, the purpose of using the Mutex and Semaphore is different.
- Semaphores are of two types based
on this value
Semaphore value = 1, then binary semaphore Semaphore values > 1 then counting semaphore i.e. it can handle multiple identical resources with its count value. - Mutex is just a lock, whoever acquired it they should release that lock so that next process/thread which is in the queue will get this lock. There is no signal mechanism for Mutex. But, Semaphore can signal the other process/threads by simply increment the semaphore value by 1. So, Semaphores are mainly used for synchronizing the multiple processes/threads.
Mutex
|
Semaphore
|
A Locking mechanism used to synchronize access to a resource. Mutex
is just a Lock.
|
A Signaling mechanism and used for message passing mechanism
|
Only one task can acquire the Mutex and can release it. Mutex
maintains ownership info
|
Lacks of ownership details
|
Mutex is used within a process by multiple threads
|
Semaphore can be used across processes
|
Mutex can be used only in userspace
|
Semaphore can be used in Kernel space also
|
What is Critical Section?
Definition: A Critical section is a piece of code that
accesses shared resources that must not be concurrently accessed by more than one
thread of execution.
So, some Synchronization mechanism are being used to ensure
this for ex: Mutex and Semaphores
What is Mutual Exclusion Property?
It is a property to ensure that no two concurrently
executing threads/processes are in their critical section at the same time.
What is Synchronization?
Definition: Synchronization is a mechanism to ensure that
two concurrently executing threads/processes do not execute specific portion of
code/program at the same time. This specific portion of code/program is called
as Critical Section.
Reference: Wikipedia and GeeksForGeeks
No comments:
Post a Comment