#include #include #include #include #include #include #define STACK_SIZE 8192 #define NPROC 128 #define SNONE 0 /* no process there */ #define SRUN 1 /* process can run */ #define SSLEEP 2 /* blocked process */ #define SMAIN 3 /* process cannot run, except if no more processes are ready */ /* * Structure of a process (a thread) */ struct proc { int p_state; /* state */ jmp_buf p_buf; /* cpu registers */ char p_stack[STACK_SIZE]; /* stack */ void* p_wchan; }; struct proc proc[NPROC]; /* the processes */ char* stack; /* lower adress of the stack (stack goes from stack to stack + STACK_SIZE */ int stack_index; /* an index used when we copy the stack */ int current; int save_in(int i) { /* save registers */ if(setjmp(proc[i].p_buf) == 0) { /* save stack */ for(stack_index=0; stack_index don't use local variable, and don't use memcpy as memcpy may use local variable */ /* restore stack (stack_index and current are both global variables) */ for(stack_index=0; stack_index