用c语言完成以下功能:实现先来先服务调度算法,实现最短作业优先调度算法,实现最高响应比优先调度算法。 基本要求: (1)手动创建几个进程,或者随机创建几个进程,都在界面上完成;要求包括进程的名称(不能重复)、创建时间、执行时间等。 (2)输出各进程不同调度算法的调度过程,输出周转时间、平均周转时间和平均带权周转时间。对比各算法的优劣,输出对比结果。
时间: 2023-05-19 20:04:25 浏览: 86
C知道:为了实现先来先服务调度算法,最短作业优先调度算法和最高响应比优先调度算法,您需要使用C语言编写程序。您可以手动创建几个进程或随机创建几个进程,包括进程的名称、创建时间和执行时间等。然后,您需要输出各进程不同调度算法的调度过程,并计算周转时间、平均周转时间和平均带权周转时间。最后,您需要对比各算法的优劣,并输出对比结果。
以下是一个示例程序,可以实现您的要求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PROCESS_NUM 10
typedef struct {
char name[10];
int arrive_time;
int execute_time;
int start_time;
int finish_time;
int turnaround_time;
float weighted_turnaround_time;
} Process;
void fcfs(Process processes[], int n);
void sjf(Process processes[], int n);
void hrrn(Process processes[], int n);
int main() {
int n, i;
Process processes[MAX_PROCESS_NUM];
printf("请输入进程数:");
scanf("%d", &n);
printf("请输入每个进程的名称、到达时间和执行时间:\n");
for (i = 0; i < n; i++) {
printf("进程%d:", i + 1);
scanf("%s%d%d", processes[i].name, &processes[i].arrive_time, &processes[i].execute_time);
}
fcfs(processes, n);
sjf(processes, n);
hrrn(processes, n);
return 0;
}
void fcfs(Process processes[], int n) {
int i, j;
float avg_turnaround_time = 0, avg_weighted_turnaround_time = 0;
printf("\n先来先服务调度算法:\n");
printf("进程\t到达时间\t执行时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");
for (i = 0; i < n; i++) {
if (i == 0) {
processes[i].start_time = processes[i].arrive_time;
} else {
processes[i].start_time = processes[i - 1].finish_time;
}
processes[i].finish_time = processes[i].start_time + processes[i].execute_time;
processes[i].turnaround_time = processes[i].finish_time - processes[i].arrive_time;
processes[i].weighted_turnaround_time = (float) processes[i].turnaround_time / processes[i].execute_time;
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrive_time,
processes[i].execute_time, processes[i].start_time, processes[i].finish_time,
processes[i].turnaround_time, processes[i].weighted_turnaround_time);
avg_turnaround_time += processes[i].turnaround_time;
avg_weighted_turnaround_time += processes[i].weighted_turnaround_time;
}
avg_turnaround_time /= n;
avg_weighted_turnaround_time /= n;
printf("平均周转时间:%.2f\n", avg_turnaround_time);
printf("平均带权周转时间:%.2f\n", avg_weighted_turnaround_time);
}
void sjf(Process processes[], int n) {
int i, j;
Process temp;
float avg_turnaround_time = 0, avg_weighted_turnaround_time = 0;
printf("\n最短作业优先调度算法:\n");
printf("进程\t到达时间\t执行时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (processes[i].execute_time > processes[j].execute_time) {
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
if (i == 0) {
processes[i].start_time = processes[i].arrive_time;
} else {
processes[i].start_time = processes[i - 1].finish_time;
}
processes[i].finish_time = processes[i].start_time + processes[i].execute_time;
processes[i].turnaround_time = processes[i].finish_time - processes[i].arrive_time;
processes[i].weighted_turnaround_time = (float) processes[i].turnaround_time / processes[i].execute_time;
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrive_time,
processes[i].execute_time, processes[i].start_time, processes[i].finish_time,
processes[i].turnaround_time, processes[i].weighted_turnaround_time);
avg_turnaround_time += processes[i].turnaround_time;
avg_weighted_turnaround_time += processes[i].weighted_turnaround_time;
}
avg_turnaround_time /= n;
avg_weighted_turnaround_time /= n;
printf("平均周转时间:%.2f\n", avg_turnaround_time);
printf("平均带权周转时间:%.2f\n", avg_weighted_turnaround_time);
}
void hrrn(Process processes[], int n) {
int i, j;
float max_response_ratio, response_ratio;
Process temp;
float avg_turnaround_time = 0, avg_weighted_turnaround_time = 0;
printf("\n最高响应比优先调度算法:\n");
printf("进程\t到达时间\t执行时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");
for (i = 0; i < n; i++) {
max_response_ratio = 0;
for (j = i; j < n; j++) {
if (processes[j].arrive_time <= processes[i].finish_time) {
response_ratio = (float) (processes[i].finish_time - processes[j].arrive_time + processes[j].execute_time) / processes[j].execute_time;
if (response_ratio > max_response_ratio) {
max_response_ratio = response_ratio;
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
}
for (i = 0; i < n; i++) {
if (i == 0) {
processes[i].start_time = processes[i].arrive_time;
} else {
processes[i].start_time = processes[i - 1].finish_time;
}
processes[i].finish_time = processes[i].start_time + processes[i].execute_time;
processes[i].turnaround_time = processes[i].finish_time - processes[i].arrive_time;
processes[i].weighted_turnaround_time = (float) processes[i].turnaround_time / processes[i].execute_time;
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f\n", processes[i].name, processes[i].arrive_time,
processes[i].execute_time, processes[i].start_time, processes[i].finish_time,
processes[i].turnaround_time, processes[i].weighted_turnaround_time);
avg_turnaround_time += processes[i].turnaround_time;
avg_weighted_turnaround_time += processes[i].weighted_turnaround_time;
}
avg_turnaround_time /= n;
avg_weighted_turnaround_time /= n;
printf("平均周转时间:%.2f\n", avg_turnaround_time);
printf("平均带权周转时间:%.2f\n", avg_weighted_turnaround_time);
}
```
请注意,此程序仅供参考。您可以根据自己的需要进行修改和优化。
阅读全文