ubuntu短作业优先调度算法c
时间: 2023-10-22 13:09:50 浏览: 85
下面是一个简单的 Ubuntu 短作业优先调度算法的 C 代码示例:
```c
#include <stdio.h>
struct Process {
int pid; // 进程 ID
int arrival_time; // 进程到达时间
int burst_time; // 进程执行时间
int waiting_time; // 进程等待时间
int turnaround_time;// 进程周转时间
int completion_time;// 进程完成时间
};
void sjf(struct Process processes[], int n) {
int i, j, time = 0;
float avg_waiting_time = 0, avg_turnaround_time = 0;
struct Process temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (processes[i].burst_time > processes[j].burst_time) {
temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
for (i = 0; i < n; i++) {
processes[i].completion_time = time + processes[i].burst_time;
processes[i].turnaround_time = processes[i].completion_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
avg_waiting_time += processes[i].waiting_time;
avg_turnaround_time += processes[i].turnaround_time;
time = processes[i].completion_time;
}
avg_waiting_time /= n;
avg_turnaround_time /= n;
printf("PID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", processes[i].pid, processes[i].arrival_time,
processes[i].burst_time, processes[i].completion_time, processes[i].turnaround_time,
processes[i].waiting_time);
}
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
}
int main() {
int n, i;
struct Process processes[10];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter process details:\n");
for (i = 0; i < n; i++) {
printf("Process %d:\n", i + 1);
printf("\tPID: ");
scanf("%d", &processes[i].pid);
printf("\tArrival Time: ");
scanf("%d", &processes[i].arrival_time);
printf("\tBurst Time: ");
scanf("%d", &processes[i].burst_time);
}
sjf(processes, n);
return 0;
}
```
这个代码实现了一个简单的 Ubuntu 短作业优先调度算法。在这个算法中,进程按照它们的执行时间进行排序,并且短的进程先执行。当一个进程执行完毕后,就会执行下一个最短的进程,直到所有的进程都执行完毕。
阅读全文