- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lamportís Algorithm for Mutual Exclusion in Distributed System
Multiple processes that are running on various machines or nodes and interacting with one another to accomplish a single objective makeup distributed systems. In these systems, it's crucial to make sure that only one process is able to utilize a shared resource at once to prevent conflicts and data inconsistencies.
One way to make sure that only one function is using a shared resource at once is through the use of mutual exclusion and Lamport's Algorithm is one of many accessible mutual exclusion algorithms.
A centralized mutual exclusion algorithm called Lamport's Algorithm employs timestamps to determine the sequence in which requests are made to access a shared resource. The requests and communications that each process sends to other processes are time-stamped using the logical clock that each process keeps track of. A request message with the timestamp is sent to all other processes by a process when it needs to utilize a resource that is shared. When a process gets a request message, it determines whether it has already given another process access to the shared resource. If not, it compares the timestamp of the received data with its own and gives access to the process with the earliest timestamp.
Only one process can access the shared resource at once because the algorithm makes sure that requests are handled in the sequence of their timestamps. It does, however, presuppose that processes have synchronized clocks and that signals are sent on time, which may not always be the case in distributed systems in the real world.
Pseudo code of Lamportís Algorithm for Mutual Exclusion in Distributed System
Lamport's algorithm for mutual exclusion in distributed systems has the following pseudo code −
When a process wants to access the shared resource
requesting = true; clock = clock + 1; send_request_message_to_all_other_processes(clock); wait_for_replies_from_all_other_processes();
When a process receives a request message from another process
receive_request_message(clock); update_clock(clock); if (!requesting || (clock, this_process_id) < (requesting_clock, requesting_process_id)) send_reply_message(sender_process_id); else queue_request(sender_process_id, sender_clock);
When a process receives a reply message from another process
receive_reply_message(sender_process_id); update_clock(sender_clock); num_replies_received = num_replies_received + 1; if (num_replies_received == num_processes - 1) accessing_shared_resource = true;
When a process finishes accessing the shared resource
accessing_shared_resource = false; num_replies_received = 0; for each queued request send_reply_message(requesting_process_id); dequeue_request();
A step-by-step process for Lamport's Algorithm for Mutual Exclusion in Distributed Systems −
A logical clock that is originally set to 0 is kept by each process, along with a boolean variable called requesting that denotes whether the process desires access to the shared resource.
A process increases its logical clock value by 1 and sets the request to true when it wishes to use a shared resource.
All other processes in the system are then sent a request notification by the process. The logical clock number for the process is contained in the request message.
A process updates its own logical clock value to be the highest of its existing logical clock value and the clock value in the incoming request message when it gets a request message from another process.
A reply message is sent to the requesting process if the receiving process is not presently using the shared resource. There is no more detail in the reply message.
The request communication is queued if the receiving process is already using the shared resource.
A process updates its own logical clock value to be the maximum of its existing logical clock value and the clock value in the received reply message when it gets a reply message from another process. A property called "num_replies_received" is also incremented.
The process sets a flag to indicate that it is accessing the shared resource if "num_replies_received" matches the total number of processes in the system minus 1 (i.e., all processes excluding the requesting process).
The shared resource is now accessible to the workflow. When finished, it transmits a reply message to each request that was in line, sorted by timestamp. (i.e., the order in which they were received).
The procedure then resets num_replies_received to 0 and sets requesting to false.
Each time a process needs to access the shared resource steps 2 through 10 can be repeated as often as required.
Here are some of the advantages of Lamport's Algorithm for Mutual Exclusion in Distributed Systems −
Easy to understand − The Lamportis Algorithm is straightforward and simple to understand. This makes it an excellent option for a variety of applications.
Scalable − This algorithm does not usually depend on any server or coordinator in the middle of the system. Hence, it can be used in systems with many processes.
Fairness − This algorithm makes sure that every process has an equal opportunity to utilize the shared resource. This is because the algorithm handles the processes in the same order that it receives.
Low latency − Because mutual exclusion is achieved after only one round of contact, the algorithm has low latency.
Works in asynchronous environments − The algorithm operates in asynchronous environments where the timing of message transmission is unpredictable.
Here are some of the disadvantages of Lamport's Algorithm for Mutual Exclusion in Distributed Systems −
The algorithm necessitates frequent contact between processes, which could lead to a high message overhead.
The algorithm may be ineffective in situations with high levels of contention, such as when several processes are vying for access to the same resource at once, as it may take several rounds of conversation to settle disputes.
The algorithm implies that all processes' clocks are synchronized, which can be difficult to achieve in practice.
Limited to exclusive access: The algorithm cannot be easily modified to support other kinds of access control because it is built specifically for achieving mutual exclusion.
The algorithm is susceptible to network failures, which can result in delays or lost communications.
Without using a central coordinator or server, Lamport's Algorithm for Mutual Exclusion in Distributed Systems offers a quick and efficient way to accomplish mutual exclusion in a distributed setting. The algorithm ensures fairness and avoids starvation because it is scalable and functional in asynchronous settings. It may, however, experience high message overhead, inefficiency in situations of high contention, difficulties with time synchronization, and susceptibility to network outages.
Kickstart Your Career
Get certified by completing the courseGet Started