Last active
October 18, 2024 01:42
-
-
Save Pablo1107/3db182caa83e911857d9e18e27aaac73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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