实现高响应比优先调度算法的c语言代码
时间: 2023-08-13 14:09:53 浏览: 104
以下是一个简单的高响应比优先调度算法的 C 语言代码实现:
```
#include <stdio.h>
struct process {
int pid;
int arrival_time;
int burst_time;
int priority;
int waiting_time;
int turnaround_time;
int response_time;
int remaining_time;
};
void sort_processes(struct process [], int);
int find_next_process(struct process [], int, int);
void calculate_times(struct process [], int);
int main() {
int n, i, time_quantum;
float avg_waiting_time = 0, avg_turnaround_time = 0, avg_response_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process proc[n];
for (i = 0; i < n; i++) {
printf("\nEnter arrival time, burst time and priority for process %d: ", i + 1);
scanf("%d%d%d", &proc[i].arrival_time, &proc[i].burst_time, &proc[i].priority);
proc[i].pid = i + 1;
proc[i].remaining_time = proc[i].burst_time;
}
sort_processes(proc, n);
printf("\nEnter time quantum: ");
scanf("%d", &time_quantum);
int current_time = 0, completed = 0, current_pid = -1;
while (completed < n) {
int next_pid = find_next_process(proc, n, current_time);
if (next_pid != -1 && next_pid != current_pid) {
proc[next_pid].response_time = current_time - proc[next_pid].arrival_time;
}
if (current_pid != -1 && proc[current_pid].remaining_time == 0) {
completed++;
proc[current_pid].turnaround_time = current_time - proc[current_pid].arrival_time;
proc[current_pid].waiting_time = proc[current_pid].turnaround_time - proc[current_pid].burst_time;
}
if (next_pid != -1) {
if (current_pid != next_pid) {
proc[next_pid].waiting_time += current_time - proc[next_pid].response_time;
}
if (proc[next_pid].remaining_time <= time_quantum) {
current_time += proc[next_pid].remaining_time;
proc[next_pid].remaining_time = 0;
} else {
current_time += time_quantum;
proc[next_pid].remaining_time -= time_quantum;
}
current_pid = next_pid;
} else {
current_time++;
}
}
calculate_times(proc, n);
for (i = 0; i < n; i++) {
avg_waiting_time += proc[i].waiting_time;
avg_turnaround_time += proc[i].turnaround_time;
avg_response_time += proc[i].response_time;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
avg_response_time /= n;
printf("\nAverage Waiting Time: %.2f", avg_waiting_time);
printf("\nAverage Turnaround Time: %.2f", avg_turnaround_time);
printf("\nAverage Response Time: %.2f", avg_response_time);
return 0;
}
void sort_processes(struct process proc[], int n) {
int i, j;
struct process temp;
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (proc[i].arrival_time > proc[j].arrival_time) {
temp = proc[i];
proc[i] = proc[j];
proc[j] = temp;
}
}
}
}
int find_next_process(struct process proc[], int n, int current_time) {
int i, highest_priority = -1, next_pid = -1;
for (i = 0; i < n; i++) {
if (proc[i].arrival_time <= current_time && proc[i].remaining_time > 0) {
float response_ratio = 1 + (current_time - proc[i].arrival_time) / (float) proc[i].burst_time;
if (proc[i].priority > highest_priority || (proc[i].priority == highest_priority && response_ratio > proc[next_pid].priority)) {
highest_priority = proc[i].priority;
next_pid = i;
}
}
}
return next_pid;
}
void calculate_times(struct process proc[], int n) {
int i;
for (i = 0; i < n; i++) {
proc[i].turnaround_time = proc[i].burst_time + proc[i].waiting_time;
}
}
```
该算法使用一个结构体数组来存储每个进程的信息,包括 PID、到达时间、执行时间、优先级等等。首先,进程按照到达时间排序。然后,算法开始执行循环,直到所有进程都完成为止。在每个时间步骤中,算法会寻找下一个要执行的进程,根据其优先级和响应比来选择。如果当前进程已经执行完毕,那么就计算其等待时间和周转时间。当所有进程都完成时,算法计算平均等待时间、平均周转时间和平均响应时间,并将它们输出到控制台。
相关推荐
![](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)