You are on page 1of 5

Academic Year 2022-23 SAP: 60003200104

DEPARTMENT OF INFORMATION TECHNOLOGY

COURSE CODE: DJ19ITL601 CLASS: T Y B. TECH

COURSE NAME: Parallel and Distributed Computing

NAME: Saurav Shah SAP: 60003200104

LAB EXPERIMENT NO. 7

CO/LO: Implementation of Election Algorithms for Distributed environment.

AIM / OBJECTIVE: To implement Bully and Token Ring Election algorithms.

Bully Algorithm
When a process P determines that the current coordinator is down because of message
timeouts or failure of the coordinator to initiate a handshake, it performs the following
sequence of actions:
• P broadcasts an election message (inquiry) to all other processes with
higher process IDs.
• If P hears from no process with a higher process ID than it, it wins the
election and broadcasts victory.
• If P hears from a process with a higher ID, P waits a certain amount of time
for that process to broadcast itself as the leader. If it does not receive this
message in time, it re-broadcasts the election message.
• If P gets an election message (inquiry) from another process with a lower
ID it sends an "I am alive" message back and starts new elections.

Note that if P receives a victory message from a process with a lower ID number, it
immediately initiates a new election. This is how the algorithm gets its name - a process
with a higher ID number will bully a lower ID process out of the coordinator position
as soon as it comes online.
Academic Year 2022-23 SAP: 60003200104

2. Token Ring Algorithm

 Any process Pi noticing that the coordinator is not responding, sends an election
message to its successor.
 If its successor is down, the message is sent to the next member.
 The receiving process adds its number to the message and passes it along.
 When the election message gets back to the election initiator, it recognizes its own
process number and changes the message to C.
 Now it circulates this message to all members.
 The coordinator is the highest process in the total order.

SOURCE CODE:

Bully Algorithm

import java.io.*;
import java.util.Scanner;
public class Main {
static int numberOfProcess;
static int priorities[] = new int[100];
static int status[] = new int[100];
static int cord;
public static void main(String args[]) throws IOException { // handle
IOException
System.out.println("Enter total number of processes:");
Scanner sc = new Scanner(System.in);
numberOfProcess = sc.nextInt();
int i;
for (i = 0; i < numberOfProcess; i++) {
System.out.println("Status for process " + (i + 1) + ":");
status[i] = sc.nextInt();
Academic Year 2022-23 SAP: 60003200104
System.out.println("Priority of process " + (i + 1) + ":");
priorities[i] = sc.nextInt();
}
System.out.println("Enter process which will initiate election");
int ele = sc.nextInt();
sc.close();
electProcess(ele);
System.out.println("After electing process the final coordinator is " +
cord);
}
static void electProcess(int ele) {
ele = ele - 1;
cord = ele + 1;
for (int i = 0; i < numberOfProcess; i++) {
if (priorities[ele] < priorities[i]) {
System.out.println("Election message is sent from " + (ele + 1) +
" to " + (i + 1) + "\n");
if (status[i] == 1) electProcess(i + 1);
}
}
}
}

Output:

Ring Algorithm

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of processes");
int num_proc = sc.nextInt();
int[] flag = new int[num_proc];
for (int i = 0; i < num_proc; i++) {
flag[i] = 1;
}
Academic Year 2022-23 SAP: 60003200104
System.out.println("Enter no. of processes that fail");
int fail_proc = sc.nextInt();
System.out.println("Enter process no. that fails Start from 0 to n-1");
for (int j = 0; j < fail_proc; j++) {
int temp = sc.nextInt();
flag[temp] = 0;
}
System.out.println("Enter initiator");
int init = sc.nextInt();
int coord = -1;
for (int i = init + 1; i <= num_proc + init; i++) {
int i2 = i % num_proc;
if (flag[i2] == 0) continue;
System.out.println("E sent to processor no." + i2);
}
for (int i = init + 1; i < num_proc; i++) {
if (flag[i] == 0) continue;
System.out.println("C is sent to " + i);
coord = i;
}
System.out.println("Coordinator is " + coord);
}
}

OUTPUT:

CONCLUSION:
In distributed computing, leader election is the process of designating a single process as the
organizer of some task distributed among several computers (nodes). Before the task has begun,all
network nodes are either unaware which node will serve as the "leader" (or coordinator) ofthe
task, or unable to communicate with the current coordinator. After a leader election algorithm
has been run, however, each node throughout the network recognizes a, unique nodeas the task
leader.
The network nodes communicate among themselves to decide which of them will get into the
"leader" state. For that, they need some method to break the symmetry among them. For example,
Academic Year 2022-23 SAP: 60003200104
if each node has unique and comparable identities, then the nodes can compare theiridentities,
and decide that the node with the highest identity is the leader.

Therefore, implemented election algorithms like bully and token ring using java, explained
concepts with clear and easily understandable outputs.

REFERENCES:

[1] Mahdi Zargarnataj, “New Election Algorithm based on Assistant in DistributedSystems".

You might also like