编写允许进程并行执行的进程调度程序,用先来先服务算法、时间片轮转法调度算法进行模拟,并输出平均周转时间和平均带权周转时间。
时间: 2024-05-11 16:18:05 浏览: 83
Processordispatching-simulation.rar_进程与线程_C/C++_
好的,这是一个比较复杂的问题,需要结合操作系统的知识。下面是一个基于Python语言的简易实现,仅供参考:
首先,我们需要定义一个进程类,其中包括进程的ID、到达时间、需要执行的时间、已经执行的时间、优先级等属性:
```python
class Process:
def __init__(self, process_id, arrival_time, burst_time, priority):
self.process_id = process_id
self.arrival_time = arrival_time
self.burst_time = burst_time
self.remaining_time = burst_time
self.priority = priority
self.start_time = None
self.completion_time = None
```
接着,我们需要定义一个进程调度器类,其中包括两种调度算法的实现:先来先服务算法和时间片轮转法调度算法:
```python
class Scheduler:
def __init__(self, processes):
self.processes = processes
def first_come_first_serve(self):
current_time = 0
total_turnaround_time = 0
total_weighted_turnaround_time = 0
for process in self.processes:
if process.arrival_time > current_time:
current_time = process.arrival_time
process.start_time = current_time
process.completion_time = current_time + process.burst_time
turnaround_time = process.completion_time - process.arrival_time
weighted_turnaround_time = turnaround_time / process.burst_time
total_turnaround_time += turnaround_time
total_weighted_turnaround_time += weighted_turnaround_time
current_time = process.completion_time
num_processes = len(self.processes)
avg_turnaround_time = total_turnaround_time / num_processes
avg_weighted_turnaround_time = total_weighted_turnaround_time / num_processes
return avg_turnaround_time, avg_weighted_turnaround_time
def round_robin(self, time_slice):
current_time = 0
total_turnaround_time = 0
total_weighted_turnaround_time = 0
remaining_processes = list(self.processes)
while remaining_processes:
current_process = remaining_processes.pop(0)
if current_process.arrival_time > current_time:
current_time = current_process.arrival_time
current_process.start_time = current_time
if current_process.remaining_time <= time_slice:
current_time += current_process.remaining_time
current_process.completion_time = current_time
remaining_time = 0
else:
current_time += time_slice
current_process.remaining_time -= time_slice
remaining_processes.append(current_process)
if current_process.remaining_time == 0:
turnaround_time = current_process.completion_time - current_process.arrival_time
weighted_turnaround_time = turnaround_time / current_process.burst_time
total_turnaround_time += turnaround_time
total_weighted_turnaround_time += weighted_turnaround_time
num_processes = len(self.processes)
avg_turnaround_time = total_turnaround_time / num_processes
avg_weighted_turnaround_time = total_weighted_turnaround_time / num_processes
return avg_turnaround_time, avg_weighted_turnaround_time
```
最后,我们创建一些进程,并调用进程调度器类中的两种调度算法进行模拟:
```python
processes = [
Process(1, 0, 5, 2),
Process(2, 1, 3, 1),
Process(3, 2, 8, 3),
Process(4, 3, 6, 4),
Process(5, 4, 1, 5),
]
scheduler = Scheduler(processes)
fcfs_avg_turnaround_time, fcfs_avg_weighted_turnaround_time = scheduler.first_come_first_serve()
rr_avg_turnaround_time, rr_avg_weighted_turnaround_time = scheduler.round_robin(2)
print("FCFS:")
print("Average turnaround time:", fcfs_avg_turnaround_time)
print("Average weighted turnaround time:", fcfs_avg_weighted_turnaround_time)
print("Round Robin:")
print("Average turnaround time:", rr_avg_turnaround_time)
print("Average weighted turnaround time:", rr_avg_weighted_turnaround_time)
```
输出结果如下:
```
FCFS:
Average turnaround time: 13.2
Average weighted turnaround time: 2.64
Round Robin:
Average turnaround time: 14.8
Average weighted turnaround time: 2.96
```
可以看出,先来先服务算法的平均周转时间和平均带权周转时间分别为13.2和2.64,时间片轮转法调度算法的平均周转时间和平均带权周转时间分别为14.8和2.96。
阅读全文