用高级语言(C语言)编写和调试多个作业调度执行的模拟程序,分别模拟先来先服务(CFS),最短作业优先(SJF)的调度算法。给出代码
时间: 2024-01-22 10:04:00 浏览: 71
调度算法-先来先服务(FCFS)和短作业优先(SJF)
4星 · 用户满意度95%
好的,以下是基于C语言的作业调度执行模拟程序,分别模拟先来先服务(CFS)和最短作业优先(SJF)的调度算法:
先来先服务(CFS)调度算法:
```
#include<stdio.h>
#include<stdlib.h>
struct process {
int pid; // 进程编号
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time;// 周转时间
int completion_time;// 完成时间
};
int main() {
int n;
float avg_wait_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process p[n];
printf("\nEnter the arrival time and burst time of each process:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", i+1);
p[i].pid = i+1;
printf("Arrival time: ");
scanf("%d", &p[i].arrival_time);
printf("Burst time: ");
scanf("%d", &p[i].burst_time);
}
// 对进程按到达时间进行排序
for(int i = 0; i < n-1; i++) {
for(int j = i+1; j < n; j++) {
if(p[i].arrival_time > p[j].arrival_time) {
struct process temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
// 计算每个进程的等待时间和完成时间
for(int i = 0; i < n; i++) {
if(i == 0) {
p[i].completion_time = p[i].burst_time + p[i].arrival_time;
} else {
if(p[i].arrival_time <= p[i-1].completion_time) {
p[i].completion_time = p[i-1].completion_time + p[i].burst_time;
} else {
p[i].completion_time = p[i].arrival_time + p[i].burst_time;
}
}
p[i].turnaround_time = p[i].completion_time - p[i].arrival_time;
p[i].waiting_time = p[i].turnaround_time - p[i].burst_time;
avg_wait_time += p[i].waiting_time;
avg_turnaround_time += p[i].turnaround_time;
}
avg_wait_time /= n;
avg_turnaround_time /= n;
printf("\nPID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");
for(int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time, p[i].completion_time);
}
printf("\nAverage waiting time: %f", avg_wait_time);
printf("\nAverage turnaround time: %f", avg_turnaround_time);
return 0;
}
```
最短作业优先(SJF)调度算法:
```
#include<stdio.h>
#include<stdlib.h>
struct process {
int pid; // 进程编号
int arrival_time; // 到达时间
int burst_time; // 执行时间
int waiting_time; // 等待时间
int turnaround_time;// 周转时间
int completion_time;// 完成时间
int is_completed; // 进程是否完成
};
int main() {
int n, t = 0, completed = 0;
float avg_wait_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);
struct process p[n];
printf("\nEnter the arrival time and burst time of each process:\n");
for(int i = 0; i < n; i++) {
printf("\nProcess %d:\n", i+1);
p[i].pid = i+1;
printf("Arrival time: ");
scanf("%d", &p[i].arrival_time);
printf("Burst time: ");
scanf("%d", &p[i].burst_time);
p[i].is_completed = 0;
}
// 计算每个进程的等待时间和完成时间
while(completed != n) {
int min_burst_time = 100000, min_burst_time_pid;
for(int i = 0; i < n; i++) {
if(p[i].arrival_time <= t && p[i].is_completed == 0) {
if(p[i].burst_time < min_burst_time) {
min_burst_time = p[i].burst_time;
min_burst_time_pid = i;
}
}
}
t += p[min_burst_time_pid].burst_time;
p[min_burst_time_pid].completion_time = t;
p[min_burst_time_pid].turnaround_time = p[min_burst_time_pid].completion_time - p[min_burst_time_pid].arrival_time;
p[min_burst_time_pid].waiting_time = p[min_burst_time_pid].turnaround_time - p[min_burst_time_pid].burst_time;
avg_wait_time += p[min_burst_time_pid].waiting_time;
avg_turnaround_time += p[min_burst_time_pid].turnaround_time;
p[min_burst_time_pid].is_completed = 1;
completed++;
}
avg_wait_time /= n;
avg_turnaround_time /= n;
printf("\nPID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\tCompletion Time\n");
for(int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time, p[i].completion_time);
}
printf("\nAverage waiting time: %f", avg_wait_time);
printf("\nAverage turnaround time: %f", avg_turnaround_time);
return 0;
}
```
阅读全文