]> Devi Nivas Git - cs3210-lab0.git/commitdiff
fork minibug
authorrsc <rsc>
Wed, 28 Nov 2007 20:47:22 +0000 (20:47 +0000)
committerrsc <rsc>
Wed, 28 Nov 2007 20:47:22 +0000 (20:47 +0000)
TRICKS
sysproc.c

diff --git a/TRICKS b/TRICKS
index 688358898928f0c2b68194bf3ba0929c497826db..b5388343dc2a60d3945eb9f11dea9d94a07160a1 100644 (file)
--- a/TRICKS
+++ b/TRICKS
@@ -110,3 +110,27 @@ moves reads down after writes, but the language in
 the spec allows it.  There is no telling whether future
 processors will need it.
 
+---
+
+The code in sys_fork needs to read np->pid before
+setting np->state to RUNNABLE.  
+
+       int
+       sys_fork(void)
+       {
+         int pid;
+         struct proc *np;
+       
+         if((np = copyproc(cp)) == 0)
+           return -1;
+         pid = np->pid;
+         np->state = RUNNABLE;
+         return pid;
+       }
+
+After setting np->state to RUNNABLE, some other CPU
+might run the process, it might exit, and then it might
+get reused for a different process (with a new pid), all
+before the return statement.  So it's not safe to just do
+"return np->pid;".
+
index 4a9c8de3371e17be54b4fe1b0ec3ba9ad45f367a..990a4261ef6c319b129ef044df2e6a9c78855ffb 100644 (file)
--- a/sysproc.c
+++ b/sysproc.c
@@ -7,12 +7,14 @@
 int
 sys_fork(void)
 {
+  int pid;
   struct proc *np;
 
   if((np = copyproc(cp)) == 0)
     return -1;
+  pid = np->pid;
   np->state = RUNNABLE;
-  return np->pid;
+  return pid;
 }
 
 int