高响应比调度算法c语言源代码
时间: 2023-08-03 16:03:33 浏览: 106
以下是一个简单的高响应比调度算法的C语言源代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 10
typedef struct Process {
int id; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int completion_time; // 完成时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
float response_ratio; // 响应比
} Process;
int main() {
int n, i, j;
Process processes[MAX_PROCESSES], temp;
// 输入进程数量
printf("Enter the number of processes: ");
scanf("%d", &n);
// 输入每个进程的到达时间和执行时间
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for Process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].id = i + 1;
processes[i].completion_time = 0;
processes[i].waiting_time = 0;
processes[i].turnaround_time = 0;
processes[i].response_ratio = 0;
}
// 计算每个进程的响应比
for (i = 0; i < n; i++) {
processes[i].response_ratio = 1 + (float)(processes[i].waiting_time) / processes[i].burst_time;
}
// 按照响应比排序
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (processes[i].response_ratio < processes[j].response_ratio) {
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
// 计算每个进程的完成时间、等待时间和周转时间
for (i = 0; i < n; i++) {
if (i == 0) {
processes[i].completion_time = processes[i].burst_time;
} else {
processes[i].completion_time = processes[i - 1].completion_time + processes[i].burst_time;
}
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
}
// 输出每个进程的信息
printf("\nProcess\tArrival Time\tBurst Time\tCompletion Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].id, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time, processes[i].waiting_time,
processes[i].turnaround_time);
}
return 0;
}
```
该算法首先输入每个进程的到达时间和执行时间,然后计算每个进程的响应比,并按照响应比从大到小排序。接着,计算每个进程的完成时间、等待时间和周转时间,并输出每个进程的信息。
阅读全文