C语言实现先来先服务算法代码
时间: 2023-08-20 20:14:18 浏览: 50
以下是一个简单的C语言实现先来先服务算法的代码,假设有一个结构体Process表示进程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_PROCESS_NUM 100
typedef struct Process {
int id; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time; // 周转时间
} Process;
int main() {
int n; // 进程数
Process processes[MAX_PROCESS_NUM];
printf("请输入进程数:");
scanf("%d", &n);
printf("请按进程到达时间长度从小到大的顺序依次输入每个进程的到达时间和执行时间:\n");
for (int i = 0; i < n; i++) {
processes[i].id = i + 1;
scanf("%d%d", &processes[i].arrival_time, &processes[i].burst_time);
}
// 先来先服务算法
int current_time = 0; // 当前时间
for (int i = 0; i < n; i++) {
current_time += processes[i].burst_time;
processes[i].turnaround_time = current_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
}
// 输出结果
printf("进程ID\t到达时间\t执行时间\t等待时间\t周转时间\n");
for (int i = 0; i < n; i++) {
printf("%d\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].waiting_time, processes[i].turnaround_time);
}
return 0;
}
```
该代码中,先输入进程数和每个进程的到达时间和执行时间,然后按照进程的到达时间长度从小到大的顺序运行先来先服务算法,并输出每个进程的等待时间和周转时间。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)