Mutex in OS

Learn via video courses
Topics Covered

Overview

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 and unlock. A thread having the lock of mutex can use the critical section while other threads must wait till the lock is released.

What is Mutex?

Mutex is special a binary semaphore that synchronizes the access to shared resources like memory or I/O. It is a locking mechanism.

Use of Mutex

In case multiple 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 (which has shared resources) being controlled will only be used by a single thread at a time.

Uses of Mutex

Mutex tries to solve the famous Producer Consumer Problem. As it ensures mutual exclusion, either the producer or consumer has their key(mutex), so at a given time either the producer fills the buffer or the consumer consumes it but both cannot occur at once.

Mutex Producer Consumer Problem

Note: Buffer has N slots to store N units of data.

Thundering Herd Problem

A large number of processes or threads waiting for an event are awoken when that event occurs, but only one process is able to proceed further.

  • All waiting processes wake up and move to the ready queue.
  • Leading to several context switches
  • Only one of the awoken processes goes to the critical section, while the rest go back to sleep.

Two problems occur:-

  • A large number of context switches
  • Could lead to starvation

The Solution:

  • When entering the critical section, push into a queue before blocking
  • When exiting the critical section, wake up only the first process in the queue.

Locks and Priorities

Suppose we have a high-priority task and a low-priority task that share the same data and have a critical section.

Suppose the low-priority task is executed in the critical section. Now, a high-priority task comes and requests for locks to enter into the critical section. Then we face the dilemma of having a high-priority task waiting for a low-priority task to release the lock and this is called Priority Inversion Problem.

Solution

Priority inheritance: When a low-priority task is in a critical section and a high-priority task requests for that critical section, we face priority inversion problems. To solve this, we make the priority of low-priority tasks equal to that of high-priority.

Advantages of Mutex

  • Mutexes are 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.

Advantages of Mutex

Disadvantages of Mutex

  • Use of mutex can lead to Starvation.
    • eg: A thread on gaining lock enters the critical section, but if this thread gets preempted or goes to sleep, then the other process cannot enter the critical section as the other process will wait till the lock is released.
  • 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.

Conclusion

  • Mutex, a special binary semaphore, is a locking mechanism that synchronizes access to shared resources.
  • Thundering herd problem occurs when a large number of processes wake up when the event occurs then all processes go back to sleep except for one.
  • It results in many context switches and could lead to starvation. This is solved using a queue.
  • Sometimes a high-priority process requests for the lock when a low-priority process executes in a critical section resulting in priority inversion.
  • Priority inversion is solved by using priority inheritance where a low-priority task is escalated to high priority.
  • Mutexes ensure data consistency and avoid race conditions but it can also lead to starvation and only 1 thread is used at a time.