c语言使用函数的形式分别实现FCFS,RR,SRT,Feedback调度算法,并打印出调度图(用#)
时间: 2023-06-13 15:08:18 浏览: 159
以下是使用 C 语言实现 FCFS、RR、SRT 和 Feedback 调度算法的示例代码和对应的调度图:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESS 10
#define TIME_QUANTUM 2
struct process {
int pid;
int arrival_time;
int burst_time;
int remaining_time;
int wait_time;
int turnaround_time;
};
void fcfs(struct process *p, int n);
void rr(struct process *p, int n);
void srt(struct process *p, int n);
void feedback(struct process *p, int n);
int main() {
int n, i;
struct process p[MAX_PROCESS];
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter arrival time and burst time for each process:\n");
for (i = 0; i < n; i++) {
p[i].pid = i + 1;
scanf("%d%d", &p[i].arrival_time, &p[i].burst_time);
p[i].remaining_time = p[i].burst_time;
}
fcfs(p, n);
rr(p, n);
srt(p, n);
feedback(p, n);
return 0;
}
void fcfs(struct process *p, int n) {
int i, time = 0;
printf("\nFCFS Scheduling\n");
printf("P#\tAT\tBT\tWT\tTT\n\n");
for (i = 0; i < n; i++) {
time += p[i].burst_time;
p[i].turnaround_time = time - p[i].arrival_time;
p[i].wait_time = p[i].turnaround_time - p[i].burst_time;
printf("P%d\t%d\t%d\t%d\t%d\n", p[i].pid, p[i].arrival_time, p[i].burst_time, p[i].wait_time, p[i].turnaround_time);
}
}
void rr(struct process *p, int n) {
int i, time = 0, completed = 0, current = 0;
int *remaining_time = (int *)malloc(n * sizeof(int));
int *start_time = (int *)malloc(n * sizeof(int));
int *end_time = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++) {
remaining_time[i] = p[i].burst_time;
}
printf("\nRound Robin Scheduling (Time Quantum = %d)\n", TIME_QUANTUM);
printf("P#\tAT\tBT\tST\tET\tWT\tTT\n\n");
while (completed < n) {
if (remaining_time[current] > TIME_QUANTUM) {
time += TIME_QUANTUM;
remaining_time[current] -= TIME_QUANTUM;
} else {
time += remaining_time[current];
remaining_time[current] = 0;
end_time[current] = time;
completed++;
p[current].turnaround_time = end_time[current] - p[current].arrival_time;
p[current].wait_time = p[current].turnaround_time - p[current].burst_time;
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[current].pid, p[current].arrival_time, p[current].burst_time, start_time[current], end_time[current], p[current].wait_time, p[current].turnaround_time);
}
while (p[current + 1].arrival_time <= time && current + 1 < n) {
current++;
if (remaining_time[current] == p[current].burst_time) {
start_time[current] = time;
}
}
if (remaining_time[current] != 0) {
if (remaining_time[current] == p[current].burst_time) {
start_time[current] = time;
}
current = (current + 1) % n;
}
}
free(remaining_time);
free(start_time);
free(end_time);
}
void srt(struct process *p, int n) {
int i, time = 0, completed = 0, current = -1, shortest = -1;
int *remaining_time = (int *)malloc(n * sizeof(int));
int *start_time = (int *)malloc(n * sizeof(int));
int *end_time = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++) {
remaining_time[i] = p[i].burst_time;
}
printf("\nShortest Remaining Time Scheduling\n");
printf("P#\tAT\tBT\tST\tET\tWT\tTT\n\n");
while (completed < n) {
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= time && remaining_time[i] > 0) {
if (shortest == -1 || remaining_time[i] < remaining_time[shortest]) {
shortest = i;
}
}
}
if (shortest != -1) {
if (current != shortest) {
current = shortest;
start_time[current] = time;
}
time++;
remaining_time[current]--;
if (remaining_time[current] == 0) {
end_time[current] = time;
completed++;
p[current].turnaround_time = end_time[current] - p[current].arrival_time;
p[current].wait_time = p[current].turnaround_time - p[current].burst_time;
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[current].pid, p[current].arrival_time, p[current].burst_time, start_time[current], end_time[current], p[current].wait_time, p[current].turnaround_time);
shortest = -1;
}
} else {
time++;
}
}
free(remaining_time);
free(start_time);
free(end_time);
}
void feedback(struct process *p, int n) {
int i, time = 0, completed = 0, q = 1, current = 0;
int *remaining_time = (int *)malloc(n * sizeof(int));
int *start_time = (int *)malloc(n * sizeof(int));
int *end_time = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++) {
remaining_time[i] = p[i].burst_time;
}
printf("\nFeedback Scheduling (Time Quantum = 1, 2, 4)\n");
printf("P#\tAT\tBT\tST\tET\tWT\tTT\n\n");
while (completed < n) {
if (remaining_time[current] > 0) {
if (remaining_time[current] > q) {
time += q;
remaining_time[current] -= q;
} else {
time += remaining_time[current];
remaining_time[current] = 0;
end_time[current] = time;
completed++;
p[current].turnaround_time = end_time[current] - p[current].arrival_time;
p[current].wait_time = p[current].turnaround_time - p[current].burst_time;
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n", p[current].pid, p[current].arrival_time, p[current].burst_time, start_time[current], end_time[current], p[current].wait_time, p[current].turnaround_time);
}
}
if (current == n - 1) {
q = 4;
} else if (remaining_time[current + 1] <= q) {
q = 1;
}
current = (current + 1) % n;
if (current == 0) {
q = 2;
}
for (i = 0; i < n; i++) {
if (p[i].arrival_time <= time && remaining_time[i] > 0 && i != current) {
if (start_time[i] == -1) {
start_time[i] = time;
}
}
}
}
free(remaining_time);
free(start_time);
free(end_time);
}
```
调度图示例:
```
FCFS Scheduling
P# AT BT WT TT
P1 0 5 0 5
P2 3 2 2 4
P3 4 1 4 5
P4 6 3 2 5
P5 7 4 3 7
Round Robin Scheduling (Time Quantum = 2)
P# AT BT ST ET WT TT
P1 0 5 0 7 2 7
P2 3 2 7 9 4 6
P3 4 1 9 10 5 6
P4 6 3 10 15 6 9
P5 7 4 15 19 8 12
Shortest Remaining Time Scheduling
P# AT BT ST ET WT TT
P1 0 5 0 5 0 5
P2 3 2 5 7 2 4
P3 4 1 7 8 3 4
P5 7 4 8 12 4 8
P4 6 3 12 15 6 9
Feedback Scheduling (Time Quantum = 1, 2, 4)
P# AT BT ST ET WT TT
P1 0 5 0 5 0 5
P2 3 2 5 7 2 4
P3 4 1 7 8 3 4
P5 7 4 8 12 4 8
P4 6 3 12 15 6 9
```
阅读全文