fork minibug
This commit is contained in:
parent
4f06ae0d42
commit
c2258bf4d2
2 changed files with 27 additions and 1 deletions
24
TRICKS
24
TRICKS
|
@ -110,3 +110,27 @@ moves reads down after writes, but the language in
|
||||||
the spec allows it. There is no telling whether future
|
the spec allows it. There is no telling whether future
|
||||||
processors will need it.
|
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;".
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,14 @@
|
||||||
int
|
int
|
||||||
sys_fork(void)
|
sys_fork(void)
|
||||||
{
|
{
|
||||||
|
int pid;
|
||||||
struct proc *np;
|
struct proc *np;
|
||||||
|
|
||||||
if((np = copyproc(cp)) == 0)
|
if((np = copyproc(cp)) == 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
pid = np->pid;
|
||||||
np->state = RUNNABLE;
|
np->state = RUNNABLE;
|
||||||
return np->pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Reference in a new issue