DC Exp6
DC Exp6
Theory :
This experimental write-up covers the implementation and testing of the Bully Algorithm, a well-
known method for electing a coordinator (or leader) in distributed systems. The experiment
simulates a network of nodes communicating with each other to elect a leader based on their unique
identifiers. The focus is on understanding the algorithm's behavior, message passing, and handling
election scenarios.
Introduction
In distributed systems, having a leader or coordinator is often essential for managing shared
resources, coordination activities, and ensuring consistency. The Bully Algorithm, proposed by E.
G. Coffman Jr. and P. J. Denning, is one of the simplest and most effective algorithms for leader
election among a set of nodes.
Nodes with higher identifiers (IDs) can "bully" lower ones to defer elections.
A node can declare itself a leader if it detects no higher ID nodes responding.
Nodes communicate through messages to initiate elections and share coordination decisions.
Experimental Setup
Implementation
The Bully Algorithm is implemented in Python, using classes to represent nodes and simulate
communication between them. Each node can send and receive messages, start an election, and
declare itself a coordinator if no higher ID nodes are present.
1.Node Class:
Represents each node in the system.
Contains methods for sending and receiving messages, starting elections, and declaring
coordination.
2.Message Types:
ELECTION: Initiated by nodes seeking leadership.
ELECTION_REPLY: Response indicating that a higher ID node exists.
COORDINATOR: Indicates which node has been elected as the leader.
Experimental Procedure
1.Setup:
Create a network of 5 nodes, each with unique IDs ranging from 1 to 5.
Establish connections between all nodes to allow complete communication.
2.Election Initiation:
Start the election process by having node 1 initiate the election.
3.Message Passing Simulation:
Simulate communication delays to mimic real-world conditions.
Observe and log the messages exchanged between nodes.
4.Leader Declaration:
Monitor which node successfully declares itself as the leader and propagates this information to
others.
Results Collection
Log all messages exchanged and decisions made during the election process.
Note the time taken for the election to conclude and the node that becomes the coordinator.
Program :
import time
import random
class Process:
def _init_(self, process_id, is_failed=False):
self.process_id = process_id
self.is_failed = is_failed
# Election Process
for i in range(initiator, num_processes):
if not processes[i].is_failed and i + 1 < num_processes:
processes[i].send_election_message(processes[i + 1])
print("End Of Election")
Output :