Skip to Content

Semaphore vs Mutex Differences and Comparison

Semaphore vs Mutex Differences and Comparison
Semaphore vs Mutex Differences and Comparison

Overview : Semaphore vs Mutex

The software based solutions such as Peterson’s don’t seem to be guarantee to figure on modern computer architectures. Instead we are able to generally state that any solution to the critical selection problem requires a straightforward tool a lock. Race conditions are prevented by requiring that critical regions be protected by locks that is process must acquire a lock before entering a critical selection releases the lock when it exits the critical selection. So here we introduced two tools they’re semaphores and mutex.

What is Semaphore?

Semaphores in operating systems are the integer variable that are used to solve critical selection problem using two atomic operations they are wait() and signal().

The wait() operation was originally termed P to signal() was originally called V.
The definition of wait() is as follows:

 Wait(s) 
 { 
 While s0) likewise as its possible modification(S--),must be executed 
 without interruption. 

Forms of Semaphores

Operating systems are distinguished as counting Semaphore and binary semaphores. the worth of a counting semaphore can range over an unrestricted domain .

Binary semaphore

The worth of a binary semaphore can range only between 0 and 1 .On some systems binary semaphores are called mutex locks as there are laws that provide mutual conclusion. we are able to use binary semaphore posts to traumatize critical selection problems for multiple processes. The “n” process shares a semaphore and mutex initialized to 1 . Each process is organized.

Counting Semaphore

Counting semaphore will be accustomed control access to a given resource consisting of a finite number of instances. The semaphore is initialized to number of resources available. Each process that wishes to use a resource performs a wait operation on a semaphore. When a process releases a resource it performs a sign operation when a count for semaphores goes to zero resources are being employed.

we are able to also use semaphore to resolve various synchronization problems as an example we will cure yes to be executed only after S1 has coae can implement this came really by letting P1 and P2 share a typical simmer for synchronization initial eyes to visualize row and so by inserting these statements.

S1; 
 Signal(synch); 

 within the process P1 and also the statement 

 Wait(synch); 
 S2; 

In process P2. Because sync is initialized to 0 and P2 will execute S2 only after P1 has invoked
the signal which is after statement 1 has been executed.

Implementation of Semaphore

Implementation of semaphores under this definition a “C ” struct:

type struct
{
Int value;
Struct process *list;
}
Semaphore;

Each semaphore has an integer value and a listing of process lists. When a process must attend a semaphore is added to the list of processes when a signal() operation removes one process from the list of the waiting processes and awakens that process.

The wait () semaphores will be now defined as

Wait( semaphore *S)
{
S-->value--;
If(S-->value list;
block();
}
}

The signal() Semaphore operation will be defined as

Signal(Semaphore *s)
{
S->value++;
If(S->value list;
Wakeup(P);
}
}

Advantages of Semaphore

Semaphores allow only 1 process into the critical section.

They follow the mutual exclusion principle strictly and are rather more efficient than other methods of synchronization; there’s no resource wastage attributable to busy waiting in semaphores as processor time isn’t wasted unnecessarily to visualize if a condition is fulfilled to permit a process to access the critical section.

Disadvantages of Semaphore

The most disadvantage of the semaphore definition is that it requires busy writing. While a process is in its critical section, any other process that tries to enter it’s vital section must loop continuously within the entry code. This continual looping is clearly a print in an exceedingly real multiprogramming system.

do
{
Wait(mutex);
signal (mutex);
while (TRUE);
}

What is Mutex?

Mutex follows the principle of locking and releasing mechanism. The best example is by considering a Doctor’s consultation room where a patient walks in close the door and after his/her consultation the patient walks out and offers the possibility for successive person to enter.

From the above example it’s clearly understood that only 1 thread can accrue at a time.

Advantages of Mutex

  • Binary Semaphore and mutex are different
  • Mutex provides locking mechanism
  • Mutex is Binary in nature
  • Mutex works in user-space and Semaphore for kernel
  • Mutex is for Threads, while Semaphores are for processes.
  • Operations like Lock and Release are possible
  • A thread may acquire over one mutex

Disadvantages of Mutex

The disadvantage of mutex is that it allows just one thread that’s when in some cases if there is another process it automatically gets blocked. Hence causes deadlock.

Implementation of Mutex

Mutex locks are a sort of a dispatcher object. The following example are illustrating way to create a mutex lock using the CreteMutex() function

#include
HANDLE mutex;
Mutex= CreateMutex(Null,FALSE,NULL);

The primary parameter refers to a security attribute for the mutex lock. By setting this attribute to NULL, we are disallowing any children of the method creating this mutex lock to inherit the handle of the mutex.The second parameter indicator whether the creator of the mutex is that the initial owner of the mutex lock. Passing a price of FALSE indicates that the thread creating the mutex isn’t initial owner. The third parameter allow the name of the mutex. however, we provide a worth of Null we don’t name the mutex lock; otherwise, it returns NULL.

Comparison Table:

Comparison HeadSemaphoreMutex
MechanismA semaphore follows a signalling mechanism  The mutex in the operating system follows locking mechanism
OperationThe semaphore value is modified using the wait () and the signal () operation.  In mutex an object is locked or unlocked when the process is requesting or releasing the resource
ExistenceSemaphore exists as an integer variable  Mutex exist as an object
FunctionIn semaphore it allows multiple program threads to access a finite instance of resources  Allows multiple program file to access a single thread
ClassificationThe semaphore can be classified as counting & binary semaphore.  There does not exist any classification
OwnershipThe value could be changed by any process acquiring or by releasing the resourcesLock is released only by the lock that has acquired in it.