0% found this document useful (0 votes)
10 views3 pages

DC Exp6

This document details the implementation of the Bully Algorithm for leader election in distributed systems, focusing on nodes with unique identifiers. The experiment involves simulating a network of nodes that communicate to elect a leader, with specific procedures for message passing and election initiation. The implementation is done in Python, showcasing the election process and the resulting coordinator declaration.

Uploaded by

abhijaysingh66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

DC Exp6

This document details the implementation of the Bully Algorithm for leader election in distributed systems, focusing on nodes with unique identifiers. The experiment involves simulating a network of nodes that communicate to elect a leader, with specific procedures for message passing and election initiation. The implementation is done in Python, showcasing the election process and the resulting coordinator declaration.

Uploaded by

abhijaysingh66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT : 06

AIM: To implement an Election Algorithm.

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.

The key characteristics of the Bully Algorithm include:

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.

Components of the Implementation:

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

def send_election_message(self, to_process):


print(f"Process {self.process_id} pass Election({self.process_id}) to{to_process.process_id}")
time.sleep(0.2)

def send_coordinator_message(self, to_process):


print(f"Process {self.process_id} pass Coordinator({self.process_id}) message to process
{to_process.process_id}")
time.sleep(0.2)

# Total number of processes


num_processes = 8
processes = [Process(i, is_failed=(i == 7)) for i in range(num_processes)]

print(f"No of processes {num_processes}")


print(f"Process no 7 fails")

# Election initiation by a random active process


initiator = 4
print(f"Election Initiated by {initiator}")

# 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])

# Finding the highest active process as coordinator


new_coordinator = None
for i in range(num_processes - 1, -1, -1):
if not processes[i].is_failed:
new_coordinator = processes[i]
break
if new_coordinator:
print(f"Process {new_coordinator.process_id} becomes coordinator")

# Coordinator sends messages to all lower processes


for i in range(new_coordinator.process_id):
processes[new_coordinator.process_id].send_coordinator_message(processes[i])

print("End Of Election")

Output :

You might also like