随机体现高响应比调度算法c语言代码
时间: 2023-07-29 10:05:39 浏览: 66
以下是一个简单的随机高响应比调度算法的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_PROCESSES 10
struct process {
int pid;
int burst_time;
int response_ratio;
int waiting_time;
};
int main() {
int i, j, n, total_waiting_time = 0, total_turnaround_time = 0;
float avg_waiting_time, avg_turnaround_time;
struct process processes[MAX_PROCESSES];
srand(time(NULL));
// 输入进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
// 输入每个进程的信息
for (i = 0; i < n; ++i) {
processes[i].pid = i + 1;
printf("Enter the burst time for process %d: ", processes[i].pid);
scanf("%d", &processes[i].burst_time);
processes[i].response_ratio = rand() % 10 + 1; // 随机生成响应比
}
// 随机高响应比调度算法
for (i = 0; i < n - 1; ++i) {
for (j = i + 1; j < n; ++j) {
if (processes[i].response_ratio < processes[j].response_ratio) {
struct process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
// 计算等待时间和周转时间
for (i = 0; i < n; ++i) {
processes[i].waiting_time = total_waiting_time;
total_waiting_time += processes[i].burst_time;
total_turnaround_time += total_waiting_time;
}
// 计算平均等待时间和平均周转时间
avg_waiting_time = (float)total_waiting_time / n;
avg_turnaround_time = (float)total_turnaround_time / n;
// 输出每个进程的信息
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; ++i) {
printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, processes[i].waiting_time, processes[i].waiting_time + processes[i].burst_time);
}
// 输出平均等待时间和平均周转时间
printf("\nAverage Waiting Time: %.2f", avg_waiting_time);
printf("\nAverage Turnaround Time: %.2f", avg_turnaround_time);
return 0;
}
```
该代码实现了一个简单的随机高响应比调度算法,在输入每个进程的信息后,程序会随机生成每个进程的响应比,并根据响应比进行排序。然后,程序计算每个进程的等待时间和周转时间,并输出所有进程的信息以及平均等待时间和平均周转时间。
阅读全文