112 lines
4.0 KiB
Python
112 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
高响应比优先调度算法 (HRRN)
|
|
支持 IO 模拟
|
|
"""
|
|
|
|
from typing import List, Dict
|
|
import heapq
|
|
|
|
from base import (
|
|
Process,
|
|
ProcessScheduler,
|
|
generate_random_processes,
|
|
print_processes
|
|
)
|
|
|
|
|
|
class HRRNScheduler(ProcessScheduler):
|
|
"""高响应比优先调度
|
|
|
|
响应比 = (等待时间 + 服务时间) / 服务时间
|
|
|
|
支持 IO 模拟
|
|
"""
|
|
|
|
def schedule(self) -> Dict:
|
|
"""HRRN 调度算法 (支持IO模拟)"""
|
|
all_processes = list(self.processes)
|
|
for p in all_processes:
|
|
self.init_process(p, p.arrival_time)
|
|
|
|
events = []
|
|
for p in all_processes:
|
|
heapq.heappush(events, (p.arrival_time, 'arrival', id(p), p))
|
|
|
|
ready_queue = [] # (response_ratio, pid, process)
|
|
completed = []
|
|
|
|
current_time = 0
|
|
|
|
while len(completed) < len(all_processes):
|
|
while events and events[0][0] <= current_time:
|
|
event_time, event_type, uid, p = heapq.heappop(events)
|
|
|
|
if event_type == 'arrival':
|
|
p.status = self.STATUS_READY
|
|
ready_queue.append(p)
|
|
elif event_type == 'io_complete':
|
|
if p.status == self.STATUS_IO_WAIT:
|
|
p.status = self.STATUS_READY
|
|
ready_queue.append(p)
|
|
|
|
if not ready_queue:
|
|
if events:
|
|
current_time = events[0][0]
|
|
continue
|
|
else:
|
|
break
|
|
|
|
# 计算响应比
|
|
for p in ready_queue:
|
|
wait_time = current_time - p.arrival_time
|
|
remaining = p.remaining_cpu_time if p.remaining_cpu_time > 0 else p.total_cpu_time
|
|
if remaining > 0:
|
|
p.response_ratio = (wait_time + remaining) / remaining
|
|
else:
|
|
p.response_ratio = float('inf')
|
|
|
|
ready_queue.sort(key=lambda x: (-x.response_ratio, id(x)))
|
|
current_process = ready_queue.pop(0)
|
|
|
|
if current_process.start_time == -1:
|
|
current_process.start_time = current_time
|
|
|
|
current_process.status = self.STATUS_RUNNING
|
|
|
|
if current_process.current_cpu_idx < len(current_process.cpu_bursts):
|
|
cpu_burst = current_process.cpu_bursts[current_process.current_cpu_idx]
|
|
|
|
self.gantt_chart.append((current_time, current_process.pid, 'CPU', cpu_burst))
|
|
|
|
current_time += cpu_burst
|
|
current_process.remaining_cpu_time -= cpu_burst
|
|
current_process.current_cpu_idx += 1
|
|
|
|
io_idx = current_process.current_cpu_idx - 1
|
|
if io_idx < len(current_process.io_bursts):
|
|
io_time = current_process.io_bursts[io_idx]
|
|
current_process.status = self.STATUS_IO_WAIT
|
|
heapq.heappush(events, (current_time + io_time, 'io_complete', id(current_process), current_process))
|
|
else:
|
|
current_process.status = self.STATUS_TERMINATED
|
|
current_process.completion_time = current_time
|
|
self.calculate_metrics(current_process, current_time)
|
|
completed.append(current_process)
|
|
else:
|
|
current_process.status = self.STATUS_TERMINATED
|
|
current_process.completion_time = current_time
|
|
self.calculate_metrics(current_process, current_time)
|
|
completed.append(current_process)
|
|
|
|
self.results = completed
|
|
return self.print_results("HRRN (高响应比优先)")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
processes = generate_random_processes(n=5, seed=42, io_probability=0.5)
|
|
print_processes(processes, "测试数据")
|
|
|
|
scheduler = HRRNScheduler(processes)
|
|
scheduler.schedule()
|