短作业优先调度算法c语言实现
时间: 2023-11-17 19:06:23 浏览: 91
好的,短作业优先调度算法的C语言实现可以参考以下代码:
```
#include <stdio.h>
int main() {
int n, burst_time[50], waiting_time[50], turnaround_time[50], i, j, temp;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter total number of processes:");
scanf("%d", &n);
printf("\nEnter Process Burst Time:\n");
for (i = 0; i < n; i++) {
printf("P[%d]:", i + 1);
scanf("%d", &burst_time[i]);
}
// Sorting burst time in ascending order using selection sort
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (burst_time[i] > burst_time[j]) {
temp = burst_time[i];
burst_time[i] = burst_time[j];
burst_time[j] = temp;
}
}
}
waiting_time[0] = 0; // Waiting time for first process is 0
// Calculating waiting time
for (i = 1; i < n; i++) {
waiting_time[i] = 0;
for (j = 0; j < i; j++) {
waiting_time[i] += burst_time[j];
}
}
// Calculating turnaround time
for (i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
avg_waiting_time += waiting_time[i];
avg_turnaround_time += turnaround_time[i];
}
// Printing processes along with their burst time, waiting time and turnaround time
printf("\nProcesses\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("P[%d]\t\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], waiting_time[i], turnaround_time[i]);
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("\nAverage Waiting Time:%.2f", avg_waiting_time);
printf("\nAverage Turnaround Time:%.2f", avg_turnaround_time);
return 0;
}
```
以上是短作业优先调度算法的C语言实现,其核心思想是选择执行时间最短的作业优先执行,以保证作业的最短响应时间和最小平均等待时间。
阅读全文