13 Major Difference Between Semaphore And Mutex (With Chart)

SHARE

What Is Semaphore?

Semaphore is a variable or abstract data type used to control access to a common resource by multiple processes in a concurrent system such as multitasking operating system. Semaphore concept was invented by Dutch computer scientist, Edsger Dijkstra in 1962. Semaphore is typically a variable used to solve critical section problems and to achieve process synchronization in multi processing environment.

In concurrent programming, semaphore is typically an integer variable that is initialized to the number of resources present in the system. The value of semaphore can be modified only by two functions: wait () and signal () apart from initialization.

Semaphores which are restricted to the values 0 and 1 (lock/unlock, available/unavailable) are referred to as binary semaphore and are used to implement locks. On the other hand, Semaphores which allow an arbitrary resource count are referred to as counting semaphore.

Binary Semaphore

What Are Some Of The Disadvantages Of Semaphore?

  • Semaphore programming is a complex method and therefore chances of not achieving mutual exclusion are high.
  • Semaphore is more prone to errors
  • The operating system has to keep track of all calls to wait and signal semaphore.
  • Chances of deadlock are high in semaphore incase the wait and signal operations require to be executed in the correct order.

What Are The Some Of The Advantages Of Semaphore

  • Does not allow multiple processes to enter critical section.
  • It allows more than one thread to access the critical section.
  • They allow efficient management of resources.
  • There is no wastage of process time and resources in semaphore due to busy waiting.

What You Need To Know About Semaphore

  1. Semaphore is a signaling mechanism and a thread waiting on a semaphore can be signaled by another thread.
  2. Semaphore is for processes.
  3. Semaphore is atomic but not singular in nature.
  4. A binary semaphore can be used as a mutex along with providing feature of signaling amongst threads.
  5. Semaphore value can be changed by any process acquiring or releasing the resource.
  6. Semaphore is an integer variable.
  7. If locked, a semaphore can be acted upon by different threads.
  8. A semaphore uses two atomic operations, wait and signal for process synchronization.
  9. Only one process can acquire binary semaphore at a time but multiple processes can simultaneously acquire semaphore in case of counting semaphore.
  10. Semaphore works in kernel space.
  11. The concept of ownership is absent in semaphore.
  12. Semaphore can be categorized into counting semaphore and binary semaphore.
  13. If all resources are being used, the process requesting for resource performs wait () operation and block itself till semaphore count become greater than one.

What Is Mutex?

In concurrent programming, Mutex is an object in a program that serves as a lock, used to negotiate mutual exclusion among threads. Mutex is a special case of the Semaphore; it is a mutual exclusion object that synchronizes access to a resource. A mutex object only allows one thread into a controlled section, forcing other threads which attempt to gain access to that section to wait until the first thread has exited from that section.

When a program is started, a mutex is created with a unique name. After this stage, any thread that needs the resource must lock the mutex from other threads while it is using the resource. The mutex is set to unlock when the data is no longer needed or the routine is finished.

Mutex

What Are Some Of The Disadvantages Of Mutex?

  • It is difficult to lock or unlock mutex from a different context than the one that acquired it.
  • Only one thread is supposed to be allowed in the critical section at a time.
  • In case of busy waiting state the CPU time is wasted.
  • If a thread obtains a lock and in the process it is preempted, then, the other thread may not be able to move.

What Are Some Of The Advantages of Mutex?

  • There are no race conditions and data always remain consistent due to the fact that, in mutex, only one thread is in critical section at any given time.
  • The thread with mutex has ownership over the resource.
  • Mutex is typically atomic and singular in nature.

What You Need To Know About Semaphore

  1. The Mutex is a locking mechanism that makes sure only one thread can acquire the mutex at a time and enter the critical section.
  2. Mutex is for thread.
  3. Mutex is typically atomic and singular in nature.
  4. A mutex can never be used as a semaphore.
  5. Mutex object lock is released only by the process that has acquired the lock on it.
  6. Mutex is an Object.
  7. Mutex if locked has to be unlocked by the same thread.
  8. Mutex object is locked or unlocked by the process requesting or releasing the resource.
  9. Only one thread can acquire a mutex at a time.
  10. Mutex works in userspace.
  11. The thread with mutex has ownership over the resource.
  12. Mutex does not have further categorization.
  13. If a mutex object is already locked, the process requesting for resources waits and queued by the system till lock is released.

Also Read: Difference Between Process And Thread In Os

Difference Between Semaphore And Mutex In Tabular Form

BASIS OF COMPARISON SEMAPHORE MUTEX
Description Semaphore is a signaling mechanism and a thread waiting on a semaphore can be signaled by another thread.   The Mutex is a locking mechanism that makes sure only one thread can acquire the mutex at a time and enter the critical section.  
Purpose Semaphore is for processes.   Mutex is for thread.  
Nature Semaphore is atomic but not singular in nature.   Mutex is typically atomic and singular in nature.  
Use A binary semaphore can be used as a mutex along with providing feature of signaling amongst threads.   A mutex can never be used as a semaphore.  
Release Of The Mechanism Semaphore value can be changed by any process acquiring or releasing the resource.   Mutex object lock is released only by the process that has acquired the lock on it.  
What They Are Semaphore is an integer variable.   Mutex is an Object.  
Unlocking If locked, a semaphore can be acted upon by different threads.   Mutex if locked has to be unlocked by the same thread.  
Thread & Process Only one process can acquire binary semaphore at a time but multiple processes can simultaneously acquire semaphore in case of counting semaphore.   Only one thread can acquire a mutex at a time.  
Working Semaphore works in kernel space.   Mutex works in userspace.  
Ownership Concept The concept of ownership is absent in semaphore.   The thread with mutex has ownership over the resource.  
Categorization Semaphore can be categorized into counting semaphore and binary semaphore.   Mutex does not have further categorization.  
The Process Requesting For Resources If all resources are being used, the process requesting for resource performs wait () operation and block itself till semaphore count become greater than one.   If a mutex object is already locked, the process requesting for resources waits and queued by the system till lock is released.  

Comments are closed.