先来先服务算法c语言
时间: 2023-11-18 13:54:36 浏览: 48
先来先服务算法(FCFS)是一种简单的作业调度算法,按照作业提交的先后次序进行调度,总是首先调度最先到达的作业。在C语言中,可以通过结构体来表示作业控制块(JCB),结构体中包含作业名、提交时间、所需的运行时间等信息。具体实现可以参考以下代码:
```
#include <stdio.h>
struct JCB {
char name[20]; // 作业名
int arrive_time; // 提交时间
int run_time; // 运行时间
};
int main() {
int n; // 作业数
printf("请输入作业数:");
scanf("%d", &n);
struct JCB job[n];
int start_time[n], finish_time[n], turnaround_time[n], weighted_turnaround_time[n];
int total_turnaround_time = 0, total_weighted_turnaround_time = 0;
for (int i = 0; i < n; i++) {
printf("请输入第%d个作业的作业名、提交时间、运行时间:", i + 1);
scanf("%s%d%d", job[i].name, &job[i].arrive_time, &job[i].run_time);
}
start_time[0] = job[0].arrive_time;
finish_time[0] = start_time[0] + job[0].run_time;
turnaround_time[0] = finish_time[0] - job[0].arrive_time;
weighted_turnaround_time[0] = turnaround_time[0] / job[0].run_time;
total_turnaround_time += turnaround_time[0];
total_weighted_turnaround_time += weighted_turnaround_time[0];
for (int i = 1; i < n; i++) {
start_time[i] = finish_time[i - 1];
finish_time[i] = start_time[i] + job[i].run_time;
turnaround_time[i] = finish_time[i] - job[i].arrive_time;
weighted_turnaround_time[i] = turnaround_time[i] / job[i].run_time;
total_turnaround_time += turnaround_time[i];
total_weighted_turnaround_time += weighted_turnaround_time[i];
}
printf("作业名\t提交时间\t运行时间\t开始时间\t完成时间\t周转时间\t带权周转时间\n");
for (int i = 0; i < n; i++) {
printf("%s\t%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", job[i].name, job[i].arrive_time, job[i].run_time, start_time[i], finish_time[i], turnaround_time[i], weighted_turnaround_time[i]);
}
printf("平均周转时间:%f\n", (float)total_turnaround_time / n);
printf("平均带权周转时间:%f\n", (float)total_weighted_turnaround_time / n);
return 0;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)