Mutex in OS

 Mutex

The word "Mutex" stands for an object providing mutual exclusion between threads. Mutex ensures that only one thread has access to a critical section or data by using operations like a lock or unlock. 

    Mutex is a binary semaphore that synchronizes access to shared resources like memory or I/O. 

    In case multi threads want to access the critical section, mutex allows only one thread at a time to be in the critical section. Mutex ensures that the code in the critical section being controlled will be used by only a single thread at a time.

    Mutex is a mutual exclusion object that synchronizes access to a resource. It is created with a unique name at the start of a program. The Mutex is a locking mechanism that makes sure only one thread can acquire the Mutex at a time and enter the critical section. This thread only releases the Mutex when it exits the critical section.



   Advantages of Mutex

  • Mutex is nothing more than basic locks that must be obtained prior to entry in a critical section and subsequently released. 
  • Absence of race situations since a single thread is in the crucial region at a particular time, and data consistency is there.



Disadvantages of Mutex

  • The use of mutex can lead to starvation.
  • Locking or unlocking cannot be done from a different context.
  • A single thread is allowed in the critical section at any instance.
  • The standard implementation may result in a busy waiting state, wasting CPU time.