-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.py
More file actions
executable file
·123 lines (99 loc) · 4.67 KB
/
Simulator.py
File metadata and controls
executable file
·123 lines (99 loc) · 4.67 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# Entry point for starting the Simulator
from Fcfs import Fcfs as fcfs
from Priority import Priority as priority
import argparse
import Jobs
from PlotGraphs import PlotGraphs
class Simulator:
def display_graphs(self, fcfs_job_list, priority_job_list, njobs):
wait_y_axis = self.populate_y_axis_list(fcfs_job_list, priority_job_list, njobs, "wait")
completion_y_axis = self.populate_y_axis_list(fcfs_job_list, priority_job_list, njobs, "completion")
pg = PlotGraphs()
pg.compare_waiting_times(wait_y_axis)
pg.compare_completion_times(completion_y_axis)
def populate_y_axis_list(self, fcfs_job_list, priority_job_list, njobs, time_to_calculate):
"""
This method calculates either wait time or completion time
based on value time_to_calculate, and populates the y-axis list
:param fcfs_job_list:
:param priority_job_list:
:param linux_job_list:
:param njobs:
:param time_to_calculate:
:return:
"""
y_axis_list = []
if str(time_to_calculate).lower() == "wait":
# Calculate the wait time for each algorithm
fcfs_wait_time = self.calculate_avg_wait_time(fcfs_job_list, njobs)
priority_wait_time = self.calculate_avg_wait_time(priority_job_list, njobs)
# linux_wait_time = self.calculate_avg_wait_time(linux_job_list, njobs) # Uncomment this
# Append each wait time in the y_axis list
y_axis_list.append(fcfs_wait_time)
y_axis_list.append(priority_wait_time)
# self.y_axis_list.append(linux_wait_time)
y_axis_list.append(10) # Remove this hardcoded value
return y_axis_list
elif str(time_to_calculate).lower() == "completion":
# Calculate the completion time for each algorithm
fcfs_completion_time = self.calculate_avg_completion_time(fcfs_job_list, njobs)
priority_completion_time = self.calculate_avg_completion_time(priority_job_list, njobs)
# linux_completion_time = self.calculate_avg_completion_time(linux_job_list, njobs) # Uncomment this
# Append each completion time in the y_axis list
y_axis_list.append(fcfs_completion_time)
y_axis_list.append(priority_completion_time)
# self.y_axis_list.append(linux_wait_time)
y_axis_list.append(10) # Remove this hardcoded value
return y_axis_list
def calculate_avg_wait_time(self, job_list, no_of_jobs):
"""
This method calculates average waiting time
for given job list
:param job_list:
:param no_of_jobs:
:return:
"""
total_wait_time = 0
for x in range(0, len(job_list)):
total_wait_time = total_wait_time + job_list[x].get_waiting_time()
avg_wait_time = total_wait_time / no_of_jobs
return avg_wait_time
def calculate_avg_completion_time(self, job_list, no_of_jobs):
"""
This method calculates average waiting time
for given job list
:param job_list:
:param no_of_jobs:
:return:
"""
total_completion_time = 0
for x in range(0, len(job_list)):
total_completion_time = total_completion_time + job_list[x].get_completion_time()
avg_completion_time = total_completion_time / no_of_jobs
return avg_completion_time
def main():
print("Starting Simulator")
parser = argparse.ArgumentParser()
requiredArgs = parser.add_argument_group('required arguments')
requiredArgs.add_argument("-n", "--jobs", metavar="", help="Number of Jobs to be run on a scheduling Algorithm",
type=int, required=True, action="store")
requiredArgs.add_argument("-t", "--cputime", metavar="", help="CPU Time", required=True, action="store", type=int)
args = parser.parse_args()
njobs = args.jobs
cpuTime = args.cputime
if njobs > 0 and cpuTime > 0:
# call the create jobs methods from the Jobs file
job_List = Jobs.create_Jobs(njobs)
print("Started Jobs execution through FCFS Scheduling")
fcfs_job_list = fcfs().execute_fcfs(njobs, cpuTime, job_List)
print("Finished Jobs execution through FCFS Scheduling")
print("Started Jobs execution through Priority Scheduling")
priority_job_list = priority().execute_priority(njobs, cpuTime, job_List)
print("Finished Jobs execution through Priority Scheduling")
# Display graphs
sim = Simulator()
sim.display_graphs(fcfs_job_list, priority_job_list, njobs)
print("Simulator Exiting")
if __name__ == "__main__":
main()