Chap 3 DC
Chap 3 DC
Figure . (a) Three processes, each with its own clock. The clocks run at different
rates.
Figure (b) Lamport’s algorithm corrects the clocks.
2. Vector Clocks
o Each process maintains a vector VC[i] for n processes.
o VC[i] represents the knowledge of events from other processes.
o When an event occurs locally: VC[i]++
o When sending a message: The sender includes its vector clock.
o When receiving a message: VC = pairwise-max(VC, received_VC),
then VC[i]++.
o Provides causality tracking and helps in detecting concurrent
events.
3. Election Algorithms
Election algorithms help select a coordinator in distributed systems.
Types of Election Algorithms:
1. Bully Algorithm
o The process with the highest ID initiates the election.
o It sends an ELECTION message to all higher-ID processes.
o If no higher process responds, it wins and becomes the new
leader.
o If a higher process responds, it takes over the election.
o The new leader announces its victory to all processes.
2. Ring Algorithm
o Processes are arranged in a logical ring.
o A process detecting coordinator failure sends an election message
to its neighbor.
o Each node adds its ID and forwards the message.
o When the message returns to the sender, the highest ID is chosen
as the leader.
o The leader then broadcasts its identity.
4. Lamport Mutual Exclusion Algorithm
A distributed approach to mutual exclusion using logical timestamps.
Steps:
1. Requesting the Critical Section (CS)
o A process Pi sends a REQUEST message (timestamp, Pi) to all
processes.
o Each receiving process places the request in a queue and sends a
REPLY.
2. Executing the CS
o Pi enters CS only when:
▪ It has received replies from all other processes.
▪ Its request is the oldest in the queue.
3. Releasing the CS
o When Pi exits CS, it sends a RELEASE message to all.
o Each process removes Pi's request from its queue.
Performance:
5. Ricart-Agrawala Algorithm
A non-token-based mutual exclusion algorithm optimizing message overhead.
Steps:
1. Requesting the CS
o A process sends a request (timestamp, process ID) to all processes.
o If a process is not in CS and not requesting, it sends an OK reply.
o If in CS, it queues the request.
o If also requesting, it compares timestamps:
▪ Lower timestamp wins.
▪ The process with a higher timestamp waits.
2. Executing the CS
o A process enters CS after receiving OK from all processes.
3. Releasing the CS
o It sends OK messages to all waiting processes.
1. A process wanting to enter critical sections (cs) sends a message with (cs
name, process id, current time) to all processes (including itself).
2. When a process receives a cs request from another process, it reacts
based on its current state with respect to the cs requested. There are
three possible cases:
3. If the receiver is not in the cs and it does not want to enter the cs, it
sends an OK message to the sender.
4. If the receiver is in the cs, it does not reply and queues the request.
5. If the receiver wants to enter the cs but has not yet, it compares the
timestamp of the incoming message with the timestamp of its message
sent to everyone. {The lowest timestamp wins.} If the incoming
timestamp is lower, the receiver sends an OK message to the sender. If
its own timestamp is lower, the receiver queues the request and sends
nothing.
6. After a process sends out a request to enter a cs, it waits for an OK from
all the other processes. When all are received, it enters the cs.
7. Upon exiting cs, it sends OK messages to all processes on its queue for
that cs and deletes them from the queue.