用c语言写先来先服务算法
时间: 2024-02-20 21:00:02 浏览: 23
好的,以下是用 C 语言实现先来先服务(FCFS)调度算法的示例代码:
```c
#include <stdio.h>
typedef struct {
int pid; // 进程 ID
int arrivalTime; // 到达时间
int burstTime; // 执行时间
int waitingTime; // 等待时间
int turnaroundTime; // 周转时间
} Process;
void calculateWaitingTime(Process processes[], int n) {
int currentTime = 0; // 当前时间
for (int i = 0; i < n; i++) {
if (currentTime < processes[i].arrivalTime) {
currentTime = processes[i].arrivalTime;
}
processes[i].waitingTime = currentTime - processes[i].arrivalTime;
currentTime += processes[i].burstTime;
}
}
void calculateTurnaroundTime(Process processes[], int n) {
for (int i = 0; i < n; i++) {
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
}
}
void calculateAverageTime(Process processes[], int n, float *avgWaitingTime, float *avgTurnaroundTime) {
int totalWaitingTime = 0, totalTurnaroundTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += processes[i].waitingTime;
totalTurnaroundTime += processes[i].turnaroundTime;
}
*avgWaitingTime = (float) totalWaitingTime / n;
*avgTurnaroundTime = (float) totalTurnaroundTime / n;
}
void printResult(Process processes[], int n, float avgWaitingTime, float avgTurnaroundTime) {
printf("Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrivalTime, processes[i].burstTime, processes[i].waitingTime, processes[i].turnaroundTime);
}
printf("Average Waiting Time: %.2f\n", avgWaitingTime);
printf("Average Turnaround Time: %.2f\n", avgTurnaroundTime);
}
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
Process processes[n];
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrivalTime, &processes[i].burstTime);
processes[i].pid = i + 1;
}
calculateWaitingTime(processes, n);
calculateTurnaroundTime(processes, n);
float avgWaitingTime, avgTurnaroundTime;
calculateAverageTime(processes, n, &avgWaitingTime, &avgTurnaroundTime);
printResult(processes, n, avgWaitingTime, avgTurnaroundTime);
return 0;
}
```
使用该算法,我们可以计算出每个进程的等待时间、周转时间以及平均等待时间和平均周转时间。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)