Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views6 pages

DC Exp 08

Uploaded by

Hemant Gaikwad
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)
9 views6 pages

DC Exp 08

Uploaded by

Hemant Gaikwad
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/ 6

Experiment No.

Name Of The Exp. : Load Balancing Algorithm

Aim : Write a program for Load Balancing Algorithm


Pre- Requisite : Networking
Theory :
Rapid growth in use of computer has increased the number of resource sharing application and
ultimately increased the amount of load across internet. Problem can be solved by increasing the
size of servers or distribute the applications in effective manner across different servers.
Distribution is termed as load balancing. Load Balancing based on the idea of migration of excess
load from heavily loaded node to lightly loaded ones
Load balancing is the way of distributing load units (jobs or tasks) across a set of processors
which are connected to a network which may be distributed across the globe. The excess load or
remaining unexecuted load from a processor is migrated to other processors which have load below
the threshold load. Threshold load is such an amount of load to a processor that any load may come
further to that processor. In a system with multiple nodes there is a very high chance that some
nodes will be idle while the other will be over loaded. So the processors in a system can be
identified according to their present load as heavily loaded processors (enough jobs are waiting
for execution), lightly loaded processors(less jobs are waiting) and idle processors (have no
job to execute). By load balancing strategy it is possible to make every processor equally busy and
to finish the works approximately at the same time.
Static Load Balancing:
In static algorithm the processes are assigned to the processors at the compile time according to the
performance of the nodes. Once the processes are assigned, no change or reassignment is possible
at the run time. Number of jobs in each node is fixed in static load balancing algorithm.
Static algorithms do not collect any information about the nodes.
Dynamic Load Balancing:
The assignment of jobs is done at the runtime. In DLB jobs are reassigned at the runtime
depending upon the situation that is the load will be transferred from heavily loaded nodes to
the lightly loaded nodes. In this case communication over heads occur and becomes more when
number of processors increase.
Benefits of Load balancing:
a) Load balancing improves the performance of each node and hence the overall system
performance.
b) Load balancing reduces the job idle time
c) Small jobs do not suffer from long starvation
d) Maximum utilization of resources
e) Response time becomes shorter
f) Higher throughput
g) Higher reliability
h) Low cost but high gain
i) Extensibility and incremental growth
Load Balancing Strategies:
Sender-Initiated vs. Receiver-Initiated Strategies:- In sender-initiated policies, congested nodes
attempt to move work to lightly-loaded nodes. In receiver-initiated policies, lightly-loaded nodes
look for heavily-loaded nodes from which work may be received. The sender-initiated policy
performs better than the receiver-initiated policy at low to moderate system loads. Reasons are that
at these loads, the probability of finding a lightly-loaded node is higher than that of finding a
heavily-loaded node. Similarly, at high system loads, the receiver initiated policy performs better
since it is much easier to find a heavily-loaded node. As a result, adaptive policies have been
proposed which behave like sender-initiated policies at low to moderate system loads, while at high
system loads they behave like receiver-initiated policies.
Implementation:
class LoadBalancer:
def __init__(self):
self.servers = [] # List of available servers
self.processes = [] # List of processes (tasks) to be distributed
self.index = 0 # Keeps track of the last used server

def add_server(self, server_name):


"""Add a new server to the load balancer"""
self.servers.append(server_name)
print(f"Server {server_name} added.")

def remove_server(self, server_name):


"""Remove a server from the load balancer"""
if server_name in self.servers:
self.servers.remove(server_name)
print(f"Server {server_name} removed.")
else:
print(f"Server {server_name} not found.")

def add_process(self, process_id):


"""Add a new process (task) to be distributed among servers"""
if not self.servers:
print("No servers available to process tasks.")
return

self.processes.append(process_id)
# Get the next server in the round-robin rotation
server = self.get_next_server()
print(f"Process {process_id} is being sent to {server}")

def remove_process(self, process_id):


"""Simulate removal of a process"""
if process_id in self.processes:
self.processes.remove(process_id)
print(f"Process {process_id} has been removed.")
else:
print(f"Process {process_id} not found.")

def get_next_server(self):
"""Return the next server in round-robin fashion"""
if len(self.servers) == 0:
return None
server = self.servers[self.index]
# Update the index for the next server in line
self.index = (self.index + 1) % len(self.servers)
return server

def display_servers(self):
"""Display the list of servers"""
if self.servers:
print("Current Servers:")
for server in self.servers:
print(f"- {server}")
else:
print("No servers available.")

def display_processes(self):
"""Display the list of processes"""
if self.processes:
print("Current Processes:")
for process in self.processes:
print(f"- {process}")
else:
print("No processes available.")

def display_menu():
print("\nLoad Balancer Menu:")
print("1. Add a new server")
print("2. Remove a server")
print("3. Add a new process")
print("4. Remove a process")
print("5. Display servers")
print("6. Display processes")
print("7. Exit")

def main():
load_balancer = LoadBalancer()

while True:
# Display the menu
display_menu()

# Get user input


try:
choice = int(input("Enter your choice: "))
except ValueError:
print("Invalid input. Please enter a number between 1 and 7.")
continue

# Perform action based on user input


if choice == 1:
server_name = input("Enter server name to add: ")
load_balancer.add_server(server_name)

elif choice == 2:
server_name = input("Enter server name to remove: ")
load_balancer.remove_server(server_name)

elif choice == 3:
process_id = input("Enter process ID to add (e.g., Process-1): ")
load_balancer.add_process(process_id)

elif choice == 4:
process_id = input("Enter process ID to remove: ")
load_balancer.remove_process(process_id)

elif choice == 5:
load_balancer.display_servers()

elif choice == 6:
load_balancer.display_processes()

elif choice == 7:
print("Exiting program.")
break

else:
print("Invalid choice! Please choose a number between 1 and 7.")

if __name__ == "__main__":
main()

Output :
Conclusion :
Thus, we have successfully implemented a sender initiated load balancing algorithm.

You might also like