c语言使用schedule工程函数的形式分别实现给定样例的FCFS,RR,SRT,Feedback调度算法
时间: 2023-06-14 07:04:48 浏览: 146
C++实现CPU调度算法先来先服务(FCFS),非抢占最短作业优先调度(SJF),优先级调度,时间片轮转调度(RR)并输出相应的平均等待时间和平均周转时间。
5星 · 资源好评率100%
以下是基于schedule工程函数实现的四种调度算法的代码示例:
1. FCFS调度算法
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "schedule.h"
/* 队列结构体 */
typedef struct {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
} process;
/* 进程队列 */
process queue[10];
int front = -1, rear = -1;
/* 将进程按照到达时间排序 */
void sort_by_arrival_time(process *q, int n) {
int i, j;
process temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (q[i].arrival_time > q[j].arrival_time) {
temp = q[i];
q[i] = q[j];
q[j] = temp;
}
}
}
}
/* 将进程加入队列 */
void enqueue(process p) {
if (rear == 10-1) {
printf("Queue overflow!\n");
exit(1);
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = p;
}
/* 从队列中取出一个进程 */
process dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow!\n");
exit(1);
}
process p = queue[front];
front++;
return p;
}
/* FCFS调度算法 */
void fcfs(process *q, int n) {
sort_by_arrival_time(q, n);
int i, time = 0;
for (i = 0; i < n; i++) {
while (time < q[i].arrival_time) {
schedule_idle(q[i].arrival_time - time);
time = q[i].arrival_time;
}
schedule_cpu(q[i].pid, q[i].burst_time);
time += q[i].burst_time;
}
}
int main() {
int i, n;
process p;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d%d", &p.arrival_time, &p.burst_time);
p.pid = i+1;
enqueue(p);
}
fcfs(queue, n);
return 0;
}
```
2. RR调度算法
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "schedule.h"
/* 队列结构体 */
typedef struct {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
} process;
/* 进程队列 */
process queue[10];
int front = -1, rear = -1;
/* 将进程按照到达时间排序 */
void sort_by_arrival_time(process *q, int n) {
int i, j;
process temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (q[i].arrival_time > q[j].arrival_time) {
temp = q[i];
q[i] = q[j];
q[j] = temp;
}
}
}
}
/* 将进程加入队列 */
void enqueue(process p) {
if (rear == 10-1) {
printf("Queue overflow!\n");
exit(1);
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = p;
}
/* 从队列中取出一个进程 */
process dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow!\n");
exit(1);
}
process p = queue[front];
front++;
return p;
}
/* RR调度算法 */
void rr(process *q, int n, int quantum) {
sort_by_arrival_time(q, n);
int i, time = q[0].arrival_time;
for (i = 0; i < n; i++) {
q[i].remaining_time = q[i].burst_time;
}
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (q[i].remaining_time > 0) {
done = 0;
if (q[i].remaining_time > quantum) {
schedule_cpu(q[i].pid, quantum);
q[i].remaining_time -= quantum;
time += quantum;
}
else {
schedule_cpu(q[i].pid, q[i].remaining_time);
time += q[i].remaining_time;
q[i].remaining_time = 0;
}
while (i < n-1 && time >= q[i+1].arrival_time) {
i++;
}
}
}
if (done == 1) {
break;
}
}
}
int main() {
int i, n, quantum;
process p;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d%d", &p.arrival_time, &p.burst_time);
p.pid = i+1;
enqueue(p);
}
printf("Enter time quantum: ");
scanf("%d", &quantum);
rr(queue, n, quantum);
return 0;
}
```
3. SRT调度算法
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "schedule.h"
/* 队列结构体 */
typedef struct {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
} process;
/* 进程队列 */
process queue[10];
int front = -1, rear = -1;
/* 将进程按照到达时间排序 */
void sort_by_arrival_time(process *q, int n) {
int i, j;
process temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (q[i].arrival_time > q[j].arrival_time) {
temp = q[i];
q[i] = q[j];
q[j] = temp;
}
}
}
}
/* 将进程加入队列 */
void enqueue(process p) {
if (rear == 10-1) {
printf("Queue overflow!\n");
exit(1);
}
if (front == -1) {
front = 0;
}
rear++;
queue[rear] = p;
}
/* 从队列中取出一个进程 */
process dequeue() {
if (front == -1 || front > rear) {
printf("Queue underflow!\n");
exit(1);
}
process p = queue[front];
front++;
return p;
}
/* SRT调度算法 */
void srt(process *q, int n) {
sort_by_arrival_time(q, n);
int i, time = q[0].arrival_time;
for (i = 0; i < n; i++) {
q[i].remaining_time = q[i].burst_time;
}
while (1) {
int min_remaining_time = 99999;
int min_index = -1;
for (i = 0; i < n; i++) {
if (q[i].remaining_time > 0 && q[i].remaining_time < min_remaining_time && time >= q[i].arrival_time) {
min_remaining_time = q[i].remaining_time;
min_index = i;
}
}
if (min_index == -1) {
break;
}
schedule_cpu(q[min_index].pid, 1);
q[min_index].remaining_time--;
time++;
}
}
int main() {
int i, n;
process p;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d%d", &p.arrival_time, &p.burst_time);
p.pid = i+1;
enqueue(p);
}
srt(queue, n);
return 0;
}
```
4. Feedback调度算法
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "schedule.h"
/* 队列结构体 */
typedef struct {
int pid; // 进程ID
int arrival_time; // 到达时间
int burst_time; // 执行时间
int remaining_time; // 剩余执行时间
int priority; // 优先级
} process;
/* 进程队列 */
process queue[10];
int front[5] = {-1, -1, -1, -1, -1};
int rear[5] = {-1, -1, -1, -1, -1};
/* 将进程按照到达时间排序 */
void sort_by_arrival_time(process *q, int n) {
int i, j;
process temp;
for (i = 0; i < n-1; i++) {
for (j = i+1; j < n; j++) {
if (q[i].arrival_time > q[j].arrival_time) {
temp = q[i];
q[i] = q[j];
q[j] = temp;
}
}
}
}
/* 将进程加入队列 */
void enqueue(process p, int priority) {
if (rear[priority] == 10-1) {
printf("Queue overflow!\n");
exit(1);
}
if (front[priority] == -1) {
front[priority] = 0;
}
rear[priority]++;
queue[rear[priority]] = p;
}
/* 从队列中取出一个进程 */
process dequeue(int priority) {
if (front[priority] == -1 || front[priority] > rear[priority]) {
printf("Queue underflow!\n");
exit(1);
}
process p = queue[front[priority]];
front[priority]++;
return p;
}
/* Feedback调度算法 */
void feedback(process *q, int n) {
sort_by_arrival_time(q, n);
int i, time = q[0].arrival_time, current_priority = 0;
for (i = 0; i < n; i++) {
q[i].remaining_time = q[i].burst_time;
q[i].priority = 0;
}
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (q[i].remaining_time > 0) {
done = 0;
if (q[i].remaining_time > 1) {
if (q[i].priority < 4) {
q[i].priority++;
}
enqueue(q[i], q[i].priority);
q[i].remaining_time--;
time++;
}
else {
schedule_cpu(q[i].pid, 1);
q[i].remaining_time = 0;
time++;
}
while (i < n-1 && time >= q[i+1].arrival_time) {
i++;
if (q[i].priority < current_priority) {
enqueue(q[i], q[i].priority+1);
}
else {
enqueue(q[i], q[i].priority);
}
}
}
}
if (done == 1) {
break;
}
current_priority++;
if (current_priority > 4) {
current_priority = 0;
}
}
}
int main() {
int i, n;
process p;
printf("Enter the number of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter arrival time and burst time for process %d: ", i+1);
scanf("%d%d", &p.arrival_time, &p.burst_time);
p.pid = i+1;
enqueue(p, 0);
}
feedback(queue, n);
return 0;
}
```
阅读全文