-
Notifications
You must be signed in to change notification settings - Fork 3
/
RR_algo.py
139 lines (123 loc) · 6.64 KB
/
RR_algo.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
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
import sys
sys.path.append(".")
from Grantt_Information import ProcessGrantInfo
class RoundRobin:
processes = []
mode = ''
grantt_chart = []
queue_num = None
ClassName = 'RR'
def __init__(self, processes, mode=''):
self.mode = mode
self.processes = processes
if mode != '':
self.queue_num = mode.split('xx')[0]
def cpu_process(self, time_quantum):
current_cpu_time = 0
prev_cpu_time = 0
not_started = False
ready_processes_queue = []
processes_next_ready_queue = self.get_arrival_times()
while True:
finished = False
if not not_started:
first_process = self.processes[0]
self.processes.remove(first_process)
ready_processes_queue.append([first_process, 0])
for process in self.processes.copy():
if process.arrival_time == first_process.arrival_time:
ready_processes_queue.append([process, 0])
self.processes.remove(process)
not_started = True
if len(ready_processes_queue) == 0:
current_cpu_time += 1
continue
sub_count = ready_processes_queue[0][1]
current_process = ready_processes_queue[0][0]
pre_current_process = current_process.cpu_burst_time1 + current_process.cpu_burst_time2 + current_process.io_time
if current_process.cpu_burst_time1 > 0 and current_process.arrival_time <= current_cpu_time:
not_entered = True
for element in self.grantt_chart:
if element.process.process_id == current_process.process_id:
not_entered = False
if not_entered:
self.grantt_chart.append(ProcessGrantInfo(current_process,
max(current_process.arrival_time, current_cpu_time),
# first cpu start
-1, # io start time
-1, # sec cpu start
-1, # first cpu end
-1, # io end time
-1)) # sec cpu end
current_process.cpu_burst_time1 -= time_quantum
sub_count += 1
if current_process.cpu_burst_time1 < 0:
current_cpu_time += (current_process.cpu_burst_time1 + time_quantum)
else:
current_cpu_time += time_quantum
if current_process.cpu_burst_time1 <= 0 and current_process.arrival_time <= current_cpu_time and \
processes_next_ready_queue[str(current_process.process_id)] <= current_cpu_time:
if current_process.io_time > 0:
ready_processes_queue[0][1] = 0 # sub_count = 0
sub_count = 0
processes_next_ready_queue[
str(current_process.process_id)] = current_cpu_time + current_process.io_time
for info in self.grantt_chart:
if info.process.process_id == current_process.process_id:
info.process = current_process
info.io_start_time = current_cpu_time
info.cpu_end_time1 = current_cpu_time
info.io_end_time = current_cpu_time + current_process.io_time
if current_process.cpu_burst_time2 <= 0:
info.cpu_start_time2 = -1
info.cpu_end_time2 = -1
break
current_process.io_time = 0
finished = True
elif current_process.cpu_burst_time2 > 0 and current_process.cpu_burst_time1 <= 0:
if sub_count == 0:
for info in self.grantt_chart:
if info.process.process_id == current_process.process_id:
info.cpu_start_time2 = current_cpu_time
if processes_next_ready_queue[str(current_process.process_id)] <= current_cpu_time:
current_process.cpu_burst_time2 -= time_quantum
sub_count += 1
if current_process.cpu_burst_time2 < 0:
current_cpu_time += (current_process.cpu_burst_time2 + time_quantum)
else:
current_cpu_time += time_quantum
if current_process.cpu_burst_time2 <= 0:
for info in self.grantt_chart:
if info.process.process_id == current_process.process_id:
info.process = current_process
info.cpu_end_time2 = current_cpu_time
break
# if ready_processes_queue[0][1] != sub_count or finished:
current_process_value = current_process.cpu_burst_time1 + current_process.cpu_burst_time2 + current_process.io_time
if pre_current_process != current_process_value:
ready_processes_queue.pop(0)
if current_process.cpu_burst_time2 > 0 or current_process.cpu_burst_time1 > 0 or current_process.io_time > 0:
ready_processes_queue.append([current_process, sub_count])
# if current_process.cpu_burst_time2 + current_process.cpu_burst_time1 + current_process.io_time <= 0:
# ready_processes_queue.pop(0)
if len(self.processes) <= 0 and len(ready_processes_queue) <= 0:
break
if prev_cpu_time == current_cpu_time:
current_cpu_time += 1
prev_cpu_time = current_cpu_time
_temp = []
new_queue = False
for process in self.processes.copy():
if process.arrival_time <= current_cpu_time:
_temp.insert(0, [process, 0])
self.processes.remove(process)
new_queue = True
if new_queue:
for process_info in _temp:
ready_processes_queue.insert(0, process_info)
_temp.clear()
def get_arrival_times(self):
_temp = {}
for process in self.processes:
_temp[str(process.process_id)] = process.arrival_time
return _temp