Skip to content

Instantly share code, notes, and snippets.

@Pablo1107
Last active October 18, 2024 01:42
Show Gist options
  • Save Pablo1107/3db182caa83e911857d9e18e27aaac73 to your computer and use it in GitHub Desktop.
Save Pablo1107/3db182caa83e911857d9e18e27aaac73 to your computer and use it in GitHub Desktop.
#include "sched.h"
#include "switch.h"
#include <stdlib.h>
#include <limits.h>
extern struct proc proc[];
struct proc* select_next(){
int next_rnd = -1;
struct proc *next = NULL;
for (int i = 0; i < NUMPROC; i++) {
if (proc[i].status == RUNNABLE) {
int random = rand();
if (random > next_rnd) {
next_rnd = random;
next = &proc[i];
}
}
}
return next;
}
void scheduler(){
while (!done()){
struct proc *candidate = select_next();
if (candidate != NULL) {
candidate->status = RUNNING;
candidate->runtime += swtch(candidate);
} else {
idle();
}
}
}
#include "sched.h"
#include "switch.h"
#include <stdlib.h>
#include <limits.h>
extern struct proc proc[];
struct proc* select_next(){
int next_runtime = INT_MAX;
struct proc *next = NULL;
for (int i = 0; i < NUMPROC; i++) {
if (proc[i].status == RUNNABLE) {
if (proc[i].runtime <= next_runtime) {
next_runtime = proc[i].runtime;
next = &proc[i];
}
}
}
return next;
}
void scheduler(){
while (!done()){
struct proc *candidate = select_next();
if (candidate != NULL) {
candidate->status = RUNNING;
candidate->runtime += swtch(candidate);
} else {
idle();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment