I make a function that must implement the algorithm (SJF). I already have a ready function for the algorithm (FCFS). If I understand correctly, in order to implement the (SJF) algorithm, you need to streamline the duration of all processes from small to very long and how to use the function (FCFS).
Now there are two questions: do I understand correctly the implementation principle and how best to order the array and use the function (FCFS).
The arrival of processes is done by "String" and looks like this (1.10; 4.2; 12.3; 13.2), where the first number to the comma is the moment when the process gets in the queue and the second is its duration.
Any data entry eventually takes the form ([[1,10], [4,2], [12,3], [13,2]]), there is a ready-made function that transforms in this way.
def SJF(Order): Output = [] # Массив counter = 1 # P1, P2, P3, P4 follow up = 0 # Отслеживает время, когда запускается процесс. ( Смотрят на какой секунде они ) Whole_waiting_time = 0 # Общее время ожидания # Ставят процессы по порядку for p in sorted(Output, reverse=False): Arrival = p[0] # Время, когда процесс заходит в очередь time = p[1] # Сколько длится этот процесс FCFS (already given)
def FCFS(Order): Output = [] # Массив counter = 1 # Программа еще рисует график, счетчик процессов follow up = 0 # Когда запускается процесс Whole_waiting_time = 0 # Общее время ожидание # paneb protsesse vajalikku järjekorda for p in sorted(Output, reverse=False): Arrival = p[0] # Когда встает в очередь time = p[1] # Сколько длится if Arrival > (follow up): # kui kahe protsessi vahel on "auk", siis jäetakse sinna delay näitamiseks õige pikkusega tühik Output.append([" ", Arrival-follow up]) Output.append(["P" + str(counter), time]) follow up = Arrival + time counter+=1 else: # vaatab, kui kaua konkreetne protsess oma järge ootas if Arrival < follow up: Whole_waiting_time += follow up - Arrival # väljundlisti kirjutatakse protsess koos nime ja kestusega Output.append(["P" + str(counter), time]) follow up += time counter += 1 # arvutan keskmise ooteaja ( Тут считается общее время ожидания ) Whole_waiting_time = round(Whole_waiting_time / len(Order), 2) return (Output, Whole_waiting_time)
counter- MaxU