Skip to content

Now works on python 3.x #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 106 additions & 66 deletions algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,27 @@

TIME_QUANTUM = 3

FIRST_COME_FIRST_SERVE = 'First Come First Serve'
FIRST_COME_FIRST_SERVE = "First Come First Serve"

SHORTEST_JOB_FIRST = 'Shortest Job First'
SHORTEST_JOB_FIRST = "Shortest Job First"

SHORTEST_REMAINING_TIME_FIRST = 'Shortest Remaining Time First'
SHORTEST_REMAINING_TIME_FIRST = "Shortest Remaining Time First"

ROUND_ROBIN = 'Round Robin'
ROUND_ROBIN = "Round Robin"

PRIORITY_SCHEDULING = 'Priority Scheduling'
PRIORITY_SCHEDULING = "Priority Scheduling"

RESULTS = {}

GANTT = {}

ALGORITHMS = [FIRST_COME_FIRST_SERVE,
SHORTEST_JOB_FIRST,
SHORTEST_REMAINING_TIME_FIRST,
ROUND_ROBIN,
PRIORITY_SCHEDULING]
ALGORITHMS = [
FIRST_COME_FIRST_SERVE,
SHORTEST_JOB_FIRST,
SHORTEST_REMAINING_TIME_FIRST,
ROUND_ROBIN,
PRIORITY_SCHEDULING,
]


class CPU:
Expand Down Expand Up @@ -72,7 +74,10 @@ def record_process_start_time(self, time):

def record_process_finish_time(self, time):
if self.process is not None:
if self.process.finished is not True and self.process.remaining_burst_time == 0:
if (
self.process.finished is not True
and self.process.remaining_burst_time == 0
):
self.process.finish_time = time
self.process.finished = True

Expand Down Expand Up @@ -106,13 +111,17 @@ def __init__(self, pid):
self.setup()

def setup(self):
self.arrival_time = int(random.uniform(ARRIVAL_TIME_RANGE_MIN, ARRIVAL_TIME_RANGE_MAX))
self.arrival_time = int(
random.uniform(ARRIVAL_TIME_RANGE_MIN, ARRIVAL_TIME_RANGE_MAX)
)
self.priority = int(random.uniform(PRIORITY_RANGE_MIN, PRIORITY_RANGE_MAX))
self.burst_time = int(random.uniform(BURST_TIME_RANGE_MIN, BURST_TIME_RANGE_MAX))
self.burst_time = int(
random.uniform(BURST_TIME_RANGE_MIN, BURST_TIME_RANGE_MAX)
)
self.remaining_burst_time = self.burst_time


class Simulation():
class Simulation:
def __init__(self):
self.process_pool = set()
self.setup()
Expand All @@ -126,14 +135,22 @@ def setup(self):
def pprint_process_pool(self):
processes = sorted(self.process_pool)
for process in processes:
print 'pid %s: Arrival time: %s, Burst Time: %s, Prio: %s, remainingburst: %s' % (
process.pid, process.arrival_time, process.burst_time, process.priority, process.remaining_burst_time)
print (
"pid %s: Arrival time: %s, Burst Time: %s, Prio: %s, remainingburst: %s"
% (
process.pid,
process.arrival_time,
process.burst_time,
process.priority,
process.remaining_burst_time,
)
)

@classmethod
def determine_simulation_time(self):
""" Determines the simulation time based on the latest None in GANTT chart"""

assert len(GANTT) != 0, 'GANTT chart is not populated yet'
assert len(GANTT) != 0, "GANTT chart is not populated yet"
indicies = []

for algorithm in GANTT:
Expand All @@ -145,7 +162,7 @@ def determine_simulation_time(self):
return max(indicies)


class Algorithm():
class Algorithm:
def __init__(self):
pass

Expand All @@ -168,34 +185,42 @@ def calculate_results(self):
total_turnaround_time += turnaround_time

