DC Exp 8 Election Algorithms
DC Exp 8 Election Algorithms
Experiment No.: 8
Branch: COMPS A – Batch B
Date: 10/11/2024
Name: Hrishikesh Dalal, Dhureen Shettigar, Dhruv Doshi, Vivek
Gangwani
UID: 2022300019, 2022300023, 2022300024,2022300027
Objective:
Theory:
In the Bully Algorithm, any process that detects the absence of a coordinator can
initiate an election. This algorithm operates under the following assumptions:
1. A process that suspects the coordinator has failed will send an "Election"
message to all processes with higher priority.
2. If no higher-priority process responds, the initiating process assumes the
role of coordinator and informs all lower-priority processes by sending an
"Elected" message.
3. If a higher-priority process responds, the initiating process waits for a set
duration to allow the higher-priority process to complete the election.
Ring Algorithm:
In the Ring Algorithm, processes are arranged in a logical ring, and each process
communicates only with its immediate neighbor. The algorithm assumes the
message continues circulating around the ring, even if a process crashes.
self.id = id
self.active_status = active_status
class Ring:
def __init__(self):
if self.n <= 0:
def get_max_id_process(self):
max_id = -1
max_process = None
max_id = process.id
max_process = process
return max_process
if process.id == process_id:
return i
return -1
if self.processes[next_idx].active_status:
return next_idx
return current_idx
def perform_election(self):
failed_process = self.get_max_id_process()
if not failed_process:
return
failed_process.active_status = False
while True:
try:
initiator_idx = self.get_process_index(initiator_id)
if initiator_idx == -1 or not
self.processes[initiator_idx].active_status:
continue
break
except ValueError:
# Election Phase
current_idx = initiator_idx
print("\nElection Phase:")
while True:
next_idx = self.get_next_active_process(current_idx)
election_message.append(self.processes[current_idx].id)
break
current_idx = next_idx
coordinator_id = max(election_message)
coordinator_idx = self.get_process_index(coordinator_id)
current_idx = coordinator_idx
visited_processes = set([coordinator_id])
while True:
next_idx = self.get_next_active_process(current_idx)
visited_processes.add(self.processes[next_idx].id)
current_idx = next_idx
else:
break
print("\nEnd of election")
def main():
try:
ring = Ring()
ring.perform_election()
except ValueError as e:
print(f"Error: {e}")
except KeyboardInterrupt:
if __name__ == "__main__":
main()
Output:
Bully Algorithm
Code:
class Process:
self.id = id
self.active_status = active_status
self.coordinator = None
class BullyElection:
def __init__(self):
if self.n <= 0:
self.current_coordinator = self.get_max_id_process()
process.coordinator = self.current_coordinator.id
def get_max_id_process(self):
max_id = -1
max_process = None
for process in self.processes:
max_id = process.id
max_process = process
return max_process
def simulate_process_failure(self):
if self.current_coordinator:
self.current_coordinator.active_status = False
self.current_coordinator = None
return True
return False
higher_processes = self.get_higher_processes(initiator_id)
if not higher_processes:
self.become_coordinator(initiator_id)
return
received_ok = False
self.send_election_message(initiator_id, process.id)
if process.active_status and not process.timeout:
self.send_ok_message(process.id, initiator_id)
received_ok = True
self.start_election(process.id)
if not received_ok:
self.become_coordinator(initiator_id)
self.current_coordinator = self.processes[process_id]
self.send_coordinator_message(process_id, process.id)
process.coordinator = process_id
def perform_election(self):
return
while True:
try:
continue
if not self.processes[initiator_id].active_status:
continue
break
except ValueError:
self.start_election(initiator_id)
print("\nFinal state:")
else:
def main():
try:
bully = BullyElection()
bully.perform_election()
except ValueError as e:
print(f"Error: {e}")
except KeyboardInterrupt:
if __name__ == "__main__":
main()
Output:
Conclusion:
In this practical study, we successfully implemented and analyzed two fundamental distributed
election algorithms - the Ring Algorithm and the Bully Algorithm - using Python.
Our implementation demonstrated distinct characteristics of each algorithm. The Ring Algorithm
showed consistent message complexity, requiring exactly 2(n-1) messages for any election
process: (n-1) messages for the election phase and (n-1) messages for coordinator
announcement, regardless of which process initiates the election. In contrast, the Bully
Algorithm's performance varied significantly based on the initiating process. When initiated by
the lowest ID process, it required Θ(n²) messages, but performed more efficiently when higher
ID processes initiated the election.
Through this implementation, we gained practical insights into distributed systems and
successfully demonstrated how both algorithms handle coordinator failures with different
approaches and trade-offs.