MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / round_robin

Method round_robin

scheduling/multi_level_feedback_queue.py:189–241  ·  view source on GitHub ↗

RR(Round Robin) RR will be applied to MLFQ's all queues except last queue All processes can't use CPU for time more than time_slice If the process consume CPU up to time_slice, it will go back to ready queue >>> P1 = Process("P1", 0, 53) >>> P2 = Proc

(
        self, ready_queue: deque[Process], time_slice: int
    )

Source from the content-addressed store, hash-verified

187 return finished
188
189 def round_robin(
190 self, ready_queue: deque[Process], time_slice: int
191 ) -> tuple[deque[Process], deque[Process]]:
192 """
193 RR(Round Robin)
194 RR will be applied to MLFQ's all queues except last queue
195 All processes can't use CPU for time more than time_slice
196 If the process consume CPU up to time_slice, it will go back to ready queue
197 >>> P1 = Process("P1", 0, 53)
198 >>> P2 = Process("P2", 0, 17)
199 >>> P3 = Process("P3", 0, 68)
200 >>> P4 = Process("P4", 0, 24)
201 >>> mlfq = MLFQ(3, [17, 25], deque([P1, P2, P3, P4]), 0)
202 >>> finish_queue, ready_queue = mlfq.round_robin(mlfq.ready_queue, 17)
203 >>> mlfq.calculate_sequence_of_finish_queue()
204 ['P2']
205 """
206 finished: deque[Process] = deque() # sequence deque of terminated process
207 # just for 1 cycle and unfinished processes will go back to queue
208 for _ in range(len(ready_queue)):
209 cp = ready_queue.popleft() # current process
210
211 # if process's arrival time is later than current time, update current time
212 if self.current_time < cp.arrival_time:
213 self.current_time += cp.arrival_time
214
215 # update waiting time of unfinished processes
216 self.update_waiting_time(cp)
217 # if the burst time of process is bigger than time-slice
218 if cp.burst_time > time_slice:
219 # use CPU for only time-slice
220 self.current_time += time_slice
221 # update remaining burst time
222 cp.burst_time -= time_slice
223 # update end point time
224 cp.stop_time = self.current_time
225 # locate the process behind the queue because it is not finished
226 ready_queue.append(cp)
227 else:
228 # use CPU for remaining burst time
229 self.current_time += cp.burst_time
230 # set burst time 0 because the process is finished
231 cp.burst_time = 0
232 # set the finish time
233 cp.stop_time = self.current_time
234 # update the process' turnaround time because it is finished
235 cp.turnaround_time = self.current_time - cp.arrival_time
236 # add the process to queue that has finished queue
237 finished.append(cp)
238
239 self.finish_queue.extend(finished) # add finished process to finish queue
240 # return finished processes queue and remaining processes queue
241 return finished, ready_queue
242
243 def multi_level_feedback_queue(self) -> deque[Process]:
244 """

Callers 1

Calls 4

update_waiting_timeMethod · 0.95
popleftMethod · 0.80
appendMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected