What is Mutex?

Mutex

  • The simplest of the software tool to solve critical section problem is the mutex lock. (It is short form for mutual exclusion.)
  • We use the mutex lock to protect critical regions and thus prevent race conditions.
  • That is, a process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section.
  • The acquire() function acquires the lock, and the release() function releases the lock mutex lock has a boolean variable available whose value indicates if the lock is available or not.
  •  If the lock is available, a call to acquire() succeeds, and the lock is then considered unavailable. A process that attempts to acquire an unavailable lock is blocked until the lock is released.
  • Calls to either acquire() or release() must be performed atomically.
  • The definition of acquire() is as follows :acquire() {
    while (!available); // busy wait
    available = false;
    }
  • The definition of release() is as follows:release() {
    available = true;
    }
  • The main disadvantage of the implementation given here is that it requires busy waiting. While a process is in its critical section, any other process that tries to enter its critical section must loop continuously in the call to acquire().
  • This type of mutex lock is also called a spinlock because the process “spins” while waiting for the lock to become available.

Solving Critical Section problem using Mutex Locks :

do {
      acquire lock
critical section
      release lock
remainder section
}
while (true);

Leave a Reply

Your email address will not be published. Required fields are marked *