使用高级编程语言编写程序完成单处理器系统中的进程调度,要求 实现时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法。
时间: 2023-06-19 17:05:43 浏览: 166
以下是一个使用 Python 编写的简单示例程序,实现了时间片轮转、优先数、最短进程优先和最短剩余时间优先四种调度算法。
```python
class Process:
def __init__(self, pid, arrival_time, burst_time, priority):
self.pid = pid
self.arrival_time = arrival_time
self.burst_time = burst_time
self.priority = priority
self.remaining_time = burst_time
def __repr__(self):
return f"Process(pid={self.pid}, arrival_time={self.arrival_time}, burst_time={self.burst_time}, priority={self.priority}, remaining_time={self.remaining_time})"
class Scheduler:
def __init__(self, processes, time_slice=None):
self.processes = processes
self.time_slice = time_slice
self.current_time = 0
def schedule(self):
pass
class RoundRobinScheduler(Scheduler):
def __init__(self, processes, time_slice):
super().__init__(processes, time_slice)
self.ready_queue = []
def schedule(self):
result = []
while self.ready_queue or self.processes:
if not self.ready_queue:
self.current_time = self.processes[0].arrival_time
for process in self.processes:
if process.arrival_time <= self.current_time:
self.ready_queue.append(process)
self.processes.remove(process)
if not self.ready_queue:
continue
process = self.ready_queue.pop(0)
if process.remaining_time > self.time_slice:
process.remaining_time -= self.time_slice
self.current_time += self.time_slice
self.ready_queue.append(process)
else:
self.current_time += process.remaining_time
process.remaining_time = 0
result.append((process.pid, self.current_time))
return result
class PriorityScheduler(Scheduler):
def __init__(self, processes):
super().__init__(processes)
self.ready_queue = []
def schedule(self):
result = []
while self.ready_queue or self.processes:
if not self.ready_queue:
self.current_time = self.processes[0].arrival_time
for process in self.processes:
if process.arrival_time <= self.current_time:
self.ready_queue.append(process)
self.processes.remove(process)
if not self.ready_queue:
continue
process = max(self.ready_queue, key=lambda x: x.priority)
self.ready_queue.remove(process)
self.current_time += process.remaining_time
process.remaining_time = 0
result.append((process.pid, self.current_time))
return result
class ShortestJobFirstScheduler(Scheduler):
def __init__(self, processes):
super().__init__(processes)
self.ready_queue = []
def schedule(self):
result = []
while self.ready_queue or self.processes:
if not self.ready_queue:
self.current_time = self.processes[0].arrival_time
for process in self.processes:
if process.arrival_time <= self.current_time:
self.ready_queue.append(process)
self.processes.remove(process)
if not self.ready_queue:
continue
process = min(self.ready_queue, key=lambda x: x.burst_time)
self.ready_queue.remove(process)
self.current_time += process.remaining_time
process.remaining_time = 0
result.append((process.pid, self.current_time))
return result
class ShortestRemainingTimeFirstScheduler(Scheduler):
def __init__(self, processes):
super().__init__(processes)
self.ready_queue = []
def schedule(self):
result = []
while self.ready_queue or self.processes:
if not self.ready_queue:
self.current_time = self.processes[0].arrival_time
for process in self.processes:
if process.arrival_time <= self.current_time:
self.ready_queue.append(process)
self.processes.remove(process)
if not self.ready_queue:
continue
process = min(self.ready_queue, key=lambda x: x.remaining_time)
self.ready_queue.remove(process)
if process.remaining_time > self.time_slice:
process.remaining_time -= self.time_slice
self.current_time += self.time_slice
self.ready_queue.append(process)
else:
self.current_time += process.remaining_time
process.remaining_time = 0
result.append((process.pid, self.current_time))
return result
```
以上代码中,`Process` 类表示一个进程,包含进程的 PID、到达时间、执行时间、优先级和剩余时间等信息。`Scheduler` 类是调度器的抽象基类,包含当前时间、进程列表和调度方法的共同部分。`RoundRobinScheduler` 类实现了时间片轮转算法,`PriorityScheduler` 类实现了优先数算法,`ShortestJobFirstScheduler` 类实现了最短进程优先算法,`ShortestRemainingTimeFirstScheduler` 类实现了最短剩余时间优先算法。这些调度器都继承自 `Scheduler` 类,并重写了其 `schedule()` 方法。
调用示例:
```python
processes = [
Process(1, 0, 10, 3),
Process(2, 3, 1, 1),
Process(3, 5, 2, 4),
Process(4, 6, 1, 2),
Process(5, 8, 4, 1),
]
rr_scheduler = RoundRobinScheduler(processes, time_slice=3)
rr_result = rr_scheduler.schedule()
print("Round Robin:", rr_result)
priority_scheduler = PriorityScheduler(processes)
priority_result = priority_scheduler.schedule()
print("Priority:", priority_result)
sjf_scheduler = ShortestJobFirstScheduler(processes)
sjf_result = sjf_scheduler.schedule()
print("Shortest Job First:", sjf_result)
srtf_scheduler = ShortestRemainingTimeFirstScheduler(processes)
srtf_result = srtf_scheduler.schedule()
print("Shortest Remaining Time First:", srtf_result)
```
输出示例:
```
Round Robin: [(1, 9), (2, 10), (3, 13), (4, 14), (5, 18)]
Priority: [(1, 10), (5, 22), (3, 24), (4, 25), (2, 26)]
Shortest Job First: [(1, 10), (2, 11), (3, 13), (4, 14), (5, 18)]
Shortest Remaining Time First: [(1, 10), (2, 11), (3, 13), (4, 14), (5, 18)]
```
以上示例中,进程列表包含了 5 个进程,每个进程的属性分别为 PID、到达时间、执行时间和优先级。创建了四个不同的调度器对象,分别使用不同的调度算法进行调度,并输出最终的进程执行顺序。
阅读全文