• Operating System Video Tutorials

Mutual Exclusion in Synchronization



What is Mutual Exclusion?

Mutual exclusion implies that only one process can be inside the critical section at any time. If other processes need to execute in their critical sections, they must wait until it is free.

When more than one process wants to access or update a shared resource, i.e. get in their critical sections, if they execute simultaneously, race condition may occur leading to inconsistent results. In order to avoid race condition, the process synchronization technique should satisfy the mutual exclusion criteria.

Explanation

A process often needs to perform operations like accessing or updating shared resources like a common variable, a table of indexes or a shared file. When the process is doing so, it is said to be executing in its critical section. In the critical section, if the executing process is interrupted by another process which updates the shared resource in a different way, race condition occurs and the value of the shared resource become invalid.

Synchronization mechanisms are techniques that ensure that processes are executed in such a manner so that the state of the system and common data structures are consistent at all times. In order to avoid race conditions, the synchronization techniques make sure that the instructions of the operations in the critical section are executed as a single unit, i.e. in an atomic manner. Atomicity is guaranteed through mutual exclusion, through which only one process can execute in the critical section at a particular time.

Requisites of Mutual Exclusion

  • Only one process can enter its critical section at a particular time.
  • A process is permitted to execute in its critical section only for a bounded time.
  • No process (which is not in its critical section) can prevent another process from entering into its critical section.
  • No process must wait indefinitely to enter its critical section.

Techniques of Mutual Exclusion in Synchronization

Mutual exclusion can be achieved using a variety of strategies, such as the following −

Locks / Mutex

Synchronization primitives called locks or Mutex (short for mutual exclusion) are implemented to maintain consistency of the value or status of the shared resources. There are two possible states for a lock: locked and unlocked. An operating system or procedure must obtain the lock before being able to utilize the resource that is shared. The requesting string is going to be restricted as long as the lock has been released if it has already been locked by a distinct thread.

Semaphore

Semaphore is an advanced synchronization tool. It uses two atomic operations, "wait()" and "signal()". The wait() instruction is used in the entry section to gain access to the critical section, while the signal() operation is used to release control of the shared resource. Semaphores can be of two types, binary semaphores and counting semaphores. In a counting semaphore, when a thread needs to enter the critical area, semaphores keep track of a counter and decrease it. The running thread becomes immobilized if the counter decreases, signifying that the critical portion has become in use.

Atomic Operations

Without using locks or semaphores, certain processors offer atomic operations which may be employed to guarantee mutual exclusion. Atomic operations are advantageous for modifying shared parameters because they are unbreakable and can't be stopped. A property of a variable may only be altered using atomic compare-and-swap (CAS) procedures, for instance, if the value of the variable coincides with the value that is anticipated.

Software-based Techniques

Mutual exclusion can be achieved using a variety of software-based algorithms and strategies, including Peterson's algorithm, Dekker's algorithm, or Lamport's bakery algorithm. These techniques make a guarantee that only a single thread at one point is able to utilize the critical component by combining factors, flags, and busy-waiting.

Practical Instances of Mutual Exclusion in Synchronization

A few instances of mutual exclusion in synchronization that can occur in real life −

Printer Spooling

Several procedures or individuals may ask for printed documents at once in an OS with a number of users. Mutual exclusion is used to guarantee that just one process at the moment has access to the printer. In order to provide restricted access to the printer, avoid conflicts, and guarantee that printed positions are dealt with in the proper order, a lock or semaphore is used.

Bank Account Transactions

Many people may simultaneously try to obtain and alter their financial accounts in an electronic banking system. In order to avoid problems like overloading or erratic accounts, mutual exclusion is required. A single transaction is allowed to access a certain bank account at a time using locks or other synchronization primitives, maintaining the confidentiality of the information and avoiding conflicts.

Traffic Signal Control

Traffic signals at a crosswalk must be coordinated in order to safely manage the movement of transport vehicles. In order to avoid competing communication from being displayed at once, mutual exclusion is used. One indicator is allowed to be present at a time thanks to the mutual exclusion rule, which promotes efficient and organized traffic flow.

Resource Allocation in Shared Database

Mutual exclusion is essential to preserving information consistency in database systems where various procedures or transactions access information that is shared concurrently. For instance, mutual exclusion mechanisms make absolutely certain that just a single transaction is able to alter identical data at a time, hindering disagreements and maintaining data integrity when two separate operations try to alter the same information concurrently.

Accessing Shared Memory in Real-Time Systems

Mutual exclusion is required in real-time systems in which operations or procedures require shared memory to facilitate interaction or cooperation. Important memory-sharing regions are protected using synchronization basic functions like locks or semaphores, which make sure that only a single assignment is able to use and alter the area of shared memory at once.

Advertisements