C语言实现FCFS 、SJF 、RR 、PSA调度算法
时间: 2023-07-10 12:27:49 浏览: 190
以下是C语言实现FCFS、SJF、RR、PSA调度算法的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 10
typedef struct {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;
void fcfs(Process processes[], int n) {
int current_time = 0;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time > current_time) {
current_time = processes[i].arrival_time;
}
processes[i].waiting_time = current_time - processes[i].arrival_time;
processes[i].turnaround_time = processes[i].waiting_time + processes[i].burst_time;
current_time += processes[i].burst_time;
}
}
void sjf(Process processes[], int n) {
int current_time = 0;
int remaining_processes = n;
while (remaining_processes > 0) {
int next_process_index = -1;
int shortest_burst_time = 999999;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time < shortest_burst_time) {
next_process_index = i;
shortest_burst_time = processes[i].burst_time;
}
}
if (next_process_index == -1) {
current_time++;
} else {
Process next_process = processes[next_process_index];
processes[next_process_index] = processes[remaining_processes-1];
processes[remaining_processes-1] = next_process;
remaining_processes--;
next_process.waiting_time = current_time - next_process.arrival_time;
next_process.turnaround_time = next_process.waiting_time + next_process.burst_time;
current_time += next_process.burst_time;
}
}
}
void rr(Process processes[], int n, int quantum) {
int current_time = 0;
int remaining_processes = n;
while (remaining_processes > 0) {
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time > 0) {
int time_slice = processes[i].burst_time < quantum ? processes[i].burst_time : quantum;
processes[i].burst_time -= time_slice;
current_time += time_slice;
if (processes[i].burst_time == 0) {
remaining_processes--;
processes[i].waiting_time = current_time - processes[i].arrival_time - processes[i].turnaround_time;
}
}
}
}
}
void psa(Process processes[], int n) {
int current_time = 0;
int remaining_processes = n;
while (remaining_processes > 0) {
int highest_priority_index = -1;
int highest_priority = -1;
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && processes[i].burst_time > 0) {
int priority = 1 + processes[i].waiting_time / processes[i].burst_time;
if (priority > highest_priority) {
highest_priority_index = i;
highest_priority = priority;
}
}
}
if (highest_priority_index == -1) {
current_time++;
} else {
Process next_process = processes[highest_priority_index];
processes[highest_priority_index] = processes[remaining_processes-1];
processes[remaining_processes-1] = next_process;
remaining_processes--;
next_process.waiting_time = current_time - next_process.arrival_time;
next_process.turnaround_time = next_process.waiting_time + next_process.burst_time;
current_time += next_process.burst_time;
}
}
}
int main() {
Process processes[MAX_PROCESSES];
int n;
int quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);
processes[i].pid = i+1;
}
printf("\nFCFS:\n");
fcfs(processes, n);
for (int i = 0; i < n; i++) {
printf("Process %d: arrival_time=%d, burst_time=%d, waiting_time=%d, turnaround_time=%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("\nSJF:\n");
sjf(processes, n);
for (int i = 0; i < n; i++) {
printf("Process %d: arrival_time=%d, burst_time=%d, waiting_time=%d, turnaround_time=%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("\nEnter the time quantum for RR: ");
scanf("%d", &quantum);
printf("\nRR:\n");
rr(processes, n, quantum);
for (int i = 0; i < n; i++) {
printf("Process %d: arrival_time=%d, burst_time=%d, waiting_time=%d, turnaround_time=%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
printf("\nPSA:\n");
psa(processes, n);
for (int i = 0; i < n; i++) {
printf("Process %d: arrival_time=%d, burst_time=%d, waiting_time=%d, turnaround_time=%d\n", processes[i].pid, processes[i].arrival_time, processes[i].burst_time, processes[i].waiting_time, processes[i].turnaround_time);
}
return 0;
}
```
在上面的代码中,有一个 `Process` 结构体来表示一个进程,包含 PID、到达时间、执行时间、等待时间和周转时间等属性。然后有四个函数 `fcfs`、`sjf`、`rr` 和 `psa` 分别实现先来先服务、最短作业优先、时间片轮转和优先级调度算法。最后在 `main` 函数中通过用户输入创建进程列表,依次使用四种算法对进程进行调度,并输出每个进程的信息。
阅读全文