The Wayback Machine - https://web.archive.org/web/20240901154140/https://www.geeksforgeeks.org/mutual-exclusion-in-synchronization/
Open In App

Mutual Exclusion in Synchronization

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

During concurrent execution of processes, processes need to enter the critical section (or the section of the program shared across processes) at times for execution. It might happen that because of the execution of multiple processes at once, the values stored in the critical section become inconsistent. In other words, the values depend on the sequence of execution of instructions – also known as a race condition. The primary task of process synchronization is to get rid of race conditions while executing the critical section.

What is Mutual Exclusion?

Mutual Exclusion is a property of process synchronization that states that “no two processes can exist in the critical section at any given point of time“. The term was first coined by Dijkstra. Any process synchronization technique being used must satisfy the property of mutual exclusion, without which it would not be possible to get rid of a race condition. 

The need for mutual exclusion comes with concurrency. There are several kinds of concurrent execution:

  • Interrupt handlers
  • Interleaved, preemptively scheduled processes/threads
  • Multiprocessor clusters, with shared memory
  • Distributed systems

Mutual exclusion methods are used in concurrent programming to avoid the simultaneous use of a common resource, such as a global variable, by pieces of computer code called critical sections.

The requirement of mutual exclusion is that when process P1 is accessing a shared resource R1, another process should not be able to access resource R1 until process P1 has finished its operation with resource R1.

Examples of such resources include files, I/O devices such as printers, and shared data structures.

Conditions Required for Mutual Exclusion

According to the following four criteria, mutual exclusion is applicable:

  • When using shared resources, it is important to ensure mutual exclusion between various processes. There cannot be two processes running simultaneously in either of their critical sections.
  • It is not advisable to make assumptions about the relative speeds of the unstable processes.
  • For access to the critical section, a process that is outside of it must not obstruct another process.
  • Its critical section must be accessible by multiple processes in a finite amount of time; multiple processes should never be kept waiting in an infinite loop.

Approaches To Implementing Mutual Exclusion

  • Software Method: Leave the responsibility to the processes themselves. These methods are usually highly error-prone and carry high overheads.
  • Hardware Method: Special-purpose machine instructions are used for accessing shared resources. This method is faster but cannot provide a complete solution. Hardware solutions cannot give guarantee the absence of deadlock and starvation.
  • Programming Language Method: Provide support through the operating system or through the programming language.

Requirements of Mutual Exclusion

  • At any time, only one process is allowed to enter its critical section.
  • The solution is implemented purely in software on a machine.
  • A process remains inside its critical section for a bounded time only.
  • No assumption can be made about the relative speeds of asynchronous concurrent processes.
  • A process cannot prevent any other process from entering into a critical section.
  • A process must not be indefinitely postponed from entering its critical section.

In order to understand mutual exclusion, let’s take an example. 

What is a Need of Mutual Exclusion?

An easy way to visualize the significance of mutual exclusion is to imagine a linked list of several items, with the fourth and fifth items needing to be removed. By changing the previous node’s next reference to point to the succeeding node, the node that lies between the other two nodes is deleted.

To put it simply, whenever node “i” wants to be removed, node “with – 1″‘s subsequent reference is changed to point to node “ith + 1” at that time. Two distinct nodes can be removed by two threads at the same time when a shared linked list is being used by many threads. This occurs when the first thread modifies node “ith – 1” next reference, pointing towards the node “ith + 1,” and the second thread modifies node “ith” next reference, pointing towards the node “ith + 2.” Although both nodes have been removed, the linked list’s required state has not yet been reached because node “i + 1” still exists in the list because node “ith – 1” next reference still points to it.

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.

Example:

In the clothes section of a supermarket, two people are shopping for clothes.

Mutual-Exclusion Example

Boy, A decides upon some clothes to buy and heads to the changing room to try them out. Now, while boy A is inside the changing room, there is an ‘occupied’ sign on it – indicating that no one else can come in. Boy B has to use the changing room too, so she has to wait till boy A is done using the changing room. 

Mutual-Exclusion-Example

Once boy A comes out of the changing room, the sign on it changes from ‘occupied’ to ‘vacant’ – indicating that another person can use it. Hence, boy B proceeds to use the changing room, while the sign displays ‘occupied’ again.

The changing room is nothing but the critical section, boy A and boy B are two different processes, while the sign outside the changing room indicates the process synchronization mechanism being used.

Mutual-Exclusion Example

Conclusion

In conclusion, mutual exclusion is a key concept in synchronization that ensures only one process accesses a shared resource at a time. This prevents conflicts and data corruption, making sure that processes run smoothly and correctly. By using mutual exclusion mechanisms, we can create stable and reliable systems that handle multiple processes efficiently.

Frequently Asked Questions on Mutual Exclusion in Synchronization – FAQs

What is a race condition?

A race condition occurs when multiple processes or threads access shared data concurrently, and the final outcome of the program depends on the relative timing of their execution. This can lead to unexpected and undesired results, such as data corruption, incorrect calculations, or program crashes.

