def SRTF(process_list, cst): num = len(process_list) #sort the array of processes according to arrival time and running time process_list.sort(key=lambda x: (x.arrival, x.running), reverse=False) # the program always start at arrival time of 1st process Current_Time = process_list[0].arrival Current_Process = list() process_queue = list() # process sequence of execution complete = 0 i = 0 l_id = 0 s_t = np.array([-1.0 for i in range(0, num + 1)]) f_t = np.array([0.0 for i in range(0, num + 1)]) step = 0.0001 #assume in this algorithm check every 1 unit time while complete != num: process_queue = update_queue(process_list, process_queue, Current_Time) if len(process_queue) > 0: if process_queue[0].pid != l_id: # check if process changed or not Current_Time += cst if s_t[process_queue[0]. pid] == -1: # check if this process start before or not s_t[process_queue[0].pid] = Current_Time process_queue[0].start = Current_Time Current_Process.insert(i, deepcopy(process_queue[0])) l_id = process_queue[0].pid i += 1 process_queue[0].remaining -= step if (process_queue[0].remaining <= 0): f_t[process_queue[0].pid] = Current_Time + step complete += 1 process_queue.remove(process_queue[0]) Current_Process[len(Current_Process) - 1].finish = Current_Time + step Current_Time += step avg_tat = 0.0 avg_wtat = 0.0 for i in range(0, len(process_list)): process_list[i].tat = f_t[ process_list[i].pid] - process_list[i].copy_arrival process_list[i].wait = process_list[i].tat - process_list[ i].running # tat = wait time + brust process_list[i].wtat = process_list[i].tat / process_list[ i].running #turnaroundtime/burst time avg_tat += process_list[i].tat avg_wtat += process_list[i].wtat #calculate Average turnaround time of schedule avg_tat /= num #calculate Average weightturnaround time of schedule avg_wtat /= num print_p(process_list, avg_tat, avg_wtat, "SRTF") return (Current_Process)
def FCFS(process_list, cst): num = len(process_list) # the program always start at arrival time of 1st process Current_Time = min(process_list, key=lambda x: x.arrival).arrival #calculate Wait time ,turnaround time, weighted turnaround time avg_tat = 0 avg_wtat = 0 process_queue = list() step = 0.0001 i = 0 lst_id = -1 while i < num: process_queue = update_queue(process_list, process_queue, Current_Time) if (lst_id == process_queue[-1].pid): Current_Time += step else: process_queue[i].start = Current_Time if process_queue[i - 1].finish < process_queue[i].arrival: process_queue[i].start = process_queue[i].arrival process_queue[i].start += cst process_queue[ i].finish = process_queue[i].start + process_queue[i].running process_queue[ i].wait = process_queue[i].start - process_queue[i].arrival process_queue[i].tat = process_queue[i].wait + process_queue[ i].running # wait time+ burst time process_queue[i].wtat = process_queue[i].tat / process_queue[ i].running # turnaroundtime/burst time avg_tat += process_queue[i].tat avg_wtat += process_queue[i].wtat Current_Time = process_queue[i].finish lst_id = process_queue[i].pid i += 1 avg_tat /= float(num) avg_wtat /= float(num) print_p(process_queue, avg_tat, avg_wtat, "FCFS") return process_queue
def HPF (process_list,cst): num=len(process_list) #sort the array of processes according to arrival time and priority process_list.sort(key=lambda x: (-x.arrival,x.priority), reverse=True) # the program always start at arrival time of 1st process Current_Time = process_list[0].arrival Current_Process=list() # process sequence of execution process_queue=list() avg_tat = 0 avg_wtat = 0 complete = 0 step = 0.0001 while complete != num: process_queue= update_queue(process_list, process_queue, Current_Time) if(len(process_queue)==0): Current_Time += step else: Current_Process.insert(complete, deepcopy(process_queue[0])) Current_Process[complete].start=Current_Time+cst process_queue.remove(process_queue[0]) Current_Time += Current_Process[complete].running+cst Current_Process[complete].finish = Current_Time # calculate Wait time ,turnaround time, weighted turnaround time Current_Process[complete].wait = Current_Process[complete].start - Current_Process[complete].arrival # wait time = start time - arrival time Current_Process[complete].tat = Current_Process[complete].wait + Current_Process[complete].running # wait time+ burst time Current_Process[complete].wtat = Current_Process[complete].tat / Current_Process[complete].running # turnaroundtime/burst time avg_tat += Current_Process[complete].tat avg_wtat += Current_Process[complete].wtat complete += 1 #calculate Average turnaround time of schedule avg_tat/= num #calculate Average weightturnaround time of schedule avg_wtat/=num print_p(Current_Process, avg_tat, avg_wtat, "HPF") return (Current_Process)
def RR(pList, qt, cst): pList.sort(key=lambda x: x.arrival, reverse=False) readyQ = list() readyQ.append(pList[0]) time = pList[0].arrival pList[0].arrival = -1 prev = readyQ[0] avg_tat = 0.0 avg_wtat = 0.0 chart = [] first = 0 complete = 0 while complete != len(pList): if first == 0: time += cst first = 1 if len(readyQ) == 0: if time < pList[complete].copy_arrival: time = pList[complete].copy_arrival update_readyQ(pList, readyQ, time) time += cst if readyQ[0].running > qt: readyQ[0].running -= qt chart.append(pRR(readyQ[0].pid, time, time + qt)) time += qt update_readyQ(pList, readyQ, time) readyQ.append(readyQ[0]) prev = readyQ[0] del readyQ[0] else: chart.append(pRR(readyQ[0].pid, time, time + readyQ[0].running)) time += readyQ[0].running readyQ[0].running = 0 readyQ[0].finish = time readyQ[0].tat = readyQ[0].finish - readyQ[0].copy_arrival readyQ[0].wait = readyQ[0].tat - readyQ[0].copy_running readyQ[0].wtat = readyQ[0].tat / readyQ[0].copy_running avg_tat += readyQ[0].tat avg_wtat += readyQ[0].wtat prev = readyQ[0] del readyQ[0] complete += 1 if len(readyQ) != 0: if prev != readyQ[0]: time += cst avg_tat /= float(len(pList)) avg_wtat /= float(len(pList)) print_p(pList, avg_tat, avg_wtat, "RR") return chart
def print_letter(letter): """Prints a letter. The input variable is the index of the letter to print. The letters from A to Z are indexed at 0 (so A=0, B=1, etc). There are currently 2 addition special 'characters'. 26 will 'print' a space 27 will execute a 'new line' You are free to add any additional special characters here. The pen should be positioned at the top left corner of the letter box before calling this myblock. After printing the letter, the pen will be moved to the position of the next letter. If it is at the end of the line, it will automatically move the pen to the beginning of the next line. """ variables['LastLetterWidth'] = variables['Seg4'] variables['LetterSpacing'] = 20 if letter == 0: print_a(1) elif letter == 1: print_b(1) elif letter == 2: print_c(1) elif letter == 3: print_d(1) elif letter == 4: print_e(1) elif letter == 5: print_f(1) elif letter == 6: print_g(1) elif letter == 7: print_h(1) elif letter == 8: print_i(1) variables['LastLetterWidth'] = 0 elif letter == 9: print_j(1) elif letter == 10: print_k(1) elif letter == 11: print_l(1) elif letter == 12: print_m(1) elif letter == 13: print_n(1) elif letter == 14: print_o(1) elif letter == 15: print_p(1) elif letter == 16: print_q(1) elif letter == 17: print_r(1) elif letter == 18: print_s(1) elif letter == 19: print_t(1) elif letter == 20: print_u(1) elif letter == 21: print_v(1) elif letter == 22: print_w(1) elif letter == 23: print_x(1) elif letter == 24: print_y(1) elif letter == 25: print_z(1) elif letter == 26: if variables['LinePosition'] == 0: variables['LetterSpacing'] = 0 variables['LastLetterWidth'] = 0 else: print_space(1) elif letter == 27: carriage_move(0) line_feed() variables['LetterSpacing'] = 0 # Move the pen to accommodate the letter spacing, and update all the # variables tracking the position of the pen on the line. letter_spacing = variables['LetterSpacing'] motor['A'].on_for_degrees(20, letter_spacing) line_position = (variables['LastLetterWidth'] + variables['LinePosition'] + letter_spacing) variables['LinePosition'] = line_position # Do an automatic new line if we are at the end of the current line. if line_position > variables['LineWidth']: carriage_move(0) line_feed()