if self.name in RESULTS:
RESULTS[self.name].update({
process.pid: {
'waiting_time': waiting_time,
'turnaround_time': turnaround_time,
'arrival_time': process.arrival_time,
'burst_time': process.burst_time,
'priority': process.priority,
'start_time': process.start_time,
'finish_time': process.finish_time
RESULTS[self.name].update(
{
process.pid: {
"waiting_time": waiting_time,
"turnaround_time": turnaround_time,
"arrival_time": process.arrival_time,
"burst_time": process.burst_time,
"priority": process.priority,
"start_time": process.start_time,
"finish_time": process.finish_time,
}
}
})
)
else:
RESULTS[self.name] = {
process.pid: {
'waiting_time': waiting_time,
'turnaround_time': turnaround_time,
'arrival_time': process.arrival_time,
'burst_time': process.burst_time,
'priority': process.priority,
'start_time': process.start_time,
'finish_time': process.finish_time,
"waiting_time": waiting_time,
"turnaround_time": turnaround_time,
"arrival_time": process.arrival_time,
"burst_time": process.burst_time,
"priority": process.priority,
"start_time": process.start_time,
"finish_time": process.finish_time,
}
}

RESULTS[self.name].update({
'average_waiting_time': round(float(total_waiting_time) / NUMBER_OF_PROCESSES, 2),
'average_turnaround_time': round(float(total_turnaround_time) / NUMBER_OF_PROCESSES, 2),
})
RESULTS[self.name].update(
{
"average_waiting_time": round(
float(total_waiting_time) / NUMBER_OF_PROCESSES, 2
),
"average_turnaround_time": round(
float(total_turnaround_time) / NUMBER_OF_PROCESSES, 2
),
}
)


class FirstComeFirstServe(Algorithm):
Expand All @@ -209,7 +234,7 @@ def __init__(self, simulation):
def get_first_process(self):
try:
return self.deque.popleft()
except Exception, e:
except Exception as e:
return # noop


Expand All @@ -226,7 +251,7 @@ def remove_expired_jobs_from_pool(self):
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception, e:
except Exception as e:
return # noop

def get_shortest_job_from_pool(self):
Expand All @@ -240,7 +265,7 @@ def get_shortest_job_from_pool(self):
shortest_process_pid_time = process.burst_time

return shortest_process
except IndexError, e:
except IndexError as e:
return # noop


Expand All @@ -257,7 +282,7 @@ def remove_expired_jobs_from_pool(self):
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception, e:
except Exception as e:
return # noop

def get_shortest_remaining_time_from_pool(self):
Expand All @@ -266,12 +291,17 @@ def get_shortest_remaining_time_from_pool(self):
shortest_process_pid_remaining_burst_time = 999
try:
for process in self.deque:
if process.remaining_burst_time < shortest_process_pid_remaining_burst_time:
if (
process.remaining_burst_time
< shortest_process_pid_remaining_burst_time
):
shortest_remaining_burst_time_process = process
shortest_process_pid_remaining_burst_time = process.remaining_burst_time
shortest_process_pid_remaining_burst_time = (
process.remaining_burst_time
)

return shortest_remaining_burst_time_process
except IndexError, e:
except IndexError as e:
return # noop


Expand Down Expand Up @@ -308,8 +338,8 @@ def get_next_process(self):
self.current_round_robin_process_index = index
return self.deque[index]

except Exception, e:
print e
except Exception as e:
print (e)
return # noop


Expand All @@ -326,7 +356,7 @@ def remove_expired_jobs_from_pool(self):
for process in self.deque:
if process.remaining_burst_time <= 0:
self.deque.remove(process)
except Exception, e:
except Exception as e:
return # noop

def get_lowest_priority_from_pool(self):
Expand All @@ -340,13 +370,17 @@ def get_lowest_priority_from_pool(self):
lowest_priority = process.priority

return lowest_priority_process
except Exception, e:
except Exception as e:
return # noop


def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=BURST_TIME_RANGE_MAX,
arrival_time_range_max=ARRIVAL_TIME_RANGE_MAX, time_quantum=TIME_QUANTUM,
number_of_processes=NUMBER_OF_PROCESSES):
def run_simulation(
priority_range_max=PRIORITY_RANGE_MAX,
burst_time_range_max=BURST_TIME_RANGE_MAX,
arrival_time_range_max=ARRIVAL_TIME_RANGE_MAX,
time_quantum=TIME_QUANTUM,
number_of_processes=NUMBER_OF_PROCESSES,
):
global NUMBER_OF_PROCESSES
NUMBER_OF_PROCESSES = number_of_processes

Expand All @@ -368,7 +402,7 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
simulation = Simulation()
cpu = CPU()

''' First Come First Serve '''
""" First Come First Serve """
cpu.reset() # Reset cpu for next algorithm simulation
FCFS = FirstComeFirstServe(simulation)

Expand All @@ -384,7 +418,7 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
cpu.record_process_finish_time(time)
cpu.record_gantt(FCFS.name)

''' Shortest Job First '''
""" Shortest Job First """
cpu.reset() # Reset cpu for next algorithm simulation
SJF = ShortestJobFirst(simulation)

Expand All @@ -401,7 +435,7 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
cpu.record_process_finish_time(time)
cpu.record_gantt(SJF.name)

''' Shortest Remaining Time First '''
""" Shortest Remaining Time First """
cpu.reset() # Reset cpu for next algorithm simulation
SRTF = ShortestRemainingTimeFirst(simulation)

Expand All @@ -415,7 +449,7 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
cpu.record_process_finish_time(time)
cpu.record_gantt(SRTF.name)

''' Round Robin '''
""" Round Robin """
cpu.reset() # Reset cpu for next algorithm simulation
RR = RoundRobin(simulation)

Expand All @@ -424,7 +458,11 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
for time in range(0, MAX_SIMULATION_TIME):
RR.check_process_arrivals(time)

if current_process is None or current_time_quantum >= TIME_QUANTUM or current_process.remaining_burst_time == 0:
if (
current_process is None
or current_time_quantum >= TIME_QUANTUM
or current_process.remaining_burst_time == 0
):
current_process = RR.get_next_process()
current_time_quantum = 0
else:
Expand All @@ -438,7 +476,7 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B

current_time_quantum += 1

''' Priority Scheduling '''
""" Priority Scheduling """

cpu.reset() # Reset cpu for next algorithm simulation
PS = PriorityScheduling(simulation)
Expand All @@ -453,29 +491,31 @@ def run_simulation(priority_range_max=PRIORITY_RANGE_MAX, burst_time_range_max=B
cpu.record_process_finish_time(time)
cpu.record_gantt(PS.name)

''' Analysis '''
""" Analysis """
FCFS.calculate_results()
SJF.calculate_results()
SRTF.calculate_results()
RR.calculate_results()
PS.calculate_results()

''' Compile the results to pass to the App '''
""" Compile the results to pass to the App """
# TODO Fix the way results are saved and stored - pretty clunky
max_simulation_time = simulation.determine_simulation_time()
print 'FFF', max_simulation_time
print ("FFF", max_simulation_time)
for algorithm in ALGORITHMS:
# Trimming the GANTT chart "None" tail
parsed_gantt = GANTT[algorithm][0:max_simulation_time]

RESULTS[algorithm].update({
'GANTT': parsed_gantt,
})
RESULTS[algorithm].update(
{
"GANTT": parsed_gantt,
}
)

return RESULTS


if __name__ == '__main__':
if __name__ == "__main__":
run_simulation()

pprint.pprint(RESULTS)