fcfs调度算法代码c语言
时间: 2023-08-12 16:12:54 浏览: 120
下面是一个简单的 FCFS 调度算法的代码示例,用 C 语言编写:
```c
#include <stdio.h>
void main()
{
int n, i, j;
float wait_time = 0, turnaround_time = 0, avg_wait_time, avg_turnaround_time;
printf("Enter total number of processes:");
scanf("%d", &n);
int burst_time[n], arrival_time[n], start_time[n], completion_time[n];
for(i = 0; i < n; i++)
{
printf("\nEnter Arrival Time and Burst Time for Process %d:", i+1);
scanf("%d%d", &arrival_time[i], &burst_time[i]);
}
start_time[0] = arrival_time[0];
for(i = 1; i < n; i++)
{
start_time[i] = start_time[i-1] + burst_time[i-1];
}
for(i = 0; i < n; i++)
{
completion_time[i] = start_time[i] + burst_time[i];
turnaround_time = turnaround_time + (completion_time[i] - arrival_time[i]);
wait_time = wait_time + (start_time[i] - arrival_time[i]);
}
avg_wait_time = wait_time / n;
avg_turnaround_time = turnaround_time / n;
printf("\nProcess\t Arrival Time\t Burst Time\t Start Time\t Completion Time\t Turnaround Time\t Waiting Time");
for(i = 0; i < n; i++)
{
printf("\n%d\t\t %d\t\t %d\t\t %d\t\t %d\t\t\t %d\t\t\t %d", i+1, arrival_time[i], burst_time[i], start_time[i], completion_time[i], completion_time[i] - arrival_time[i], start_time[i] - arrival_time[i]);
}
printf("\nAverage Waiting Time: %f", avg_wait_time);
printf("\nAverage Turnaround Time: %f\n", avg_turnaround_time);
}
```
这个程序首先要求输入进程的数量和每个进程的到达时间和运行时间。然后计算出每个进程的开始时间、完成时间、周转时间和等待时间,并计算平均等待时间和平均周转时间。最后打印出所有进程的详细信息和平均值。
阅读全文