高优先权算法计算平均周转时间与平均带权周转时间的代码
时间: 2023-07-29 10:13:34 浏览: 75
以下是高优先权算法计算平均周转时间和平均带权周转时间的代码,使用Python语言实现。
``` python
class Process:
def __init__(self, pid, priority, arrival_time, burst_time):
self.pid = pid
self.priority = priority
self.arrival_time = arrival_time
self.burst_time = burst_time
self.completion_time = 0
self.waiting_time = 0
self.turnaround_time = 0
self.weighted_turnaround_time = 0
def calculate(processes):
n = len(processes)
total_waiting_time = 0
total_turnaround_time = 0
total_weighted_turnaround_time = 0
# Sort processes by priority (higher priority first)
processes = sorted(processes, key=lambda x: x.priority, reverse=True)
# Calculate completion time, waiting time, turnaround time, and weighted turnaround time for each process
current_time = 0
for i in range(n):
current_time += processes[i].burst_time
processes[i].completion_time = current_time
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time
processes[i].weighted_turnaround_time = processes[i].turnaround_time / processes[i].burst_time
# Calculate total waiting time, turnaround time, and weighted turnaround time
for i in range(n):
total_waiting_time += processes[i].waiting_time
total_turnaround_time += processes[i].turnaround_time
total_weighted_turnaround_time += processes[i].weighted_turnaround_time
# Calculate average waiting time, turnaround time, and weighted turnaround time
avg_waiting_time = total_waiting_time / n
avg_turnaround_time = total_turnaround_time / n
avg_weighted_turnaround_time = total_weighted_turnaround_time / n
return avg_waiting_time, avg_turnaround_time, avg_weighted_turnaround_time
```
其中,Process类表示一个进程,包含进程ID、优先级、到达时间、执行时间等属性,以及计算出的完成时间、等待时间、周转时间和带权周转时间。calculate函数接收一个进程列表作为输入,返回平均等待时间、平均周转时间和平均带权周转时间。在函数内部,首先根据优先级对进程进行排序,然后计算每个进程的完成时间、等待时间、周转时间和带权周转时间。最后,计算所有进程的平均等待时间、平均周转时间和平均带权周转时间,并返回这些值。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)