可视化进程调度算法实现代码的代码
时间: 2023-07-09 18:03:47 浏览: 58
这里是一个基于Python的可视化进程调度算法实现代码的示例:
```python
import time
from tkinter import *
# 定义进程类
class Process:
def __init__(self, name, arrive_time, burst_time):
self.name = name
self.arrive_time = arrive_time
self.burst_time = burst_time
self.wait_time = 0
self.turnaround_time = 0
# 定义主窗口类
class MainWindow:
def __init__(self):
self.processes = [] # 进程列表
self.algorithm = None # 调度算法
self.canvas = None # 画布
self.width = 800 # 窗口宽度
self.height = 600 # 窗口高度
self.scale = 20 # 时间轴比例尺
self.current_time = 0 # 当前时间
self.quantum = 1 # 时间片大小
self.running_process = None # 正在运行的进程
self.waiting_processes = [] # 等待运行的进程
self.finished_processes = [] # 已完成的进程
self.avg_wait_time = 0 # 平均等待时间
self.avg_turnaround_time = 0 # 平均周转时间
self.create_window() # 创建窗口
# 创建窗口
def create_window(self):
self.root = Tk()
self.root.title('进程调度算法可视化')
self.root.geometry('%dx%d' % (self.width, self.height))
self.canvas = Canvas(self.root, width=self.width, height=self.height, bg='white')
self.canvas.pack()
self.create_menu() # 创建菜单
self.create_controls() # 创建控件
self.root.mainloop()
# 创建菜单
def create_menu(self):
menu = Menu(self.root)
self.root.config(menu=menu)
file_menu = Menu(menu)
menu.add_cascade(label='文件', menu=file_menu)
file_menu.add_command(label='打开', command=self.open_file)
file_menu.add_command(label='保存', command=self.save_file)
file_menu.add_separator()
file_menu.add_command(label='退出', command=self.root.quit)
algorithm_menu = Menu(menu)
menu.add_cascade(label='算法', menu=algorithm_menu)
algorithm_menu.add_radiobutton(label='先来先服务', variable=self.algorithm, value='fcfs', command=self.set_algorithm)
algorithm_menu.add_radiobutton(label='短作业优先', variable=self.algorithm, value='sjf', command=self.set_algorithm)
algorithm_menu.add_radiobutton(label='时间片轮转', variable=self.algorithm, value='rr', command=self.set_algorithm)
# 创建控件
def create_controls(self):
label1 = Label(self.root, text='进程名称')
label1.place(x=50, y=50)
self.entry1 = Entry(self.root)
self.entry1.place(x=150, y=50)
label2 = Label(self.root, text='到达时间')
label2.place(x=50, y=100)
self.entry2 = Entry(self.root)
self.entry2.place(x=150, y=100)
label3 = Label(self.root, text='服务时间')
label3.place(x=50, y=150)
self.entry3 = Entry(self.root)
self.entry3.place(x=150, y=150)
button1 = Button(self.root, text='添加进程', command=self.add_process)
button1.place(x=50, y=200)
button2 = Button(self.root, text='开始调度', command=self.start_scheduler)
button2.place(x=150, y=200)
self.label4 = Label(self.root, text='')
self.label4.place(x=50, y=250)
self.label5 = Label(self.root, text='')
self.label5.place(x=50, y=300)
# 打开文件
def open_file(self):
pass
# 保存文件
def save_file(self):
pass
# 设置调度算法
def set_algorithm(self):
pass
# 添加进程
def add_process(self):
name = self.entry1.get()
arrive_time = int(self.entry2.get())
burst_time = int(self.entry3.get())
process = Process(name, arrive_time, burst_time)
self.processes.append(process)
self.entry1.delete(0, END)
self.entry2.delete(0, END)
self.entry3.delete(0, END)
# 开始调度
def start_scheduler(self):
if not self.processes:
return
self.processes.sort(key=lambda x: x.arrive_time) # 按到达时间排序
self.canvas.delete('all') # 清空画布
self.draw_axes() # 绘制坐标轴
if self.algorithm == 'fcfs':
self.fcfs_scheduler()
elif self.algorithm == 'sjf':
self.sjf_scheduler()
elif self.algorithm == 'rr':
self.rr_scheduler()
self.calculate_metrics() # 计算性能指标
self.show_metrics() # 显示性能指标
# 先来先服务调度算法
def fcfs_scheduler(self):
for i, process in enumerate(self.processes):
if process.arrive_time > self.current_time:
self.current_time = process.arrive_time
process.wait_time = self.current_time - process.arrive_time
process.turnaround_time = process.wait_time + process.burst_time
self.draw_process(process, i)
self.current_time += process.burst_time
self.finished_processes.append(process)
# 短作业优先调度算法
def sjf_scheduler(self):
self.waiting_processes = self.processes[:] # 复制进程列表
while self.waiting_processes:
self.waiting_processes.sort(key=lambda x: x.burst_time) # 按服务时间排序
process = self.waiting_processes.pop(0)
if process.arrive_time > self.current_time:
self.current_time = process.arrive_time
process.wait_time = self.current_time - process.arrive_time
process.turnaround_time = process.wait_time + process.burst_time
self.draw_process(process, self.processes.index(process))
self.current_time += process.burst_time
self.finished_processes.append(process)
# 时间片轮转调度算法
def rr_scheduler(self):
self.waiting_processes = self.processes[:] # 复制进程列表
while self.waiting_processes or self.running_process:
if not self.running_process:
self.running_process = self.waiting_processes.pop(0)
if self.running_process.arrive_time > self.current_time:
self.current_time = self.running_process.arrive_time
self.draw_process(self.running_process, self.processes.index(self.running_process))
elif self.running_process.burst_time <= self.quantum:
self.running_process.wait_time = self.current_time - self.running_process.arrive_time - self.running_process.burst_time
self.running_process.turnaround_time = self.running_process.wait_time + self.running_process.burst_time
self.current_time += self.running_process.burst_time
self.finished_processes.append(self.running_process)
self.running_process = None
else:
self.running_process.burst_time -= self.quantum
self.current_time += self.quantum
self.draw_process(self.running_process, self.processes.index(self.running_process))
# 绘制坐标轴
def draw_axes(self):
self.canvas.create_line(100, 500, 700, 500, width=2) # x轴
self.canvas.create_line(100, 500, 100, 100, width=2) # y轴
self.canvas.create_text(50, 500, text='0') # 原点
self.canvas.create_text(95, 500, text='时间')
self.canvas.create_text(50, 100, text='进程')
for i in range(1, 31):
x = 100 + i * self.scale
self.canvas.create_line(x, 500, x, 490)
self.canvas.create_text(x, 510, text=str(i))
for i, process in enumerate(self.processes):
y = 450 - i * 20
self.canvas.create_text(50, y, text=process.name)
# 绘制进程
def draw_process(self, process, index):
x1 = 100 + process.arrive_time * self.scale
y1 = 460 - index * 20
x2 = x1 + process.burst_time * self.scale
y2 = y1 + 10
self.canvas.create_rectangle(x1, y1, x2, y2, fill='gray')
self.canvas.create_text(x1, y1 - 10, text=process.name)
self.canvas.create_text(x1, y1 + 5, text=str(process.arrive_time))
self.canvas.create_text(x2, y2 + 5, text=str(process.arrive_time + process.burst_time))
# 计算性能指标
def calculate_metrics(self):
wait_times = [process.wait_time for process in self.finished_processes]
turnaround_times = [process.turnaround_time for process in self.finished_processes]
self.avg_wait_time = sum(wait_times) / len(wait_times)
self.avg_turnaround_time = sum(turnaround_times) / len(turnaround_times)
# 显示性能指标
def show_metrics(self):
self.label4.config(text='平均等待时间:%f' % self.avg_wait_time)
self.label5.config(text='平均周转时间:%f' % self.avg_turnaround_time)
# 创建主窗口
window = MainWindow()
```
注意:这只是一个示例代码,需要根据实际情况进行修改和完善。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)