进程调度算法edf和rms代码改进
时间: 2024-03-13 17:46:30 浏览: 77
EDF(Earliest Deadline First)和RMS(Rate-Monotonic Scheduling)是常用的实时系统进程调度算法,下面是它们的代码改进示例:
EDF算法改进:
原始代码:
```
void edf_scheduler() {
while (1) {
int min_deadline = INT_MAX;
int next_process = -1;
for (int i = 0; i < num_processes; i++) {
if (!processes[i].completed && processes[i].deadline < min_deadline) {
min_deadline = processes[i].deadline;
next_process = i;
}
}
if (next_process == -1) {
break;
}
execute_process(next_process);
}
}
```
改进后的代码:
```
void edf_scheduler() {
while (1) {
int min_deadline = INT_MAX;
int next_process = -1;
for (int i = 0; i < num_processes; i++) {
if (!processes[i].completed && processes[i].deadline < min_deadline) {
if (processes[i].arrival_time <= current_time) { // 添加判断进程是否到达
min_deadline = processes[i].deadline;
next_process = i;
}
}
}
if (next_process == -1) {
break;
}
execute_process(next_process);
}
}
```
改进后的代码增加了一个判断,判断进程是否到达,如果未到达就不会被调度执行。这样可以避免出现进程还未到达就被调度执行的情况。
RMS算法改进:
原始代码:
```
void rms_scheduler() {
while (1) {
int min_period = INT_MAX;
int next_process = -1;
for (int i = 0; i < num_processes; i++) {
if (!processes[i].completed && processes[i].period < min_period) {
min_period = processes[i].period;
next_process = i;
}
}
if (next_process == -1) {
break;
}
execute_process(next_process);
processes[next_process].executed++;
if (processes[next_process].executed == processes[next_process].execution_time) {
processes[next_process].completed = true;
}
}
}
```
改进后的代码:
```
void rms_scheduler() {
while (1) {
int min_period = INT_MAX;
int next_process = -1;
for (int i = 0; i < num_processes; i++) {
if (!processes[i].completed && processes[i].period < min_period) {
if (processes[i].arrival_time <= current_time) { // 添加判断进程是否到达
min_period = processes[i].period;
next_process = i;
}
}
}
if (next_process == -1) {
break;
}
execute_process(next_process);
processes[next_process].executed++;
if (processes[next_process].executed == processes[next_process].execution_time) {
processes[next_process].completed = true;
}
}
}
```
改进后的代码同样增加了一个判断,判断进程是否到达。如果未到达就不会被调度执行。这样可以避免出现进程还未到达就被调度执行的情况。
阅读全文