From 7472b2b451f100162fa4542f5bfe260385f861ad Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Tue, 31 Aug 2010 16:26:08 -0400 Subject: Fix too-long lines --- proc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 5ac2780..0dc77b8 100644 --- a/proc.c +++ b/proc.c @@ -120,7 +120,8 @@ userinit(void) panic("userinit: out of memory?"); if (!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size)) panic("userinit: out of memory?"); - inituvm(p->pgdir, 0x0, _binary_initcode_start, (int)_binary_initcode_size); + inituvm(p->pgdir, 0x0, _binary_initcode_start, + (int)_binary_initcode_size); p->sz = PGROUNDUP((int)_binary_initcode_size); memset(p->tf, 0, sizeof(*p->tf)); p->tf->cs = (SEG_UCODE << 3) | DPL_USER; -- cgit v1.2.3 From b0751a3e9bfce88cb07c1a540ceabf21f2d53b31 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Wed, 1 Sep 2010 00:41:25 -0400 Subject: Space police --- proc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 0dc77b8..375b1bb 100644 --- a/proc.c +++ b/proc.c @@ -116,9 +116,9 @@ userinit(void) p = allocproc(); initproc = p; - if (!(p->pgdir = setupkvm())) + if(!(p->pgdir = setupkvm())) panic("userinit: out of memory?"); - if (!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size)) + if(!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size)) panic("userinit: out of memory?"); inituvm(p->pgdir, 0x0, _binary_initcode_start, (int)_binary_initcode_size); @@ -144,10 +144,10 @@ int growproc(int n) { if(n > 0){ - if (!allocuvm(proc->pgdir, (char *)proc->sz, n)) + if(!allocuvm(proc->pgdir, (char *)proc->sz, n)) return -1; } else if(n < 0){ - if (!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n)) + if(!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n)) return -1; } proc->sz += n; @@ -169,7 +169,7 @@ fork(void) return -1; // Copy process state from p. - if (!(np->pgdir = copyuvm(proc->pgdir, proc->sz))) { + if(!(np->pgdir = copyuvm(proc->pgdir, proc->sz))){ kfree(np->kstack); np->kstack = 0; np->state = UNUSED; -- cgit v1.2.3 From d8828817d72962a6220cb1fca315cab4bbf6d0a3 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 2 Sep 2010 04:15:17 -0400 Subject: Rearrange proc.h and proc.c to get our action-packed spreads back (mostly). They also make sense in this order, so it's not just for page layout. --- proc.c | 172 ++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 86 insertions(+), 86 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 375b1bb..8197377 100644 --- a/proc.c +++ b/proc.c @@ -193,6 +193,92 @@ fork(void) return pid; } +// Exit the current process. Does not return. +// An exited process remains in the zombie state +// until its parent calls wait() to find out it exited. +void +exit(void) +{ + struct proc *p; + int fd; + + if(proc == initproc) + panic("init exiting"); + + // Close all open files. + for(fd = 0; fd < NOFILE; fd++){ + if(proc->ofile[fd]){ + fileclose(proc->ofile[fd]); + proc->ofile[fd] = 0; + } + } + + iput(proc->cwd); + proc->cwd = 0; + + acquire(&ptable.lock); + + // Parent might be sleeping in wait(). + wakeup1(proc->parent); + + // Pass abandoned children to init. + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->parent == proc){ + p->parent = initproc; + if(p->state == ZOMBIE) + wakeup1(initproc); + } + } + + // Jump into the scheduler, never to return. + proc->state = ZOMBIE; + sched(); + panic("zombie exit"); +} + +// Wait for a child process to exit and return its pid. +// Return -1 if this process has no children. +int +wait(void) +{ + struct proc *p; + int havekids, pid; + + acquire(&ptable.lock); + for(;;){ + // Scan through table looking for zombie children. + havekids = 0; + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->parent != proc) + continue; + havekids = 1; + if(p->state == ZOMBIE){ + // Found one. + pid = p->pid; + kfree(p->kstack); + p->kstack = 0; + freevm(p->pgdir); + p->state = UNUSED; + p->pid = 0; + p->parent = 0; + p->name[0] = 0; + p->killed = 0; + release(&ptable.lock); + return pid; + } + } + + // No point waiting if we don't have any children. + if(!havekids || proc->killed){ + release(&ptable.lock); + return -1; + } + + // Wait for children to exit. (See wakeup1 call in proc_exit.) + sleep(proc, &ptable.lock); //DOC: wait-sleep + } +} + //PAGEBREAK: 42 // Per-CPU process scheduler. // Each CPU calls scheduler() after setting itself up. @@ -357,89 +443,3 @@ kill(int pid) return -1; } -// Exit the current process. Does not return. -// An exited process remains in the zombie state -// until its parent calls wait() to find out it exited. -void -exit(void) -{ - struct proc *p; - int fd; - - if(proc == initproc) - panic("init exiting"); - - // Close all open files. - for(fd = 0; fd < NOFILE; fd++){ - if(proc->ofile[fd]){ - fileclose(proc->ofile[fd]); - proc->ofile[fd] = 0; - } - } - - iput(proc->cwd); - proc->cwd = 0; - - acquire(&ptable.lock); - - // Parent might be sleeping in wait(). - wakeup1(proc->parent); - - // Pass abandoned children to init. - for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ - if(p->parent == proc){ - p->parent = initproc; - if(p->state == ZOMBIE) - wakeup1(initproc); - } - } - - // Jump into the scheduler, never to return. - proc->state = ZOMBIE; - sched(); - panic("zombie exit"); -} - -// Wait for a child process to exit and return its pid. -// Return -1 if this process has no children. -int -wait(void) -{ - struct proc *p; - int havekids, pid; - - acquire(&ptable.lock); - for(;;){ - // Scan through table looking for zombie children. - havekids = 0; - for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ - if(p->parent != proc) - continue; - havekids = 1; - if(p->state == ZOMBIE){ - // Found one. - pid = p->pid; - kfree(p->kstack); - p->kstack = 0; - freevm(p->pgdir); - p->state = UNUSED; - p->pid = 0; - p->parent = 0; - p->name[0] = 0; - p->killed = 0; - release(&ptable.lock); - return pid; - } - } - - // No point waiting if we don't have any children. - if(!havekids || proc->killed){ - release(&ptable.lock); - return -1; - } - - // Wait for children to exit. (See wakeup1 call in proc_exit.) - sleep(proc, &ptable.lock); //DOC: wait-sleep - } -} - -- cgit v1.2.3 From c7c21467c3d3c9f2a04d6fac3b8ef796470b0448 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 2 Sep 2010 14:30:06 -0400 Subject: Oops. Broke the build when I rearranged proc.c --- proc.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 8197377..a72ec6c 100644 --- a/proc.c +++ b/proc.c @@ -17,6 +17,8 @@ int nextpid = 1; extern void forkret(void); extern void trapret(void); +static void wakeup1(void *chan); + void pinit(void) { -- cgit v1.2.3 From f53e6110bed159c8541c6e0d2fc1b1ffac2d141a Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 2 Sep 2010 15:37:05 -0400 Subject: Simplify inituvm and userinit by assuming initcode fits on a page --- proc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index a72ec6c..853eb0a 100644 --- a/proc.c +++ b/proc.c @@ -120,11 +120,8 @@ userinit(void) initproc = p; if(!(p->pgdir = setupkvm())) panic("userinit: out of memory?"); - if(!allocuvm(p->pgdir, 0x0, (int)_binary_initcode_size)) - panic("userinit: out of memory?"); - inituvm(p->pgdir, 0x0, _binary_initcode_start, - (int)_binary_initcode_size); - p->sz = PGROUNDUP((int)_binary_initcode_size); + inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size); + p->sz = PGSIZE; memset(p->tf, 0, sizeof(*p->tf)); p->tf->cs = (SEG_UCODE << 3) | DPL_USER; p->tf->ds = (SEG_UDATA << 3) | DPL_USER; -- cgit v1.2.3 From 79cd8b3eedeb1f85d3b19fb6119bd5224c4c532a Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 2 Sep 2010 18:28:36 -0400 Subject: Simplify allocuvm/deallocuvm to operate in a contiguous memory model. This makes their interface match up better with proc->sz and also simplifies the callers (it even gets the main body of exec on one page). --- proc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 853eb0a..6f7bdb6 100644 --- a/proc.c +++ b/proc.c @@ -142,14 +142,15 @@ userinit(void) int growproc(int n) { + uint sz = proc->sz; if(n > 0){ - if(!allocuvm(proc->pgdir, (char *)proc->sz, n)) + if(!(sz = allocuvm(proc->pgdir, sz, sz + n))) return -1; } else if(n < 0){ - if(!deallocuvm(proc->pgdir, (char *)(proc->sz + n), 0 - n)) + if(!(sz = deallocuvm(proc->pgdir, sz, sz + n))) return -1; } - proc->sz += n; + proc->sz = sz; switchuvm(proc); return 0; } -- cgit v1.2.3 From faad047ab22cbe989c208bff5ecb42608ecb8d7b Mon Sep 17 00:00:00 2001 From: Robert Morris Date: Mon, 13 Sep 2010 15:34:44 -0400 Subject: change some comments, maybe more informative delete most comments from bootother.S (since copy of bootasm.S) ksegment() -> seginit() move more stuff from main() to mainc() --- proc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 6f7bdb6..2e8a0a4 100644 --- a/proc.c +++ b/proc.c @@ -65,7 +65,8 @@ procdump(void) //PAGEBREAK: 32 // Look in the process table for an UNUSED proc. -// If found, change state to EMBRYO and return it. +// If found, change state to EMBRYO and initialize +// state required to run in the kernel. // Otherwise return 0. static struct proc* allocproc(void) @@ -97,7 +98,7 @@ found: p->tf = (struct trapframe*)sp; // Set up new context to start executing at forkret, - // which returns to trapret (see below). + // which returns to trapret. sp -= 4; *(uint*)sp = (uint)trapret; @@ -105,6 +106,7 @@ found: p->context = (struct context*)sp; memset(p->context, 0, sizeof *p->context); p->context->eip = (uint)forkret; + return p; } -- cgit v1.2.3 From 1a81e38b17144624415d252a521fd5a06079d681 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 11 Jan 2011 13:01:13 -0500 Subject: make new code like old code Variable declarations at top of function, separate from initialization. Use == 0 instead of ! for checking pointers. Consistent spacing around {, *, casts. Declare 0-parameter functions as (void) not (). Integer valued functions return -1 on failure, 0 on success. --- proc.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index 2e8a0a4..e6ccd9d 100644 --- a/proc.c +++ b/proc.c @@ -120,7 +120,7 @@ userinit(void) p = allocproc(); initproc = p; - if(!(p->pgdir = setupkvm())) + if((p->pgdir = setupkvm()) == 0) panic("userinit: out of memory?"); inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size); p->sz = PGSIZE; @@ -144,12 +144,14 @@ userinit(void) int growproc(int n) { - uint sz = proc->sz; + uint sz; + + sz = proc->sz; if(n > 0){ - if(!(sz = allocuvm(proc->pgdir, sz, sz + n))) + if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0) return -1; } else if(n < 0){ - if(!(sz = deallocuvm(proc->pgdir, sz, sz + n))) + if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0) return -1; } proc->sz = sz; @@ -171,7 +173,7 @@ fork(void) return -1; // Copy process state from p. - if(!(np->pgdir = copyuvm(proc->pgdir, proc->sz))){ + if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){ kfree(np->kstack); np->kstack = 0; np->state = UNUSED; -- cgit v1.2.3 From cf4b1ad90bcaeeb0c8458098c87948f61d408f94 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Sat, 19 Feb 2011 21:17:55 -0500 Subject: xv6: formatting, cleanup, rev5 (take 2) --- proc.c | 76 +++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 38 insertions(+), 38 deletions(-) (limited to 'proc.c') diff --git a/proc.c b/proc.c index e6ccd9d..eb334d0 100644 --- a/proc.c +++ b/proc.c @@ -25,44 +25,6 @@ pinit(void) initlock(&ptable.lock, "ptable"); } -//PAGEBREAK: 36 -// Print a process listing to console. For debugging. -// Runs when user types ^P on console. -// No lock to avoid wedging a stuck machine further. -void -procdump(void) -{ - static char *states[] = { - [UNUSED] "unused", - [EMBRYO] "embryo", - [SLEEPING] "sleep ", - [RUNNABLE] "runble", - [RUNNING] "run ", - [ZOMBIE] "zombie" - }; - int i; - struct proc *p; - char *state; - uint pc[10]; - - for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ - if(p->state == UNUSED) - continue; - if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) - state = states[p->state]; - else - state = "???"; - cprintf("%d %s %s", p->pid, state, p->name); - if(p->state == SLEEPING){ - getcallerpcs((uint*)p->context->ebp+2, pc); - for(i=0; i<10 && pc[i] != 0; i++) - cprintf(" %p", pc[i]); - } - cprintf("\n"); - } -} - - //PAGEBREAK: 32 // Look in the process table for an UNUSED proc. // If found, change state to EMBRYO and initialize @@ -447,3 +409,41 @@ kill(int pid) return -1; } +//PAGEBREAK: 36 +// Print a process listing to console. For debugging. +// Runs when user types ^P on console. +// No lock to avoid wedging a stuck machine further. +void +procdump(void) +{ + static char *states[] = { + [UNUSED] "unused", + [EMBRYO] "embryo", + [SLEEPING] "sleep ", + [RUNNABLE] "runble", + [RUNNING] "run ", + [ZOMBIE] "zombie" + }; + int i; + struct proc *p; + char *state; + uint pc[10]; + + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->state == UNUSED) + continue; + if(p->state >= 0 && p->state < NELEM(states) && states[p->state]) + state = states[p->state]; + else + state = "???"; + cprintf("%d %s %s", p->pid, state, p->name); + if(p->state == SLEEPING){ + getcallerpcs((uint*)p->context->ebp+2, pc); + for(i=0; i<10 && pc[i] != 0; i++) + cprintf(" %p", pc[i]); + } + cprintf("\n"); + } +} + + -- cgit v1.2.3