-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacku
190 lines (161 loc) · 7.42 KB
/
backu
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from .algorithm import Algorithm
from tabulate import tabulate
from copy import copy
import traceback
class RoundRobin(Algorithm):
#Shortest job first
def __init__(self, run_instances):
super().__init__()
self.run_instances = run_instances
self.order = None
self.ordered_instances = None
self.num_lines_to_run_all_instances = None
self.possible_queue = None
def getOrder(self):
instances = self.order
names = []
for item in instances:
names.append(item.getFilename())
concat = []
id_ = 1
for name in names:
concat.append((id_, name))
id_ += 1
return concat
def orderRunInstancesRound(self):
self.orderRunInstancesByArrival()
self.ordered_instances = self.run_instances.copy()
######################################
self.memory = self.getMemory(self.run_instances)
originalInstructionDictionary = self.memory.getDeclarationInstructionDictionary()
instructionDictionary = {}
for key in originalInstructionDictionary:
instructionDictionary[key] = originalInstructionDictionary[key].copy()
timeToRunAll = self.time.getTimeToRunAllInstances()
new_order_run_instances = []
num_lines_to_run_all_instances = []
current_time = 0
lines_to_run = 0
declarationHistory = self.memory.declarationHistory
print(f"numInstructionsAllinstances: {timeToRunAll}")
while(current_time < timeToRunAll):
possible = self.runnableInstances(current_time)
self.printPossible(possible)
instance = possible[0]
self.time.substractFromCPU(instance, 1)
print(f"chosen instance: {instance.getFilename()}, current cpu burst: {self.time.getCurrentCpuBurst(instance)}, lines to run: {lines_to_run}, quantum: {self.time.getQuantum()}")
#if instance is finished then add it to the list
if self.time.getCurrentCpuBurst(instance) == 0:
# print("did it 1")
num_lines_to_run_all_instances.append(lines_to_run)
new_order_run_instances.append(instance)
self.removeInstanceFromQueue()
current_time +=1
lines_to_run = 0
print(f"checked: {self.instancesToReadable(new_order_run_instances)}\n_______________________\n")
continue
else:
#if quantum limit reached, append
if lines_to_run == self.time.getQuantum():
num_lines_to_run_all_instances.append(lines_to_run)
new_order_run_instances.append(instance)
self.putInstanceInTheBack()
current_time +=1
lines_to_run = 0
print(f"checked: {self.instancesToReadable(new_order_run_instances)}\n_______________________\n")
continue
print(f"checked: {self.instancesToReadable(new_order_run_instances)} ")
declaration = instance.progDefs.getDeclaration()
instructions = instructionDictionary[declaration]
print(f"instructions: {instructions}\n")
if len(instructions) == 1:
lines_to_run +=1
instructionDictionary[declaration] = []
else:
if len(instructions) != 0:
instructions.pop(0)
instructionDictionary[declaration] = instructions
lines_to_run +=1
current_time += 1
self.run_instances = new_order_run_instances
self.num_lines_to_run_all_instances = num_lines_to_run_all_instances
def removeInstanceFromQueue(self):
self.possible_queue.pop(0)
def putInstanceInTheBack(self):
for pos in self.possible_queue:
print("previous possible", pos[0].getFilename())
current_instance = self.possible_queue.pop(0)
self.possible_queue.append(current_instance)
for pos in self.possible_queue:
print("new possible", pos[0].getFilename())
def instancesToReadable(self, run_instances):
names = []
for instance in run_instances:
names.append(instance.getFilename())
return names
def getNumInstructionsInAllInstances(self):
self.memory = self.getMemory(self.run_instances)
instructions_all_instances = self.memory.get_programs()
totalNumInstructions = 0
for instructions in instructions_all_instances:
totalNumInstructions += len(instructions)
return totalNumInstructions
def orderRunInstancesByArrival(self):
self.time.setArrivalTimes(self.run_instances)
self.time.setCpuBursts(self.run_instances)
arrive_times = list(self.time.arrive_times.items())
arrive_times.sort(key= lambda tuple: tuple[1])
self.run_instances.clear()
for elem in arrive_times:
self.run_instances.append(elem[0])
def runnableInstances(self, current_time):
if self.possible_queue == None:
self.possible_queue = self.time.getSortedArrivalTimes()
arrive_times = self.possible_queue
possible = []
for time in arrive_times:
if time[1] <= current_time and self.time.getCurrentCpuBurst(time[0]) > 0:
possible.append(time[0])
return possible
def findShortestJob(self, instances_possible):
shortest = 99999999
answer = None
for instance in instances_possible:
if self.time.getCurrentCpuBurst(instance) < shortest:
answer = instance
shortest = self.time.getCurrentCpuBurst(instance)
return answer
def extractFromTimeAndCpu(self, instance): #TODO make it work without time object, but a queue object wrapper that uses cpu, time, and runners
times = self.time.getArrivalTimes()
cpu = self.time.getCpuBursts()
times = times.pop(instance, None)
cpu = cpu.pop(instance, None)
return times, cpu
def setup(self):
self.orderRunInstancesRound()
def run(self):
num_instances = len(self.run_instances)
self.order = self.run_instances.copy()
instance = None
for i in range(num_instances): #! error esta aqui
instance = self.run_instances.pop(0)
print(f"goint to run: {instance.getFilename()}")
instance.run_all_expro(self.num_lines_to_run_all_instances.pop(0))
def printPossible(self, possible):
for each in possible:
print("possible: ", each.getFilename())
print("\n")
for instance2,cpu in self.time.getCpuBursts().items():
print(f"{instance2.getFilename()}, arrival: {self.time.getArrivalTime(instance2)}, cpu: {cpu}")
def getTable(self):
instances = self.ordered_instances
arrival = []
for instance in instances:
arrival.append(self.time.getArrivalTime(instance))
cpu = []
for instance in instances:
cpu.append(self.time.getCpuBurst(instance))
table = []
for instance,arr, cp in zip(instances,arrival,cpu):
table.append([instance.getFilename(), arr, cp])
return tabulate(table, headers=['Nombre', 'Tiempo de llegada', 'Rafaga de cpu'], tablefmt='orgtbl')