HRRN算法代码C语言
时间: 2023-11-02 20:07:45 浏览: 58
好的,以下是HRRN算法的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct process {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
float response_ratio; // 响应比
};
int main() {
int n, i, j;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process p[n], temp;
for (i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i + 1);
scanf("%d%d", &p[i].arrival_time, &p[i].burst_time);
p[i].pid = i + 1;
p[i].waiting_time = p[i].turnaround_time = 0;
p[i].response_ratio = 0.0;
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (p[i].arrival_time > p[j].arrival_time) {
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
int current_time = 0, completed = 0, max_response_ratio_index;
while (completed != n) {
float max_response_ratio = -1.0;
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= current_time && p[i].burst_time > 0) {
p[i].response_ratio = (float)(p[i].burst_time + p[i].waiting_time) / p[i].burst_time;
if (p[i].response_ratio > max_response_ratio) {
max_response_ratio = p[i].response_ratio;
max_response_ratio_index = i;
}
}
}
p[max_response_ratio_index].burst_time--;
current_time++;
if (p[max_response_ratio_index].burst_time == 0) {
completed++;
p[max_response_ratio_index].turnaround_time = current_time - p[max_response_ratio_index].arrival_time;
p[max_response_ratio_index].waiting_time = p[max_response_ratio_index].turnaround_time - (p[max_response_ratio_index].burst_time + p[max_response_ratio_index].waiting_time);
avg_waiting_time += p[max_response_ratio_index].waiting_time;
avg_turnaround_time += p[max_response_ratio_index].turnaround_time;
}
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nPID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time);
}
printf("Average waiting time: %.2f\n", avg_waiting_time);
printf("Average turnaround time: %.2f\n", avg_turnaround_time);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)