先来先服务和短作业优先调度算法代码
时间: 2023-10-22 07:12:18 浏览: 87
先来先服务(FCFS)调度算法代码:
```c
#include <stdio.h>
// 进程结构体
struct Process {
int pid; // 进程编号
int arrival_time; // 到达时间
int burst_time; // 执行时间
int completion_time; // 完成时间
int turnaround_time; // 周转时间
int waiting_time; // 等待时间
};
// 计算进程的周转时间和等待时间
void calculateTimes(struct Process *processes, int n) {
int current_time = 0;
for (int i = 0; i < n; i++) {
// 更新当前时间
current_time += processes[i].burst_time;
// 更新进程的完成时间
processes[i].completion_time = current_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;
}
}
// 打印进程的信息
void printProcesses(struct Process *processes, int n) {
printf("PID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting 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", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].completion_time, processes[i].turnaround_time, processes[i].waiting_time);
}
}
int main() {
int n; // 进程数
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
// 输入每个进程的信息
for (int i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i + 1;
}
// 按到达时间进行排序
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (processes[j].arrival_time > processes[j + 1].arrival_time) {
struct Process temp = processes[j];
processes[j] = processes[j + 1];
processes[j + 1] = temp;
}
}
}
// 计算进程的周转时间和等待时间
calculateTimes(processes, n);
// 打印进程的信息
printProcesses(processes, n);
return 0;
}
```
短作业优先(SJF)调度算法代码:
```c
#include <stdio.h>
// 进程结构体
struct Process {
int pid; // 进程编号
int arrival_time; // 到达时间
int burst_time; // 执行时间
int completion_time; // 完成时间
int turnaround_time; // 周转时间
int waiting_time; // 等待时间
};
// 计算进程的周转时间和等待时间
void calculateTimes(struct Process *processes, int n) {
int current_time = 0;
int completed_processes = 0;
while (completed_processes < n) {
int shortest_job_index = -1;
int shortest_job_burst_time = 1000000;
// 找到到达时间在当前时间之前且剩余执行时间最短的进程
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].completion_time == -1 && processes[i].burst_time < shortest_job_burst_time) {
shortest_job_index = i;
shortest_job_burst_time = processes[i].burst_time;
}
}
// 如果找到了进程,执行该进程
if (shortest_job_index != -1) {
current_time += processes[shortest_job_index].burst_time;
processes[shortest_job_index].completion_time = current_time;
processes[shortest_job_index].turnaround_time = processes[shortest_job_index].completion_time - processes[shortest_job_index].arrival_time;
processes[shortest_job_index].waiting_time = processes[shortest_job_index].turnaround_time - processes[shortest_job_index].burst_time;
completed_processes++;
}
// 如果没有找到进程,时间加一
else {
current_time++;
}
}
}
// 打印进程的信息
void printProcesses(struct Process *processes, int n) {
printf("PID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting 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", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].completion_time, processes[i].turnaround_time, processes[i].waiting_time);
}
}
int main() {
int n; // 进程数
printf("Enter the number of processes: ");
scanf("%d", &n);
struct Process processes[n];
// 输入每个进程的信息
for (int i = 0; i < n; i++) {
printf("Enter the arrival time and burst time of process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i + 1;
processes[i].completion_time = -1; // 初始化为-1,表示未完成
}
// 计算进程的周转时间和等待时间
calculateTimes(processes, n);
// 打印进程的信息
printProcesses(processes, n);
return 0;
}
```
阅读全文