pcb = [] n = int(input("请输入您的进程数:")) # 进程数目 def inpcb(): i = 0 while (i < n): print("*************************") name = input("请输入第 %d 个进程名" % (i + 1)) arrivetime = int(input("请输入当前进程的到达时间")) servetime = int(input("请输入当前进程的服务时间")) # 添加的列表名分别是进程名,到达时间,服务时间,开始时间,完成时间,周转时间,带权周转时间,响应比。 pcb.append([name, arrivetime, servetime, 0, 0, 0, 0, 0]) i += 1 请基于此列表写一个能够实现hrrn算法的python代码
时间: 2023-08-09 22:04:25 浏览: 31
下面是基于给出的 PCB 列表实现 HRRN 调度算法的 Python 代码:
```python
def hrrn(pcb):
current_time = 0
finished = 0
while finished < len(pcb):
max_hrrn = -1
max_hrrn_index = -1
for i in range(len(pcb)):
if pcb[i][1] <= current_time and pcb[i][4] == 0:
# 计算当前进程的响应比
response_ratio = (current_time - pcb[i][1] + pcb[i][2]) / pcb[i][2]
if response_ratio > max_hrrn:
max_hrrn = response_ratio
max_hrrn_index = i
if max_hrrn_index == -1:
# 若当前时间段内没有进程到达,则当前时间加一
current_time += 1
else:
# 更新 PCB 列表中的开始时间和完成时间
pcb[max_hrrn_index][3] = current_time
pcb[max_hrrn_index][4] = current_time + pcb[max_hrrn_index][2]
# 更新当前时间和已完成进程数
current_time = pcb[max_hrrn_index][4]
finished += 1
# 计算并更新 PCB 列表中的周转时间和带权周转时间
pcb[max_hrrn_index][5] = pcb[max_hrrn_index][4] - pcb[max_hrrn_index][1]
pcb[max_hrrn_index][6] = pcb[max_hrrn_index][5] / pcb[max_hrrn_index][2]
# 输出调度结果
print("进程名 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间")
for i in range(len(pcb)):
print("%s\t%d\t%d\t%d\t%d\t%d\t%.2f" % (pcb[i][0], pcb[i][1], pcb[i][2], pcb[i][3], pcb[i][4], pcb[i][5], pcb[i][6]))
# 调用函数
inpcb()
hrrn(pcb)
```
代码中 `inpcb()` 函数用于输入 PCB 列表,`hrrn(pcb)` 函数用于实现 HRRN 调度算法。算法过程如下:
1. 初始化当前时间为0,已完成进程数为0。
2. 遍历 PCB 列表,对于每一个已到达但未完成的进程,计算其响应比。选择响应比最大的进程作为本次执行的进程。
3. 若当前时间段内没有进程到达,则当前时间加一。
4. 更新本次执行进程的开始时间和完成时间,并更新当前时间和已完成进程数。
5. 若仍有未完成进程,则返回第2步。
6. 输出调度结果。
输出结果中,包含了每个进程的进程名、到达时间、服务时间、开始时间、完成时间、周转时间和带权周转时间。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)