#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 SMAIN 2 /* process cannot run, except if no more processes are ready */ /* * Structure of a process (a thread) */ struct proc { int state; /* state */ jmp_buf buf; /* cpu registers */ char stack[STACK_SIZE]; /* stack */ }; 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].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