操作系统先到先服务算法可运行的c语言代码
时间: 2024-05-11 12:18:07 浏览: 43
操作系统先来先服务c算法
5星 · 资源好评率100%
抱歉,我并不能提供运行代码的服务。但是,下面是一个使用先到先服务算法的简单C语言程序示例,可以作为参考:
```c
#include<stdio.h>
struct process {
int pid;
int arrival_time;
int burst_time;
int turnaround_time;
int waiting_time;
int completion_time;
};
void fcfs(struct process arr[], int n) {
int i, j;
arr[0].completion_time = arr[0].burst_time;
arr[0].turnaround_time = arr[0].completion_time - arr[0].arrival_time;
arr[0].waiting_time = arr[0].turnaround_time - arr[0].burst_time;
for (i = 1; i < n; i++) {
arr[i].completion_time = arr[i - 1].completion_time + arr[i].burst_time;
arr[i].turnaround_time = arr[i].completion_time - arr[i].arrival_time;
arr[i].waiting_time = arr[i].turnaround_time - arr[i].burst_time;
}
}
int main() {
int n, i;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process arr[n];
printf("\nEnter the arrival time and burst time of each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d:\n", i + 1);
printf("Arrival time: ");
scanf("%d", &arr[i].arrival_time);
printf("Burst time: ");
scanf("%d", &arr[i].burst_time);
arr[i].pid = i + 1;
}
fcfs(arr, n);
printf("\nProcess\t Arrival Time\t Burst Time\t Completion Time\t Turnaround Time\t Waiting Time\n");
for (i = 0; i < n; i++) {
printf("%d\t %d\t\t %d\t\t %d\t\t\t %d\t\t\t %d\n", arr[i].pid, arr[i].arrival_time, arr[i].burst_time, arr[i].completion_time, arr[i].turnaround_time, arr[i].waiting_time);
}
return 0;
}
```
这个程序可以计算先到先服务算法下的进程的完成时间,周转时间和等待时间。
阅读全文