still serv_close bug...
[mit-jos.git] / kern / sched.c
blob739330906b092b96b89f7e98086a37174279b8e3
1 #include <inc/assert.h>
3 #include <kern/env.h>
4 #include <kern/pmap.h>
5 #include <kern/monitor.h>
8 // Choose a user environment to run and run it.
9 void
10 sched_yield(void)
12 // Implement simple round-robin scheduling.
13 // Search through 'envs' for a runnable environment,
14 // in circular fashion starting after the previously running env,
15 // and switch to the first such environment found.
16 // It's OK to choose the previously running env if no other env
17 // is runnable.
18 // But never choose envs[0], the idle environment,
19 // unless NOTHING else is runnable.
20 // LAB 4: Your code here.
21 int i;
23 // if there are no previous running environment,
24 // set this to the envs, i.e. envs[0]
25 if (curenv == NULL)
26 curenv = envs;
28 for (i = curenv - envs + 1; i < NENV; i++)
29 if (envs[i].env_status == ENV_RUNNABLE)
30 env_run(&envs[i]);
32 for (i = 1; i <= curenv - envs; i++)
33 if (envs[i].env_status == ENV_RUNNABLE)
34 env_run(&envs[i]);
36 // Run the special idle environment when nothing else is runnable.
37 if (envs[0].env_status == ENV_RUNNABLE)
38 env_run(&envs[0]);
39 else {
40 cprintf("Destroyed all environments - nothing more to do!\n");
41 while (1)
42 monitor(NULL);