-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
65 lines (52 loc) · 2.63 KB
/
runner.py
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
class Runner:
def __init__(self):
self._original_processes = []
self._available_process = []
self._blocked_processes = []
self._finished_processes = []
self._running_process = None
self._running_device = None
def add_process(self, process):
self._available_process.insert(0, process)
self._original_processes.append(process)
def run(self, original_timeslice):
timeslice = original_timeslice
while len(self._original_processes) > 0:
if self._running_process is not None:
if self._running_process.process_finish == 0:
self._original_processes.remove(self._running_process)
self._finished_processes.append(self._running_process)
self._running_process = None
elif self._running_process.process_time == 0 and self._running_process.device_time > 0:
self._blocked_processes.insert(0, self._running_process)
self._running_process = None
elif timeslice < 1 and len(self._available_process) > 0:
self._available_process.insert(0, self._running_process)
self._running_process = None
if self._running_device is not None:
if self._running_device.device_time == 0:
self._available_process.insert(0, self._running_device)
self._running_device = None
if len(self._blocked_processes) > 0 and self._running_device is None:
self._running_device = self._blocked_processes.pop()
if len(self._available_process) > 0 and self._running_process is None:
timeslice = original_timeslice
self._running_process = self._available_process.pop()
if self._running_process is not None:
self._running_process.add_history("X")
self._running_process.process()
timeslice -= 1
if self._running_device is not None:
self._running_device.add_history("D")
self._running_device.device_time -= 1
for process in self._available_process:
process.add_history("W")
for process in self._blocked_processes:
process.add_history("B")
for process in self._finished_processes:
process.add_history("T")
def print_order(self):
self._finished_processes.sort(key=lambda x:x.pid, reverse=False)
for process in self._finished_processes:
print(process.pid)
print(process.history)