From 215738336aec0d5118a90a79d380c41910f6ee4b Mon Sep 17 00:00:00 2001
From: rsc <rsc>
Date: Sun, 31 May 2009 00:38:51 +0000
Subject: [PATCH] move fork into proc.c

---
 defs.h    |  7 ++++---
 proc.c    | 34 ++++++++++++++++++++--------------
 sysproc.c | 12 +-----------
 3 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/defs.h b/defs.h
index fe863cd..cb47716 100644
--- a/defs.h
+++ b/defs.h
@@ -94,6 +94,7 @@ int             pipewrite(struct pipe*, char*, int);
 // proc.c
 struct proc*    copyproc(struct proc*);
 void            exit(void);
+int             fork(void);
 int             growproc(int);
 int             kill(int);
 void            pinit(void);
@@ -146,9 +147,9 @@ void            tvinit(void);
 extern struct spinlock tickslock;
 
 // uart.c
-void		uartinit(void);
-void		uartintr(void);
-void		uartputc(int);
+void            uartinit(void);
+void            uartintr(void);
+void            uartputc(int);
 
 
 // number of elements in fixed-size array
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.
diff --git a/sysproc.c b/sysproc.c
index da01b5b..5ecbf2b 100644
--- a/sysproc.c
+++ b/sysproc.c
@@ -8,17 +8,7 @@
 int
 sys_fork(void)
 {
-  int pid;
-  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;
+  return fork();
 }
 
 int
-- 
GitLab