summaryrefslogtreecommitdiff
path: root/proc.c
diff options
context:
space:
mode:
authorrsc <rsc>2009-05-31 00:38:51 +0000
committerrsc <rsc>2009-05-31 00:38:51 +0000
commit215738336aec0d5118a90a79d380c41910f6ee4b (patch)
tree87fa465103f5d024a2e0292db2dddbb3ac54816a /proc.c
parentdae9b0d48a94683c0a49e88e4b739482788d7175 (diff)
downloadxv6-labs-215738336aec0d5118a90a79d380c41910f6ee4b.tar.gz
xv6-labs-215738336aec0d5118a90a79d380c41910f6ee4b.tar.bz2
xv6-labs-215738336aec0d5118a90a79d380c41910f6ee4b.zip
move fork into proc.c
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c34
1 files changed, 20 insertions, 14 deletions
diff --git a/proc.c b/proc.c
index 20f4be9..a2aeab2 100644
--- a/proc.c
+++ b/proc.c
@@ -130,34 +130,40 @@ usegment(void)
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
-struct proc*
-copyproc(struct proc *p)
+int
+fork(void)
{
- int i;
+ int i, pid;
struct proc *np;
// Allocate process.
if((np = allocproc()) == 0)
- return 0;
+ return -1;
// Copy process state from p.
- np->sz = p->sz;
+ np->sz = cp->sz;
if((np->mem = kalloc(np->sz)) == 0){
kfree(np->kstack, KSTACKSIZE);
np->kstack = 0;
np->state = UNUSED;
- return 0;
+ return -1;
}
- memmove(np->mem, p->mem, np->sz);
- np->parent = p;
- *np->tf = *p->tf;
+ memmove(np->mem, cp->mem, np->sz);
+ np->parent = cp;
+ *np->tf = *cp->tf;
- for(i = 0; i < NOFILE; i++)
- if(p->ofile[i])
- np->ofile[i] = filedup(p->ofile[i]);
- np->cwd = idup(p->cwd);
+ // Clear %eax so that fork returns 0 in the child.
+ np->tf->eax = 0;
- return np;
+ for(i = 0; i < NOFILE; i++)
+ if(cp->ofile[i])
+ np->ofile[i] = filedup(cp->ofile[i]);
+ np->cwd = idup(cp->cwd);
+
+ pid = np->pid;
+ np->state = RUNNABLE;
+
+ return pid;
}
// Set up first user process.