move fork into proc.c
This commit is contained in:
parent
dae9b0d48a
commit
215738336a
3 changed files with 24 additions and 27 deletions
1
defs.h
1
defs.h
|
@ -94,6 +94,7 @@ int pipewrite(struct pipe*, char*, int);
|
||||||
// proc.c
|
// proc.c
|
||||||
struct proc* copyproc(struct proc*);
|
struct proc* copyproc(struct proc*);
|
||||||
void exit(void);
|
void exit(void);
|
||||||
|
int fork(void);
|
||||||
int growproc(int);
|
int growproc(int);
|
||||||
int kill(int);
|
int kill(int);
|
||||||
void pinit(void);
|
void pinit(void);
|
||||||
|
|
32
proc.c
32
proc.c
|
@ -130,34 +130,40 @@ usegment(void)
|
||||||
// Create a new process copying p as the parent.
|
// Create a new process copying p as the parent.
|
||||||
// Sets up stack to return as if from system call.
|
// Sets up stack to return as if from system call.
|
||||||
// Caller must set state of returned proc to RUNNABLE.
|
// Caller must set state of returned proc to RUNNABLE.
|
||||||
struct proc*
|
int
|
||||||
copyproc(struct proc *p)
|
fork(void)
|
||||||
{
|
{
|
||||||
int i;
|
int i, pid;
|
||||||
struct proc *np;
|
struct proc *np;
|
||||||
|
|
||||||
// Allocate process.
|
// Allocate process.
|
||||||
if((np = allocproc()) == 0)
|
if((np = allocproc()) == 0)
|
||||||
return 0;
|
return -1;
|
||||||
|
|
||||||
// Copy process state from p.
|
// Copy process state from p.
|
||||||
np->sz = p->sz;
|
np->sz = cp->sz;
|
||||||
if((np->mem = kalloc(np->sz)) == 0){
|
if((np->mem = kalloc(np->sz)) == 0){
|
||||||
kfree(np->kstack, KSTACKSIZE);
|
kfree(np->kstack, KSTACKSIZE);
|
||||||
np->kstack = 0;
|
np->kstack = 0;
|
||||||
np->state = UNUSED;
|
np->state = UNUSED;
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
memmove(np->mem, p->mem, np->sz);
|
memmove(np->mem, cp->mem, np->sz);
|
||||||
np->parent = p;
|
np->parent = cp;
|
||||||
*np->tf = *p->tf;
|
*np->tf = *cp->tf;
|
||||||
|
|
||||||
|
// Clear %eax so that fork returns 0 in the child.
|
||||||
|
np->tf->eax = 0;
|
||||||
|
|
||||||
for(i = 0; i < NOFILE; i++)
|
for(i = 0; i < NOFILE; i++)
|
||||||
if(p->ofile[i])
|
if(cp->ofile[i])
|
||||||
np->ofile[i] = filedup(p->ofile[i]);
|
np->ofile[i] = filedup(cp->ofile[i]);
|
||||||
np->cwd = idup(p->cwd);
|
np->cwd = idup(cp->cwd);
|
||||||
|
|
||||||
return np;
|
pid = np->pid;
|
||||||
|
np->state = RUNNABLE;
|
||||||
|
|
||||||
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up first user process.
|
// Set up first user process.
|
||||||
|
|
12
sysproc.c
12
sysproc.c
|
@ -8,17 +8,7 @@
|
||||||
int
|
int
|
||||||
sys_fork(void)
|
sys_fork(void)
|
||||||
{
|
{
|
||||||
int pid;
|
return fork();
|
||||||
struct proc *np;
|
|
||||||
|
|
||||||
if((np = copyproc(cp)) == 0)
|
|
||||||
return -1;
|
|
||||||
pid = np->pid;
|
|
||||||
|
|
||||||
// Clear %eax so that fork returns 0 in the child.
|
|
||||||
np->tf->eax = 0;
|
|
||||||
np->state = RUNNABLE;
|
|
||||||
return pid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in a new issue