0% found this document useful (0 votes)
6 views4 pages

Bully Algorithm Java Implementation

Uploaded by

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

Bully Algorithm Java Implementation

Uploaded by

abhishektamteimp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Practical_5_Bully_Code

import [Link].*;

public class Bully {

int coordinator;

int max_processes;

boolean processes[];

public Bully(int max) {

max_processes = max;
processes = new boolean[max_processes];

coordinator = max;
[Link]("Creating processes..");

for(int i = 0; i < max; i++) {

processes[i] = true;

[Link]("P"+ (i+1) + " created");

[Link]("Process P" + coordinator + " is the coordinator");

void displayProcesses() {

for(int i = 0; i < max_processes; i++) {

if(processes[i]) {

[Link]("P" + (i+1) + " is up");

} else {

[Link]("P" + (i+1) + " is down");

}
}
[Link]("Process P" + coordinator + " is the coordinator");
}

void upProcess(int process_id) {

if(!processes[process_id - 1]) {

processes[process_id - 1] = true;

[Link]("Process " + process_id + " is now up.");

} else {

[Link]("Process " + process_id + " is already up.");


}

void downProcess(int process_id) {

if(!processes[process_id - 1]) {

[Link]("Process " + process_id + " is already down.");

} else {

processes[process_id - 1] = false;

[Link]("Process " + process_id + " is down.");

void runElection(int process_id) {


coordinator = process_id;

boolean keepGoing = true;

for(int i = process_id; i < max_processes && keepGoing; i++) {

[Link]("Election message sent from process " + process_id + " to process


" + (i+1));

if(processes[i]) {
keepGoing = false;

runElection(i + 1);

public static void main(String args[]) {

Bully bully = null;


int max_processes = 0, process_id = 0;

int choice = 0;

Scanner sc = new Scanner([Link]);

while(true) {

[Link]("Bully Algorithm");

[Link]("1. Create processes");

[Link]("2. Display processes");

[Link]("3. Up a process");

[Link]("4. Down a process");

[Link]("5. Run election algorithm");

[Link]("6. Exit Program");

[Link]("Enter your choice:- ");


choice = [Link]();

switch(choice) {

case 1:

[Link]("Enter the number of processes:- ");

max_processes = [Link]();

bully = new Bully(max_processes);


break;
case 2:

[Link]();

break;

case 3:

[Link]("Enter the process number to up:- ");

process_id = [Link]();

[Link](process_id);

break;
case 4:

[Link]("Enter the process number to down:- ");

process_id = [Link]();

[Link](process_id);

break;

case 5:

[Link]("Enter the process number which will perform election:- ");

process_id = [Link]();

[Link](process_id);

[Link]();

break;

case 6:

[Link](0);
break;

default:
[Link]("Error in choice. Please try again.");

break;

}
}

Common questions

Powered by AI

The code handles the addition of new processes by initializing them as 'up' during the creation of processes. Each newly created process is automatically marked as operational and factored into future elections, ensuring inclusion in the distributed operations from its inception .

Message complexity significantly impacts the Bully algorithm's performance in larger systems, as each election involves sending messages to processes with higher IDs. This results in an O(n^2) complexity for n processes, where each process might need to communicate with every other process. As the system scales, this leads to increased network traffic and potential delays, diminishing performance efficiency .

The Bully algorithm offers simplicity in concept, making it easier to understand and implement. It guarantees that the active process with the highest ID becomes coordinator, ensuring a single, predictable outcome. However, drawbacks include high message overhead and potential delays due to sequential elections in large systems, as well as dependency on complete knowledge of system processes, which might be infeasible in dynamically changing environments .

In the Bully algorithm, the state of a process (up or down) is crucial for determining the participation in elections and the eligibility for becoming a coordinator. Processes marked as 'up' are active and can respond to election messages, while 'down' processes cannot participate in elections or become coordinators. The system relies on correctly identifying process states to ensure that the highest-numbered active process becomes the coordinator .

Processes are marked as 'down' by setting their status to false in the 'processes[]' array. This prevents them from participating in initiating or responding to elections and from being elected as coordinators. The code ensures system consistency by not including 'down' processes in elections, thereby maintaining coherent system operations despite failures .

The primary functionality of the Bully algorithm is to elect a coordinator process among a distributed system of processes. This is achieved by initiating an election when the current coordinator is suspected of failure or when a process comes online after being down. The algorithm ensures that the process with the highest ID eventually becomes the coordinator, managing efficient communication and coordination despite potential system failures .

If multiple processes initiate elections simultaneously, each sends messages to processes with higher IDs. The process with the highest ID among those active will ultimately become the coordinator, as it will not receive any higher-response messages. This results in multiple elections converging to elect the highest-numbered active process, demonstrating the algorithm's robustness in resolving multiple concurrent initiations .

In the Java implementation, the 'coordinator' variable holds the ID of the current coordinator process. It signifies the process responsible for managing certain operations within the distributed system. This variable is updated during the execution of the election method when a new coordinator is determined .

The Bully algorithm assumes reliable message delivery and that each process knows the IDs of all others. This can be limiting in real network environments where message loss or network partitioning occurs. Additionally, the algorithm can lead to significant message overhead, especially as the number of processes increases, since each election involves multiple message exchanges .

The Bully algorithm determines the coordinator through a series of election messages. When a process initiates an election, it sends messages to all processes with higher IDs. If no higher-numbered active process responds, the initiating process becomes the coordinator. If a higher-numbered process is active and responds, it takes over the election and becomes the new coordinator .

You might also like