Mutual Exclusion In OS

Learn via video courses
Topics Covered

Overview

Mutual exclusion in OS locks is a frequently used method for synchronizing processes or threads that want to access some shared resource. Their work justifies their name, if a thread operates on a resource, another thread that wants to do tasks on it must wait until the first one is done with its process.

What is Mutual Exclusion in OS?

Mutual exclusion also known as Mutex is a unit of code that avert contemporaneous access to shared resources. Mutual exclusion is concurrency control’s property that is installed for the objective of averting race conditions.

In simple words, it's a condition in which a thread of execution does not ever get involved in a critical section at the same time as a concurrent thread of execution so far using the critical section. This critical section can be a period for which the thread of execution uses the shared resource which can be defined as a data object, that different concurrent threads may attempt to alter (where the number of concurrent read operations allowed is two but on the other hand two write or one read and write is not allowed, as it may guide it to data instability).

Mutual exclusion in OS is designed so that when a write operation is in the process then another thread is not granted to use the very object before the first one has done writing on the critical section after that releases the object because the rest of the processes have to read and write it.

Why is Mutual Exclusion Required?

An easy example of the importance of Mutual Exclusion can be envisioned by implementing a linked list of multiple items, considering the fourth and fifth need removal. The deletion of the node that sits between the other two nodes is done by modifying the previous node’s next reference directing the succeeding node.

In a simple explanation, whenever node “i” wants to be removed, at that moment node “ith - 1” 's next reference is modified, directing towards the node “ith + 1”. Whenever a shared linked list is in the middle of many threads, two separate nodes can be removed by two threads at the same time meaning the first thread modifies node “ith - 1” next reference, directing towards the node “ith + 1”, at the same time second thread modifies node “ith” next reference, directing towards the node “ith + 2”. Despite the removal of both achieved, linked lists required state is not yet attained because node “i + 1” still exists in the list, due to node “ith - 1” next reference still directing towards the node “i + 1”.

Now, this situation is called a race condition. Race conditions can be prevented by mutual exclusion so that updates at the same time cannot happen to the very bit about the list.

Necessary Conditions for Mutual Exclusion

There are four conditions applied to mutual exclusion, which are mentioned below :

  • Mutual exclusion should be ensured in the middle of different processes when accessing shared resources. There must not be two processes within their critical sections at any time.
  • Assumptions should not be made as to the respective speed of the unstable processes.
  • The process that is outside the critical section must not interfere with another for access to the critical section.
  • When multiple processes access its critical section, they must be allowed access in a finite time, i.e. they should never be kept waiting in a loop that has no limits.

Example of Mutual Exclusion

There are many types of mutual exclusion, some of them are mentioned below :

  • Locks :
    It is a mechanism that applies restrictions on access to a resource when multiple threads of execution exist.
  • Recursive lock :
    It is a certain type of mutual exclusion (mutex) device that is locked several times by the very same process/thread, without making a deadlock. While trying to perform the "lock" operation on any mutex may fail or block when the mutex is already locked, while on a recursive mutex the operation will be a success only if the locking thread is the one that already holds the lock.
  • Semaphore :
    It is an abstract data type designed to control the way into a shared resource by multiple threads and prevents critical section problems in a concurrent system such as a multitasking operating system. They are a kind of synchronization primitive.
  • Readers writer (RW) lock :
    It is a synchronization primitive that works out reader-writer problems. It grants concurrent access to the read-only processes, and writing processes require exclusive access. This conveys that multiple threads can read the data in parallel however exclusive lock is required for writing or making changes in data. It can be used to manipulate access to a data structure inside the memory

Conclusion

  • Mutual exclusion in OS locks is a frequently used method for synchronizing processes or threads that want to access some shared resource.
  • Mutual exclusion is also known as Mutex.
  • The critical section can be defined as a period for which the thread of execution accesses the shared resource.
  • Mutual exclusion is designed so that if a process is performing any write operation, then no other process or thread is allowed to access or alter the same object.
  • Mutual exclusion in OS is used to prevent race conditions.
  • Mutual exclusion should be ensured in the middle of different processes when accessing shared resources.
  • The process that is outside the critical section must not interfere with another for access to the critical section.
  • Examples of mutual exclusion are locks, recursive locks, RW locks, semaphores, etc.