C语言实现短作业优先服务和先来先服务
时间: 2023-11-16 20:06:49 浏览: 43
短作业优先(Shortest Job First,简称SJF)和先来先服务(First Come First Serve,简称FCFS)是两种常见的进程调度算法。下面是C语言实现这两种算法的示例代码:
SJF算法实现:
```c
#include <stdio.h>
int main() {
int at[10], bt[10], ct[10], tat[10], wt[10];
int n, i, j, temp, total = 0;
float avg_tat, avg_wt;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the arrival time and burst time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d%d", &at[i], &bt[i]);
}
// sort processes by burst time
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (bt[i] > bt[j]) {
temp = bt[i];
bt[i] = bt[j];
bt[j] = temp;
temp = at[i];
at[i] = at[j];
at[j] = temp;
}
}
}
// calculate completion time, turnaround time and waiting time
for (i = 0; i < n; i++) {
if (i == 0) {
ct[i] = at[i] + bt[i];
} else {
ct[i] = ct[i - 1] + bt[i];
}
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
total += tat[i];
}
// calculate average turnaround time and waiting time
avg_tat = (float)total / n;
total = 0;
for (i = 0; i < n; i++) {
total += wt[i];
}
avg_wt = (float)total / n;
// print results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("Average turnaround time: %.2f\n", avg_tat);
printf("Average waiting time: %.2f\n", avg_wt);
return 0;
}
```
FCFS算法实现:
```c
#include <stdio.h>
int main() {
int at[10], bt[10], ct[10], tat[10], wt[10];
int n, i, j, total = 0;
float avg_tat, avg_wt;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter the arrival time and burst time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d%d", &at[i], &bt[i]);
}
// calculate completion time, turnaround time and waiting time
for (i = 0; i < n; i++) {
if (i == 0) {
ct[i] = at[i] + bt[i];
} else {
ct[i] = ct[i - 1] + bt[i];
}
tat[i] = ct[i] - at[i];
wt[i] = tat[i] - bt[i];
total += tat[i];
}
// calculate average turnaround time and waiting time
avg_tat = (float)total / n;
total = 0;
for (i = 0; i < n; i++) {
total += wt[i];
}
avg_wt = (float)total / n;
// print results
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for (i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, at[i], bt[i], ct[i], tat[i], wt[i]);
}
printf("Average turnaround time: %.2f\n", avg_tat);
printf("Average waiting time: %.2f\n", avg_wt);
return 0;
}
```
以上两份代码都是基于输入的到达时间和执行时间计算出完成时间、周转时间和等待时间,其中SJF算法在计算之前还对进程按照执行时间排序。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](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)