How can race conditions be avoided?

Race conditions can be avoided by implementing mutual exclusion techniques, such as locks, semaphores, or monitors. These techniques ensure that only one process or thread can access a shared resource at a time, preventing simultaneous conflicting accesses and maintaining data integrity.

What is a deadlock?

A deadlock is a situation in concurrent programming where two or more processes or threads are unable to proceed because each is waiting for the other to release a resource. As a result, the processes or threads are stuck in a state of waiting indefinitely, leading to a system freeze or unresponsiveness.



Previous Article
Next Article

Similar Reads

Performance Metrics For Mutual Exclusion Algorithm
Mutual exclusion is a program object that refers to the requirement of satisfying that no two concurrent processes are in a critical section at the same time. It is presented to intercept the race condition. If a current process is accessing the critical section then it prevents entering another concurrent process there. So, in a single word, just
4 min read
Inclusion-Exclusion and its various Applications
In the field of Combinatorics, it is a counting method used to compute the cardinality of the union set. According to basic Inclusion-Exclusion principle: For 2 finite sets [Tex]A_1 [/Tex]and [Tex]A_2 [/Tex], which are subsets of Universal set, then [Tex](A_1-A_2), (A_2-A_1) [/Tex]and [Tex](A_1\bigcap A_2) [/Tex]are disjoint sets. Hence it can be s
7 min read
Print numbers in sequence using thread synchronization
The problem is to synchronize n number of threads using pthread library. The idea is take thread count and print 1 in first thread, print 2 in second thread, print 3 in third thread, ..... print n in nth thread and again repeat from thread 1 infinitely. Prerequisite : Multithreading Examples : Input : Thread count Output : 1 2 3 ... thread count 1
2 min read
Priority Inheritance Protocol (PIP) in Synchronization
Prerequisite - Introduction of Process Synchronization Priority Inheritance Protocol (PIP) is a technique which is used for sharing critical resources among different tasks. This allows the sharing of critical resources among different tasks without the occurrence of unbounded priority inversions. Basic Concept of PIP : The basic concept of PIP is
3 min read
Centralized Clock Synchronization
Centralized Clock Synchronization is an internal clock synchronization approach where clocks of system are synchronized with one of clock of system. Clock synchronization is carried out to make all clocks in network agree on the same value. In centralized clock synchronization, one of clocks is appointed as master clock. Other clock of the system a
2 min read
Hardware Synchronization Algorithms : Unlock and Lock, Test and Set, Swap
Process Synchronization problems occur when two processes running concurrently share the same data or same variable. The value of that variable may not be updated correctly before its being used by a second process. Such a condition is known as Race Around Condition. There are a software as well as hardware solutions to this problem. In this articl
5 min read
FIFO Barbershop in Process synchronization
Overview :In process synchronization, there exists a Sleeping Barber problem, as discussed here. But in the above solution, there is no guarantee that the customers are served in the order they arrive in the barbershop, i.e. there is no guarantee that the customers enter a FIFO manner(First-in First-out). In the solution discussed above. Up to n cu
5 min read
Critical Section in Synchronization
In computer science, a critical section refers to a segment of code that is executed by multiple concurrent threads or processes, and which accesses shared resources. These resources may include shared memory, files, or other system resources that can only be accessed by one thread or process at a time to avoid data inconsistency or race conditions
9 min read
Classical Problems of Synchronization with Semaphore Solution
In this article, we will see a number of classical problems of synchronization as examples of a large class of concurrency-control problems. In our solutions to the problems, we use semaphores for synchronization, since that is the traditional way to present such solutions. However, actual implementations of these solutions could use mutex locks in
4 min read
Monitors in Process Synchronization
Monitors are a higher-level synchronization construct that simplifies process synchronization by providing a high-level abstraction for data access and synchronization. Monitors are implemented as programming language constructs, typically in object-oriented languages, and provide mutual exclusion, condition variables, and data encapsulation in a s
3 min read
Lock Variable Synchronization Mechanism
Prerequisites - Process SynchronizationA lock variable provides the simplest synchronization mechanism for processes. Some noteworthy points regarding Lock Variables are- Its a software mechanism implemented in user mode, i.e. no support required from the Operating System.Its a busy waiting solution (keeps the CPU busy even when its technically wai
5 min read
Process Synchronization | Set 2
Prerequisite - Process Synchronization | Introduction , Critical Section , SemaphoresProcess Synchronization is a technique which is used to coordinate the process that use shared Data. There are two types of Processes in an Operating Systems:- Independent Process - The process that does not affect or is affected by the other process while its exec
5 min read
Peterson's Algorithm in Process Synchronization
Prerequisite - Synchronization, Critical Section  The producer-consumer problem (or bounded buffer problem) describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue. Producers produce an item and put it into the buffer. If the buffer is already full then the producer will have to wait for an em
15+ min read
Bakery Algorithm in Process Synchronization
Prerequisite - Critical Section, Process Synchronization, Inter Process Communication The Bakery algorithm is one of the simplest known solutions to the mutual exclusion problem for the general case of N process. Bakery Algorithm is a critical section solution for N processes. The algorithm preserves the first come first serve property. How does th
13 min read
Dekker's algorithm in Process Synchronization
Prerequisite - Process Synchronization, Inter Process Communication To obtain such a mutual exclusion, bounded waiting, and progress there have been several algorithms implemented, one of which is Dekker's Algorithm. To understand the algorithm let's understand the solution to the critical section problem first. A process is generally represented a
15+ min read
Introduction of Process Synchronization
Process Synchronization is the coordination of execution of multiple processes in a multi-process system to ensure that they access shared resources in a controlled and predictable manner. It aims to resolve the problem of race conditions and other synchronization issues in a concurrent system. The main objective of process synchronization is to en
9 min read
Synchronization Examples
The Synchronization is an important concept in operating systems that ensures the smooth and coordinated execution of processes and threads. It is the task of coordinating the execution of processes in such a way that no two processes can access the same shared data and resource. It is a critical part of operating system design which ensures that p
6 min read
Synchronization in Distributed Systems
Synchronization in distributed systems is crucial for ensuring consistency, coordination, and cooperation among distributed components. It addresses the challenges of maintaining data consistency, managing concurrent processes, and achieving coherent system behavior across different nodes in a network. By implementing effective synchronization mech
11 min read
Semaphores in Process Synchronization
Semaphores are a tool used in computer science to help manage how different processes (or programs) share resources, like memory or data, without causing conflicts. A semaphore is a special kind of synchronization data that can be used only through specific synchronization primitives. Semaphores are used to implement critical sections, which are re
15+ min read
DBMS Tutorial – Learn Database Management System
Database Management System is a software or technology used to manage data from a database. Some popular databases are MySQL, Oracle, MongoDB, etc. DBMS provides many operations e.g. creating a database, Storing in the database, updating an existing database, delete from the database. DBMS is a system that enables you to store, modify, and retrieve
8 min read
Operating System Tutorial
An Operating System(OS) is software that manages and handles the hardware and software resources of a computer system. It provides interaction between users of computers and computer hardware. An operating system is responsible for managing and controlling all the activities and sharing of computer resources. An operating system is a low-level Soft
7 min read
Computer Network Tutorial
A computer network is a collection of computers or devices connected to share resources. Any device which can share or receive the data is called a Node. Through which the information or data propagate is known as channels, It can be guided or unguided. In this Computer network tutorial, you’ll learn basic to advanced concepts like the Basics of co
10 min read
What is an Operating System?
Operating System lies in the category of system software. It basically manages all the resources of the computer. An operating system acts as an interface between the software and different parts of the computer or the computer hardware. The operating system is designed in such a way that it can manage the overall resources and operations of the co
9 min read
Types of Operating Systems
An Operating System performs all the basic tasks like managing files, processes, and memory. Thus, the operating system acts as the manager of all the resources, i.e. resource manager. Thus, the operating system becomes an interface between the user and the machine. It is one of the most required software that is present in the device. Operating Sy
11 min read
TCP/IP Model
The TCP/IP model is a fundamental framework for computer networking. It stands for Transmission Control Protocol/Internet Protocol, which are the core protocols of the Internet. This model defines how data is transmitted over networks, ensuring reliable communication between devices. It consists of four layers: the Link Layer, the Internet Layer, t
13 min read
What is OSI Model? - Layers of OSI Model
OSI stands for Open Systems Interconnection , where open stands to say non-proprietary. It is a 7-layer architecture with each layer having specific functionality to perform. All these 7 layers work collaboratively to transmit the data from one person to another across the globe. The OSI reference model was developed by ISO - 'International Organiz
15+ min read
Types of Network Topology
Network topology refers to the arrangement of different elements like nodes, links, or devices in a computer network. It defines how these components are connected and interact with each other. Understanding various types of network topologies helps in designing efficient and robust networks. Common types include bus, star, ring, mesh, and tree top
11 min read
ACID Properties in DBMS
A transaction is a single logical unit of work that accesses and possibly modifies the contents of a database. Transactions access data using read and write operations. In order to maintain consistency in a database, before and after the transaction, certain properties are followed. These are called ACID properties. For those looking to master thes
6 min read
Introduction of ER Model
Peter Chen developed the ER diagram in 1976. The ER model was created to provide a simple and understandable model for representing the structure and logic of databases. It has since evolved into variations such as the Enhanced ER Model and the Object Relationship Model The Entity Relational Model is a model for identifying entities to be represent
10 min read
Normal Forms in DBMS
Normalization is the process of minimizing redundancy from a relation or set of relations. Redundancy in relation may cause insertion, deletion, and update anomalies. So, it helps to minimize the redundancy in relations. Normal forms are used to eliminate or reduce redundancy in database tables. Normalization of DBMSIn database management systems (
12 min read