c语言:实现种作业调度算法,先来先服务(fcfs),短作业优先(sjf)
时间: 2023-12-25 10:01:50 浏览: 72
作业调度算法是操作系统中一种重要的策略,可以优化作业的执行顺序和提高系统的效率。在C语言中,实现先来先服务(FCFS)和短作业优先(SJF)这两种常用的作业调度算法并不复杂,可以通过编写相应的代码来实现。
首先,先来先服务算法是按照作业到达的顺序进行调度,即先提交的作业先执行。在C语言中,我们可以通过编写一个简单的循环来实现这一算法。我们可以创建一个队列来存储作业的信息,然后按照它们到达的顺序依次执行,直到所有作业都执行完毕。
其次,短作业优先算法是按照作业的执行时间长短进行调度,执行时间越短的作业越优先执行。在C语言中,我们可以通过比较作业的执行时间来实现这一算法。我们可以先将所有作业按执行时间排序,然后按照顺序执行作业,直到所有作业执行完毕。
总的来说,在C语言中实现这两种作业调度算法并不困难,只需要利用循环、数组、排序等基本的语言特性即可实现。通过编写相应的代码,可以更好地理解这两种算法的原理和实现方法,对于学习操作系统和算法都是非常有帮助的。
相关问题
用C语言编码实现先来先服务调度算法和短作业优先调度算法。
先来先服务调度算法(FCFS)是一种最简单的调度算法,也是最容易实现的一种算法。该算法按照作业提交的先后顺序进行调度,即先提交的作业先执行,后提交的作业后执行。该算法的优点是简单易懂,缺点是平均等待时间较长,不适用于长作业。
短作业优先调度算法(SJF)是一种根据作业长度来进行调度的算法。该算法优先调度作业长度较短的作业,以期望达到最优的平均等待时间。该算法的优点是平均等待时间较短,缺点是无法预测作业的长度,可能会导致长作业等待时间过长。
以下是用C语言编码实现先来先服务调度算法和短作业优先调度算法的示例代码:
```c
// 先来先服务调度算法
#include <stdio.h>
int main() {
int n, i, j, wt[20], bt[20], tat[20], avwt = 0, avtat = 0;
printf("Enter total number of processes(maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]: ", i + 1);
scanf("%d", &bt[i]);
}
wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround 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;
}
// 短作业优先调度算法
#include <stdio.h>
int main() {
int n, i, j, pos, temp, wt[20], bt[20], tat[20], avwt = 0, avtat = 0;
printf("Enter total number of processes(maximum 20): ");
scanf("%d", &n);
printf("\nEnter Process Burst Time\n");
for(i = 0; i < n; i++) {
printf("P[%d]: ", i + 1);
scanf("%d", &bt[i]);
}
for(i = 0; i < n; i++) {
pos = i;
for(j = i + 1; j < n; j++) {
if(bt[j] < bt[pos])
pos = j;
}
temp = bt[i];
bt[i] = bt[pos];
bt[pos] = temp;
}
wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = 0;
for(j = 0; j < i; j++)
wt[i] += bt[j];
}
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround 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;
}
```
C语言实现短作业优先服务和先来先服务
短作业优先(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算法在计算之前还对进程按照执行时间排序。