def look_fun(num_list, head): first = num_list[0] new_list = [] num_list.append(head) num_list.sort() i = num_list.index(head) if head < first: for num in num_list[i + 1:]: new_list.append(num) for num in num_list[i - 1::-1]: new_list.append(num) else: for num in num_list[i - 1::-1]: new_list.append(num) for num in num_list[i + 1:]: new_list.append(num) _, total = fcfs_fun(new_list, head) return new_list, total
def c_scan_fun(num_list, head): first = num_list[0] new_list = [] num_list.append(head) num_list.sort() i = num_list.index(head) if head < first: for num in num_list[i + 1:]: new_list.append(num) new_list.insert(len(new_list), 199) new_list.insert(len(new_list), 0) for num in num_list[:i]: new_list.append(num) else: for num in num_list[i - 1::-1]: new_list.append(num) new_list.insert(len(new_list), 0) new_list.insert(len(new_list), 199) for num in num_list[len(num_list):i:-1]: new_list.append(num) _, total = fcfs_fun(new_list, head) return new_list, total
def sstf_fun (num_list, head): next_head = head num_list.append(head) num_list.sort() new_list = [] while len(num_list) > 1: i = num_list.index(next_head) if i == 0: next_head = num_list[i+1] num_list.pop(i) elif i == len(num_list) - 1 : next_head = num_list[i] num_list.pop(i) else: diff1 = abs(num_list[i] - num_list[i-1]) diff2 = abs(num_list[i+1] - num_list[i]) next_head = [num_list[i-1] if diff1 < diff2 else num_list[i+1]][0] num_list.pop(i) new_list.append(next_head) _, total = fcfs_fun(new_list, head) return new_list, total
def show_seek_time(): left = [num for num in range(1, 7)] op, num_list, hd = get_data() height = [0, 0, 0, 0, 0, 0] _, height[0] = fcfs_fun(num_list, hd) _, height[1] = sstf_fun(num_list, hd) _, height[2] = scan_fun(num_list, hd) _, height[3] = c_scan_fun(num_list, hd) _, height[4] = look_fun(num_list, hd) _, height[5] = c_look_fun(num_list, hd) al_label = ['fcfs', 'sstf', 'scan', 'c-scan', 'look', 'c-look'] plt.bar(left, height, tick_label=al_label, width=0.8, color=['red', 'green', 'blue', 'pink', 'orange', 'gray']) plt.show()
def openNewWindow(): op, num_list, hd = get_data() if op == 1: ls, total = fcfs_fun(num_list, hd) elif op == 2: ls, total = sstf_fun(num_list, hd) elif op == 3: ls, total = scan_fun(num_list, hd) elif op == 4: ls, total = c_scan_fun(num_list, hd) elif op == 5: ls, total = look_fun(num_list, hd) else: ls, total = c_look_fun(num_list, hd) title = hd_name[op] new_win = Tk() new_win.title(title) canvas = Canvas(master=new_win, width=712, height=600) canvas.configure(bg='black') new_heading = Label(canvas, font=('Courier', 24), text=title) new_heading.configure(background='black', foreground='white') canvas.pack() new_heading.place(relx=0.5, rely=0.1, anchor=CENTER) screen = TurtleScreen(canvas) screen.bgcolor('black') screen.setworldcoordinates(-30, -30, 210, 10) tim = RawTurtle(screen) tim.color('light blue') tim.shape("circle") tim.turtlesize(.3, .3, 3) tim.pensize(3) tim.speed(1) n = len(ls) y = 0 for i in range(0, n): if i == 0: # No drawing while the disktim goes to start position tim.penup() tim.goto(ls[i], y) sleep(0.5) tim.pendown() tim.stamp() tim.write("Head : {}".format(ls[i]), False, align="left", font=("Courier", 12, "bold")) sleep(0.5) else: # Disktim draws its path to each request tim.goto(ls[i], y - 2) sleep(0.5) tim.stamp() tim.write(ls[i], False, align="left", font=('Courier', 12, 'bold')) y -= 2 sleep(0.5) tim.hideturtle() tim.speed(0) tim.penup() tim.goto(100, 5) tim.pendown msg = "Total seek time is : " + str(total) tot_msg = Label(canvas, font=('Courier', 18), text=msg) tot_msg.configure(background='black', foreground='white') tot_msg.place(relx=0.5, rely=0.8, anchor=CENTER)