操作系统先到先服务算法c语言代码
时间: 2023-10-27 14:17:10 浏览: 83
操作系统进程调度C语言代码
下面是一个简单的 C 语言代码实现先到先服务算法(FCFS):
```c
#include <stdio.h>
int main() {
int n, bt[20], wt[20], tat[20], avwt = 0, avtat = 0, i, j;
printf("Enter total number of processes: ");
scanf("%d", &n);
printf("Enter process burst time:\n");
for (i = 0; i < n; i++) {
printf("P[%d]: ", i + 1);
scanf("%d", &bt[i]);
}
wt[0] = 0; // Waiting time for first process is 0
// Calculate waiting time for each process
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++) {
wt[i] += bt[j];
}
}
printf("\nProcess\t\tBurst time\tWaiting time\tTurnaround time");
// Calculate turnaround time and average waiting time and turnaround time
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avwt += wt[i];
avtat += tat[i];
printf("\nP[%d]\t\t%d\t\t%d\t\t%d", i + 1, bt[i], wt[i], tat[i]);
}
avwt /= i;
avtat /= i;
printf("\n\nAverage waiting time: %d", avwt);
printf("\nAverage turnaround time: %d", avtat);
return 0;
}
```
该代码首先要求用户输入进程数量和每个进程的 CPU 周转时间(burst time),然后计算每个进程的等待时间(waiting time)。最后,它输出每个进程的周转时间(turnaround time)、等待时间、和平均等待时间和周转时间。
阅读全文