hrrn调度算法python
时间: 2023-11-08 08:05:31 浏览: 79
根据提供的引用内容,可以得知HRRN调度算法是一种作业调度算法,它是响应比高者优先的缩写。在HRRN调度算法中,作业的优先级是根据它们的响应比计算的,响应比是作业等待时间与作业服务时间之和的比值。HRRN调度算法的优点是可以避免饥饿现象,即长时间等待的作业不会被无限期地推迟。下面是一个使用Python实现HRRN调度算法的例子:
```python
class Job:
def __init__(self, name, arrival_time, service_time):
self.name = name
self.arrival_time = arrival_time
self.service_time = service_time
self.waiting_time = 0
self.turnaround_time = 0
self.response_ratio = 0
def __str__(self):
return self.name
class HRRNScheduler:
def __init__(self, jobs):
self.jobs = jobs
self.current_time = 0
def run(self):
while self.jobs:
ready_jobs = [job for job in self.jobs if job.arrival_time <= self.current_time]
if not ready_jobs:
self.current_time += 1
continue
selected_job = max(ready_jobs, key=lambda job: (job.waiting_time + job.service_time) / job.service_time)
selected_job.waiting_time = self.current_time - selected_job.arrival_time
selected_job.turnaround_time = selected_job.waiting_time + selected_job.service_time
selected_job.response_ratio = selected_job.turnaround_time / selected_job.service_time
self.current_time += selected_job.service_time
self.jobs.remove(selected_job)
print(f"{selected_job.name} start time: {self.current_time - selected_job.service_time}, "
f"finish time: {self.current_time}, turnaround time: {selected_job.turnaround_time}, "
f"weighted turnaround time: {selected_job.response_ratio}")
```
上述代码中,Job类表示一个作业,包含作业名称、到达时间、服务时间、等待时间、周转时间和响应比等属性。HRRNScheduler类表示HRRN调度器,包含作业列表和当前时间等属性,以及run()方法来运行调度算法。在run()方法中,首先找到所有已到达的作业,然后根据响应比选择下一个要运行的作业。选择作业后,计算它的等待时间、周转时间和响应比,并将当前时间增加作业的服务时间。最后,从作业列表中删除已完成的作业,并输出作业的相关信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)