int main(void) { struct Process procs[NUM_STACKS]; unsigned int num_procs = 0; /* the next available stack. n should never be >= NUM_STACKS */ unsigned int active_proc = 0; unsigned int val; /* interrupt condition */ /* initialize the hardware timer */ *(PIC + VIC_INTENABLE) = PIC_TIMER01; *TIMER0 = 10000; *(TIMER0 + TIMER_CONTROL) = TIMER_EN | TIMER_32BIT | TIMER_PERIODIC | TIMER_INTEN; /* start running tasks */ procs[num_procs].stackptr = init_process(&(procs[num_procs]),STACK_SIZE,&first_task); num_procs++; while(1) { /* choose a process to run */ active_proc = scheduler(procs, num_procs, active_proc); /* run the process for some time */ /* bwputs("activate\n"); */ procs[active_proc].stackptr = activate(procs[active_proc].stackptr); /* handle the interrupt */ val = procs[active_proc].stackptr[2+7]; /* the interrupt value */ if(val == (unsigned int)PIC) { /* bwputs("hardware interrupt\n"); */ if(*PIC & PIC_TIMER01) { if(*(TIMER0 + TIMER_MIS)) { /* bwputs(" timer0\n"); */ *(TIMER0 + TIMER_INTCLR) = 1; } } } else if(val == (unsigned int)&yield) { /* bwputs("yield\n"); */ } else if(val == (unsigned int)&fork) { num_procs = _fork(procs, active_proc, num_procs); bwputs("fork\n"); } else if(val == (unsigned int)&write) { bwputs("write "); val = procs[active_proc].stackptr[2+0]; /* pid of receiver */ if(_write(&(procs[active_proc]),&(procs[val]))) { bwputs("-> blocked\n"); } else { bwputs("-> success\n"); } } else if(val == (unsigned int)&read) { bwputs("read "); if(_read(&(procs[active_proc]))) { bwputs("-> blocked\n"); } else { bwputs("-> success\n"); } } else if(val == (unsigned int)&getpid) { procs[active_proc].stackptr[2+0] = active_proc; } else { bwputs("UNKNOWN SYSCALL\n"); } } return 0; }
int first_task(void) { unsigned int pid; pid = fork(); if(!pid) { name_server_task(); } if(pid != NAME_SERVER_PID) { bwputs("name server PID: "); nputs(pid); bwputs("\n"); return 1; } yield_task(); return 0; }
int yield_task(void) { bwputs("yield\n"); while(1) { yield(); } return 0; }
unsigned int scheduler(struct Process procs[], unsigned int num_procs, unsigned int active_proc) { unsigned int pid; for(pid = nextpid(active_proc,num_procs); procs[pid].blocked; pid = nextpid(pid,num_procs)) { /* assume that pid is blocked */ if(pid == active_proc) { bwputs("DEADLOCK\n"); debug(pid,0,0); } } return pid; }
int main(void) { unsigned int first_stack[256]; unsigned int *first_stack_start = first_stack + 256 - 16; first_stack_start[0] = 0x10; first_stack_start[1] = (unsigned int)&first; bwputs("Starting\n"); activate(first_stack_start); while(1); /* We can't exit, there's nowhere to go */ return 0; }
void nputs(unsigned int n) { unsigned int x; int i = 30; char c[32]; c[31] = '\0'; do { x = n / 10 * 10; c[i] = digit(n-x); i--; n = x / 10; } while(x > 0 && i >= 0); bwputs(c + i + 1); }
unsigned int _fork(struct Process procs[], unsigned int parent_pid, unsigned int next_pid) { unsigned int offset; bwputs("_fork\n"); if(next_pid == NUM_STACKS) { procs[parent_pid].stackptr[2+0] = -1; return next_pid; } offset = procs[parent_pid].stackptr - procs[parent_pid].stack; procs[next_pid].stackptr = procs[next_pid].stack + offset; memcpy(procs[next_pid].stackptr, procs[parent_pid].stackptr, sizeof(*(procs[parent_pid].stack)) * (STACK_SIZE - offset)); procs[parent_pid].stackptr[2+0] = next_pid; procs[next_pid].stackptr[2+0] = 0; return next_pid + 1; }
void first(void) { bwputs("In user mode\n"); while(1); return ; }
int do_nothing_task(void) { bwputs("do nothing\n"); while(1); return 0; }
void debug(unsigned int a, unsigned int b, unsigned int c) { (void) a, (void) b, (void) c; bwputs("debug halt\n"); while(1); }
int main(void) { bwputs("Starting\n"); activate( /*void*/ ) ; while(1); /* We can't exit, there's nowhere to go */ return 0; }
void halt_and_catch_fire() { bwputs("OH NOES!!!!!!111oneone\n"); while(1); }