使用三个调度算法的linux进程调度算法模拟代码
时间: 2024-04-07 08:33:01 浏览: 117
进程调度模拟包含3种算法
3星 · 编辑精心推荐
好的,下面是使用三种常见的 Linux 进程调度算法(先来先服务、最短作业优先和时间片轮转)的模拟代码:
```python
import random
# 定义进程类
class Process:
def __init__(self, pid, arrival_time, burst_time):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.waiting_time = 0
def run(self):
self.burst_time -= 1
def is_finished(self):
return self.burst_time == 0
# 定义先来先服务调度算法模拟函数
def fcfs_simulate(processes):
current_time = 0
total_waiting_time = 0
total_processes = len(processes)
completed_processes = []
while len(completed_processes) < total_processes:
available_processes = [p for p in processes if p.arrival_time <= current_time and p not in completed_processes]
if available_processes:
next_process = min(available_processes, key=lambda p: p.arrival_time)
next_process.run()
if next_process.is_finished():
completed_processes.append(next_process)
total_waiting_time += next_process.waiting_time
else:
for p in available_processes:
if p != next_process:
p.waiting_time += 1
current_time += 1
average_waiting_time = total_waiting_time / total_processes
return average_waiting_time
# 定义最短作业优先调度算法模拟函数
def sjf_simulate(processes):
current_time = 0
total_waiting_time = 0
total_processes = len(processes)
completed_processes = []
while len(completed_processes) < total_processes:
available_processes = [p for p in processes if p.arrival_time <= current_time and p not in completed_processes]
if available_processes:
next_process = min(available_processes, key=lambda p: p.burst_time)
next_process.run()
if next_process.is_finished():
completed_processes.append(next_process)
total_waiting_time += next_process.waiting_time
else:
for p in available_processes:
if p != next_process:
p.waiting_time += 1
current_time += 1
average_waiting_time = total_waiting_time / total_processes
return average_waiting_time
# 定义时间片轮转调度算法模拟函数
def round_robin_simulate(processes, time_quantum):
current_time = 0
total_waiting_time = 0
total_processes = len(processes)
completed_processes = []
while len(completed_processes) < total_processes:
available_processes = [p for p in processes if p.arrival_time <= current_time and p not in completed_processes]
if available_processes:
next_process = available_processes[0]
next_process.run()
if next_process.is_finished():
completed_processes.append(next_process)
total_waiting_time += next_process.waiting_time
else:
for p in available_processes[1:]:
p.waiting_time += time_quantum
current_time += time_quantum
average_waiting_time = total_waiting_time / total_processes
return average_waiting_time
# 创建进程列表
processes = [
Process(1, 0, 5),
Process(2, 2, 3),
Process(3, 4, 4),
Process(4, 6, 2),
]
# 运行先来先服务调度算法模拟
average_waiting_time_fcfs = fcfs_simulate(processes)
print(f"Average waiting time (FCFS): {average_waiting_time_fcfs}")
# 运行最短作业优先调度算法模拟
average_waiting_time_sjf = sjf_simulate(processes)
print(f"Average waiting time (SJF): {average_waiting_time_sjf}")
# 运行时间片轮转调度算法模拟
time_quantum = 2
average_waiting_time_rr = round_robin_simulate(processes, time_quantum)
print(f"Average waiting time (Round Robin): {average_waiting_time_rr}")
```
这段代码演示了三种不同的调度算法:先来先服务(FCFS)、最短作业优先(SJF)和时间片轮转(Round Robin)。您可以根据需要修改进程列表和时间片大小来进行模拟,并获取每种算法的平均等待时间。请注意,这只是一个简单的示例,实际的调度算法实现要更加复杂。
阅读全文