编写一个可视化时间片轮转算法模拟程序
时间: 2023-12-01 17:30:50 浏览: 72
好的,这里提供一个用Python编写的可视化时间片轮转算法模拟程序,使用了tkinter库进行图形化界面的设计。
```python
import tkinter as tk
class Process:
def __init__(self, name, burst_time, arrival_time):
self.name = name
self.burst_time = burst_time
self.arrival_time = arrival_time
self.turnaround_time = 0
self.waiting_time = 0
class RoundRobin:
def __init__(self, processes, time_slice):
self.processes = processes
self.time_slice = time_slice
self.clock = 0
self.completed_processes = []
self.current_process = None
self.remaining_time = 0
def get_next_process(self):
if not self.processes:
return None
if self.current_process is None:
return self.processes[0]
if self.remaining_time == 0:
self.completed_processes.append(self.current_process)
self.current_process = None
if self.current_process is not None:
index = self.processes.index(self.current_process)
next_index = (index + 1) % len(self.processes)
else:
next_index = 0
while True:
next_process = self.processes[next_index]
if next_process.burst_time == 0:
next_index = (next_index + 1) % len(self.processes)
else:
return next_process
def run(self):
while True:
self.current_process = self.get_next_process()
if self.current_process is None:
break
if self.remaining_time == 0:
self.remaining_time = min(self.current_process.burst_time, self.time_slice)
self.remaining_time -= 1
self.current_process.burst_time -= 1
if self.current_process.burst_time == 0:
self.current_process.turnaround_time = self.clock - self.current_process.arrival_time + 1
self.current_process.waiting_time = self.current_process.turnaround_time - (self.current_process.burst_time + 1)
self.completed_processes.append(self.current_process)
self.current_process = None
self.clock += 1
def get_avg_turnaround_time(self):
return sum(p.turnaround_time for p in self.completed_processes) / len(self.completed_processes)
def get_avg_waiting_time(self):
return sum(p.waiting_time for p in self.completed_processes) / len(self.completed_processes)
class Application:
def __init__(self, master):
self.master = master
self.master.title("Round Robin Scheduling Algorithm Simulation")
self.processes = []
self.time_slice = tk.IntVar()
self.avg_turnaround_time = tk.DoubleVar()
self.avg_waiting_time = tk.DoubleVar()
# Time slice input
tk.Label(master, text="Time Slice:").grid(row=0, column=0)
tk.Entry(master, textvariable=self.time_slice).grid(row=0, column=1)
# Process inputs
tk.Label(master, text="Processes:").grid(row=1, column=0)
tk.Label(master, text="Name").grid(row=2, column=0)
tk.Label(master, text="Burst Time").grid(row=2, column=1)
tk.Label(master, text="Arrival Time").grid(row=2, column=2)
self.process_rows = []
for i in range(5):
name_var = tk.StringVar()
burst_time_var = tk.IntVar()
arrival_time_var = tk.IntVar()
name_entry = tk.Entry(master, textvariable=name_var)
burst_time_entry = tk.Entry(master, textvariable=burst_time_var)
arrival_time_entry = tk.Entry(master, textvariable=arrival_time_var)
name_entry.grid(row=i+3, column=0)
burst_time_entry.grid(row=i+3, column=1)
arrival_time_entry.grid(row=i+3, column=2)
self.process_rows.append((name_var, burst_time_var, arrival_time_var))
# Run button
tk.Button(master, text="Run", command=self.run).grid(row=8, column=1)
# Average turnaround time and waiting time outputs
tk.Label(master, text="Average Turnaround Time:").grid(row=9, column=0)
tk.Label(master, textvariable=self.avg_turnaround_time).grid(row=9, column=1)
tk.Label(master, text="Average Waiting Time:").grid(row=10, column=0)
tk.Label(master, textvariable=self.avg_waiting_time).grid(row=10, column=1)
def run(self):
self.processes = []
for name_var, burst_time_var, arrival_time_var in self.process_rows:
if name_var.get() and burst_time_var.get() and arrival_time_var.get():
process = Process(name_var.get(), burst_time_var.get(), arrival_time_var.get())
self.processes.append(process)
rr = RoundRobin(self.processes, self.time_slice.get())
rr.run()
self.avg_turnaround_time.set(round(rr.get_avg_turnaround_time(), 2))
self.avg_waiting_time.set(round(rr.get_avg_waiting_time(), 2))
root = tk.Tk()
app = Application(root)
root.mainloop()
```
运行程序后,可以在图形化界面中输入时间片大小和进程信息,然后点击“Run”按钮开始模拟。模拟完成后,程序会计算出平均周转时间和平均等待时间,并在界面中进行展示。
阅读全文