-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoad_Balancing.py
More file actions
104 lines (85 loc) · 3.25 KB
/
Load_Balancing.py
File metadata and controls
104 lines (85 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# Name: Amey Mahendra Thakur
# Course: Distributed Computing Lab (CSL802)
# Roll No: 50 | Batch: B3
# Date of Experiment: March 11, 2022
# Repository: https://github.com/Amey-Thakur/DISTRIBUTED-COMPUTING-AND-DISTRIBUTED-COMPUTING-LAB
# Description: Experiment 8 - Implementation of a Static Load Balancing Algorithm using multithreading in Python.
import threading
import time
# Global variables for cross-thread communication
shared_remainder = 0
lock = threading.Lock()
def processor_1_execution(limit, total_tasks):
"""
Simulates the first processor's execution logic.
Identifies overload and calculates tasks to be migrated.
"""
global shared_remainder
print(f"\n[PROCESSOR 1] Status (Capacity: {limit})")
if total_tasks == 0:
print(" -> No processes available for execution.")
with lock:
shared_remainder = 0
return
if limit > total_tasks:
print(" -> State: Underloaded")
print(f" -> Executed: {total_tasks} processes.")
rem = 0
elif limit == total_tasks:
print(" -> State: Normal Load")
print(f" -> Executed: {total_tasks} processes.")
print(" -> Result: No tasks forwarded.")
rem = 0
else:
print(" -> State: OVERLOADED")
print(f" -> Executed: {limit} processes (Max Capacity).")
rem = total_tasks - limit
print(f" -> Overflow: {rem} processes forwarded to Processor 2.")
with lock:
shared_remainder = rem
def processor_2_execution(limit):
"""
Simulates the second processor's execution logic.
Executes overflow tasks received from Processor 1.
"""
# Delay to ensure Processor 1 finishes distribution
time.sleep(2)
print(f"\n[PROCESSOR 2] Status (Capacity: {limit})")
with lock:
workload = shared_remainder
if workload == 0:
print(" -> No overflow tasks received from Processor 1.")
else:
if limit > workload:
print(" -> State: Underloaded")
print(f" -> Executed: {workload} processes.")
elif limit == workload:
print(" -> State: Normal Load")
print(f" -> Executed: {workload} processes.")
else:
print(" -> State: OVERLOADED")
print(f" -> Executed: {limit} processes (Max Capacity).")
final_rem = workload - limit
print(f" -> Critical: {final_rem} processes still queued (System Overloaded).")
def main():
print("="*60)
print(" STATIC LOAD BALANCING SIMULATOR (PYTHON) ")
print("="*60)
try:
l1 = int(input("Enter execution limit for Processor 1: "))
l2 = int(input("Enter execution limit for Processor 2: "))
n = int(input("Enter total number of processes: "))
# Creating threads for distributed processing
t1 = threading.Thread(target=processor_1_execution, args=(l1, n))
t2 = threading.Thread(target=processor_2_execution, args=(l2,))
# Starting parallel simulation
t1.start()
t2.start()
# Waiting for completion
t1.join()
t2.join()
except ValueError:
print("[!] Input Error: Please enter numerical values for limits and processes.")
print("\n" + "="*60)
if __name__ == "__main__":
main()