Wednesday, 10 April 2019

Program to demonstrate Load Balancing Algorithm (Java)

import java.io.*;
import java.util.*;


class loadbal
{
public void loadbalancer(int servers,int processes )
{
int[] s=new int[servers];
int eachProcess = processes/servers;


for(int i=0;i<servers;i++)
{
s[i]=eachProcess;
}


int remainProcess= processes % servers;
for(int j=0,i=remainProcess;i>0 && j<servers;i--,j++)
{
s[j] = s[j] +1;
}
for(int i=0; i<servers;i++)
{
System.out.println( "\nServer " + (i+1) +" = " + s[i] + " " );
}
}
public static void main(String args[])throws IOException
{
loadbal lb= new loadbal();


int servers=0,processes=0,input;
int choice;


Scanner scrinp = new Scanner(System.in);


do
{


System.out.println("Please enter your choice => \n1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit ");


choice= scrinp.nextInt();


switch(choice)
{
case 1: System.out.println("ENter the no. of servers to be added ");
input = scrinp.nextInt();
while(input < 0)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of servers to be added ");


input = scrinp.nextInt();
}
servers += input;


lb.loadbalancer(servers,processes);
break;


case 2: System.out.println("ENter the no. of servers to be removed ");
input = scrinp.nextInt();
while(input < 0 || input > servers)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of servers to be removed ");
input = scrinp.nextInt();
}
servers -= input;


lb.loadbalancer(servers,processes);
break;
case 3: System.out.println("ENter the no. of processes to be added ");
input = scrinp.nextInt();
while(input < 0)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of processes to be added ");
input = scrinp.nextInt();
}
processes += input;


lb.loadbalancer(servers,processes);
break;


case 4: System.out.println("ENter the no. of processes to be removed ");
input = scrinp.nextInt();
while(input < 0 || input > servers)
{
System.out.println("\nInvalid input.");
System.out.println("\nPlease reENter the no. of processes to be removed ");


input = scrinp.nextInt();


}


processes -= input;


lb.loadbalancer(servers,processes);
break;


case 5: System.exit(0);


default: System.out.println("\nInvalid Choice ");
}
}while(true);
}
}


Output:
// C:\Users\GRIMM\Desktop>javac loadbal.java
// C:\Users\GRIMM\Desktop>java loadbal
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 1
// ENter the no. of servers to be added
// 4


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0


// Server 4 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 2
// ENter the no. of servers to be removed
// 1


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 4
// ENter the no. of processes to be removed
// 2


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit
// 1
// ENter the no. of servers to be added
// 4


// Server 1 = 0


// Server 2 = 0


// Server 3 = 0


// Server 4 = 0


// Server 5 = 0


// Server 6 = 0


// Server 7 = 0
// Please enter your choice =>
// 1. Add Servers 2. Remove Servers 3. Add Process 4. Remove Process 5.Exit



No comments:

Post a Comment