hold ptable.lock for the entire process creation
however, processes still modify their own p->* without locking
This commit is contained in:
parent
20d05d4411
commit
19f65413bd
4
Makefile
4
Makefile
|
@ -74,8 +74,8 @@ AS = $(TOOLPREFIX)gas
|
||||||
LD = $(TOOLPREFIX)ld
|
LD = $(TOOLPREFIX)ld
|
||||||
OBJCOPY = $(TOOLPREFIX)objcopy
|
OBJCOPY = $(TOOLPREFIX)objcopy
|
||||||
OBJDUMP = $(TOOLPREFIX)objdump
|
OBJDUMP = $(TOOLPREFIX)objdump
|
||||||
#CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
|
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer
|
||||||
CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -O0 -g -Wall -MD -gdwarf-2 -m32 -Werror -fno-omit-frame-pointer
|
#CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -fvar-tracking -fvar-tracking-assignments -O0 -g -Wall -MD -gdwarf-2 -m32 -Werror -fno-omit-frame-pointer
|
||||||
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
|
CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
|
||||||
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
|
ASFLAGS = -m32 -gdwarf-2 -Wa,-divide
|
||||||
# FreeBSD ld wants ``elf_i386_fbsd''
|
# FreeBSD ld wants ``elf_i386_fbsd''
|
||||||
|
|
20
proc.c
20
proc.c
|
@ -31,23 +31,21 @@ pinit(void)
|
||||||
// If found, change state to EMBRYO and initialize
|
// If found, change state to EMBRYO and initialize
|
||||||
// state required to run in the kernel.
|
// state required to run in the kernel.
|
||||||
// Otherwise return 0.
|
// Otherwise return 0.
|
||||||
|
// Must hold ptable.lock.
|
||||||
static struct proc*
|
static struct proc*
|
||||||
allocproc(void)
|
allocproc(void)
|
||||||
{
|
{
|
||||||
struct proc *p;
|
struct proc *p;
|
||||||
char *sp;
|
char *sp;
|
||||||
|
|
||||||
acquire(&ptable.lock);
|
|
||||||
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
|
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
|
||||||
if(p->state == UNUSED)
|
if(p->state == UNUSED)
|
||||||
goto found;
|
goto found;
|
||||||
release(&ptable.lock);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
found:
|
found:
|
||||||
p->state = EMBRYO;
|
p->state = EMBRYO;
|
||||||
p->pid = nextpid++;
|
p->pid = nextpid++;
|
||||||
release(&ptable.lock);
|
|
||||||
|
|
||||||
// Allocate kernel stack.
|
// Allocate kernel stack.
|
||||||
if((p->kstack = kalloc()) == 0){
|
if((p->kstack = kalloc()) == 0){
|
||||||
|
@ -81,6 +79,8 @@ userinit(void)
|
||||||
struct proc *p;
|
struct proc *p;
|
||||||
extern char _binary_initcode_start[], _binary_initcode_size[];
|
extern char _binary_initcode_start[], _binary_initcode_size[];
|
||||||
|
|
||||||
|
acquire(&ptable.lock);
|
||||||
|
|
||||||
p = allocproc();
|
p = allocproc();
|
||||||
initproc = p;
|
initproc = p;
|
||||||
if((p->pgdir = setupkvm()) == 0)
|
if((p->pgdir = setupkvm()) == 0)
|
||||||
|
@ -100,6 +100,8 @@ userinit(void)
|
||||||
p->cwd = namei("/");
|
p->cwd = namei("/");
|
||||||
|
|
||||||
p->state = RUNNABLE;
|
p->state = RUNNABLE;
|
||||||
|
|
||||||
|
release(&ptable.lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grow current process's memory by n bytes.
|
// Grow current process's memory by n bytes.
|
||||||
|
@ -131,15 +133,20 @@ fork(void)
|
||||||
int i, pid;
|
int i, pid;
|
||||||
struct proc *np;
|
struct proc *np;
|
||||||
|
|
||||||
|
acquire(&ptable.lock);
|
||||||
|
|
||||||
// Allocate process.
|
// Allocate process.
|
||||||
if((np = allocproc()) == 0)
|
if((np = allocproc()) == 0){
|
||||||
|
release(&ptable.lock);
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Copy process state from p.
|
// Copy process state from p.
|
||||||
if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
|
if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
|
||||||
kfree(np->kstack);
|
kfree(np->kstack);
|
||||||
np->kstack = 0;
|
np->kstack = 0;
|
||||||
np->state = UNUSED;
|
np->state = UNUSED;
|
||||||
|
release(&ptable.lock);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
np->sz = proc->sz;
|
np->sz = proc->sz;
|
||||||
|
@ -158,9 +165,8 @@ fork(void)
|
||||||
|
|
||||||
pid = np->pid;
|
pid = np->pid;
|
||||||
|
|
||||||
// lock to force the compiler to emit the np->state write last.
|
|
||||||
acquire(&ptable.lock);
|
|
||||||
np->state = RUNNABLE;
|
np->state = RUNNABLE;
|
||||||
|
|
||||||
release(&ptable.lock);
|
release(&ptable.lock);
|
||||||
|
|
||||||
return pid;
|
return pid;
|
||||||
|
@ -233,11 +239,11 @@ wait(void)
|
||||||
kfree(p->kstack);
|
kfree(p->kstack);
|
||||||
p->kstack = 0;
|
p->kstack = 0;
|
||||||
freevm(p->pgdir);
|
freevm(p->pgdir);
|
||||||
p->state = UNUSED;
|
|
||||||
p->pid = 0;
|
p->pid = 0;
|
||||||
p->parent = 0;
|
p->parent = 0;
|
||||||
p->name[0] = 0;
|
p->name[0] = 0;
|
||||||
p->killed = 0;
|
p->killed = 0;
|
||||||
|
p->state = UNUSED;
|
||||||
release(&ptable.lock);
|
release(&ptable.lock);
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue