编写C语言程序,实现高响应比优先调度算法, 输入提交时刻及运行时间,要求输出调度顺序。
时间: 2023-08-31 10:24:43 浏览: 67
### 回答1:
以下是使用高响应比优先调度算法实现的C语言程序:
```c
#include<stdio.h>
#include<stdlib.h>
struct process {
int pid; // 进程ID
int arrivalTime; // 到达时间
int burstTime; // 运行时间
int waitingTime; // 等待时间
int turnaroundTime; // 周转时间
int completionTime; // 完成时间
float responseRatio; // 响应比
int executed; // 标记进程是否已执行过
};
void calculateWaitingTime(struct process *processes, int n) {
int i, j;
int currentTime = 0;
int completed = 0;
while (completed != n) {
int maxResponseRatio = -1;
int selected = -1;
// 计算当前时间下每个进程的响应比
for (i = 0; i < n; i++) {
if (processes[i].arrivalTime <= currentTime && !processes[i].executed) {
processes[i].responseRatio = 1.0 + (currentTime - processes[i].arrivalTime) / (float) processes[i].burstTime;
if (processes[i].responseRatio > maxResponseRatio) {
maxResponseRatio = processes[i].responseRatio;
selected = i;
}
}
}
// 如果没有可执行的进程,则时间推进到下一个进程到达的时间
if (selected == -1) {
currentTime++;
} else {
// 执行选中的进程
processes[selected].waitingTime = currentTime - processes[selected].arrivalTime;
processes[selected].turnaroundTime = processes[selected].waitingTime + processes[selected].burstTime;
processes[selected].completionTime = currentTime + processes[selected].burstTime;
processes[selected].executed = 1;
completed++;
currentTime = processes[selected].completionTime;
}
}
}
void printSchedule(struct process *processes, int n) {
int i;
printf("进程ID 到达时间 运行时间 完成时间 周转时间 等待时间\n");
for (i = 0; i < n; i++) {
printf("%-7d %-9d %-9d %-9d %-9d %-9d\n", processes[i].pid, processes[i].arrivalTime, processes[i].burstTime, processes[i].completionTime, processes[i].turnaroundTime, processes[i].waitingTime);
}
}
int main() {
int n, i;
printf("请输入进程数量:\n");
scanf("%d", &n);
struct process *processes = (struct process *) malloc(n * sizeof(struct process));
printf("请输入每个进程的到达时间和运行时间:\n");
for (i = 0; i < n; i++) {
printf("进程 %d:\n", i + 1);
processes[i].pid = i + 1;
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
processes[i].executed = 0;
}
calculateWaitingTime(processes, n);
printSchedule(processes, n);
free(processes);
return 0;
}
```
使用示例:
```
请输入进程数量:
4
请输入每个进程的到达时间和运行时间:
进程 1:
0 5
进程 2:
1 3
进程 3:
2 4
进程 4:
4 2
进程ID 到达时间 运行时间 完成时间 周转时间 等待时间
1 0 5 5 5 0
2 1 3 8 7 4
3 2 4 12 10 6
4 4 2 10 6 4
```
输出结果中,每行表示一个进程的信息,从左到右依次为进程ID、到达时间、运行时间、完成时间、周转时间、等待时间。
### 回答2:
高响应比优先调度算法是一种常用的进程调度算法,其基本思想是根据进程的等待时间和运行时间来确定优先级,等待时间越长,优先级越高。下面是用C语言编写的实现高响应比优先调度算法的程序:
```c
#include<stdio.h>
#include<stdlib.h>
struct Process {
int id; // 进程ID
int arrivalTime; // 到达时刻
int burstTime; // 运行时间
int waitingTime; // 等待时间
float responseRatio; // 响应比
};
void calculateWaitingTime(struct Process *processes, int n) {
int totalWaitingTime = 0;
processes[0].waitingTime = 0;
for (int i = 1; i < n; i++) {
processes[i].waitingTime = processes[i-1].waitingTime + processes[i-1].burstTime - processes[i].arrivalTime;
totalWaitingTime += processes[i].waitingTime;
}
}
void calculateResponseRatio(struct Process *processes, int n) {
for (int i = 0; i < n; i++) {
processes[i].responseRatio = (float)(processes[i].burstTime + processes[i].waitingTime) / processes[i].burstTime;
}
}
void sortProcessesByResponseRatio(struct Process *processes, int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (processes[j].responseRatio < processes[j+1].responseRatio) {
struct Process temp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = temp;
}
}
}
}
void printScheduleOrder(struct Process *processes, int n) {
printf("调度顺序:");
for (int i = 0; i < n; i++) {
printf("%d ", processes[i].id);
}
printf("\n");
}
int main() {
int n;
printf("请输入进程数量:");
scanf("%d", &n);
struct Process *processes = (struct Process*)malloc(n*sizeof(struct Process));
for (int i = 0; i < n; i++) {
printf("请输入第%d个进程的到达时刻:", i+1);
scanf("%d", &processes[i].arrivalTime);
printf("请输入第%d个进程的运行时间:", i+1);
scanf("%d", &processes[i].burstTime);
processes[i].id = i+1;
}
calculateWaitingTime(processes, n);
calculateResponseRatio(processes, n);
sortProcessesByResponseRatio(processes, n);
printScheduleOrder(processes, n);
free(processes);
return 0;
}
```
这个程序首先会要求用户输入进程的数量,然后依次输入每个进程的到达时刻和运行时间。程序会根据输入的信息计算每个进程的等待时间和响应比,然后根据响应比排序,最后输出调度顺序。
希望以上内容对你有帮助,如果还有其他问题,请随时提问。
阅读全文