0% found this document useful (0 votes)
8 views2 pages

DCEXP5

The document contains a Java program that simulates an election process among multiple processes. It initializes a specified number of processes, identifies the maximum active process, and facilitates an election to determine a coordinator. The program includes methods for initializing processes, performing the election, and passing messages between processes.

Uploaded by

Ganesh Panigrahi
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)
8 views2 pages

DCEXP5

The document contains a Java program that simulates an election process among multiple processes. It initializes a specified number of processes, identifies the maximum active process, and facilitates an election to determine a coordinator. The program includes methods for initializing processes, performing the election, and passing messages between processes.

Uploaded by

Ganesh Panigrahi
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
You are on page 1/ 2

EXP5

code :

package EXP5;
import java.util.Scanner;
class Process {
public int id;
public boolean active;
public Process(int id) {
this.id = id;
active = true;
}
}
public class Election {
int noOfProcesses;
Process[] processes;
Scanner sc;
public Election() {
sc = new Scanner(System.in);
}
public void initialiseApp() {
System.out.println("Enter no of processes");
noOfProcesses = sc.nextInt();
processes = new Process[noOfProcesses];
for (int i = 0; i < processes.length; i++) {
processes[i] = new Process(i);
}
}
public int getMax() {
int maxId = -99;
int maxIdIndex = 0;
for (int i = 0; i < processes.length; i++) {
if (processes[i].active && processes[i].id > maxId) {
maxId = processes[i].id;
maxIdIndex = i;
}
}
return maxIdIndex;
}
public void performElection() {
System.out.println("Process no " + processes[getMax()].id + " fails");
processes[getMax()].active = false;
System.out.println("Election Initiated by");
int initiatorProcesss = sc.nextInt();
int prev = initiatorProcesss;
int next = prev + 1;
while (true) {
if (processes[next].active) {
System.out.println("Process " + processes[prev].id + " pass Election(" +
processes[prev].id + ") to " + processes[next].id);
prev = next;
}
next = (next + 1) % noOfProcesses;
if (next == initiatorProcesss) {
break;
}
}
System.out.println("Process " + processes[getMax()].id + " becomes coordinator");
int coordinator = processes[getMax()].id;
prev = coordinator;
next = (prev + 1) % noOfProcesses;
while (true) {
if (processes[next].active) {
System.out.println("Process " + processes[prev].id + " pass Coordinator (" + coordinator
+ ") message to process " + processes[next].id);
prev = next;
}
next = (next + 1) % noOfProcesses;
if (next == coordinator) {
System.out.println("End Of Election ");
break;
}
}
}
public static void main(String arg[]) {
Election r = new Election();
r.initialiseApp();
r.performElection();
}
}

Output:

You might also like