summaryrefslogtreecommitdiff
path: root/proc.c
diff options
context:
space:
mode:
authorRobert Morris <[email protected]>2016-08-13 03:44:13 -0400
committerRobert Morris <[email protected]>2016-08-13 03:44:13 -0400
commit19f65413bdf7553036f2c388552580905730060a (patch)
treeae9767a66f8e58b3904b8316e1aac4a7d3387a04 /proc.c
parent20d05d4411e39ee8453310e73a6c92e4e3066e0b (diff)
downloadxv6-labs-19f65413bdf7553036f2c388552580905730060a.tar.gz
xv6-labs-19f65413bdf7553036f2c388552580905730060a.tar.bz2
xv6-labs-19f65413bdf7553036f2c388552580905730060a.zip
hold ptable.lock for the entire process creation
however, processes still modify their own p->* without locking
Diffstat (limited to 'proc.c')
-rw-r--r--proc.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/proc.c b/proc.c
index b6dd11f..85f3f17 100644
--- a/proc.c
+++ b/proc.c
@@ -31,23 +31,21 @@ pinit(void)
// If found, change state to EMBRYO and initialize
// state required to run in the kernel.
// Otherwise return 0.
+// Must hold ptable.lock.
static struct proc*
allocproc(void)
{
struct proc *p;
char *sp;
- acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
if(p->state == UNUSED)
goto found;
- release(&ptable.lock);
return 0;
found:
p->state = EMBRYO;
p->pid = nextpid++;
- release(&ptable.lock);
// Allocate kernel stack.
if((p->kstack = kalloc()) == 0){
@@ -81,6 +79,8 @@ userinit(void)
struct proc *p;
extern char _binary_initcode_start[], _binary_initcode_size[];
+ acquire(&ptable.lock);
+
p = allocproc();
initproc = p;
if((p->pgdir = setupkvm()) == 0)
@@ -100,6 +100,8 @@ userinit(void)
p->cwd = namei("/");
p->state = RUNNABLE;
+
+ release(&ptable.lock);
}
// Grow current process's memory by n bytes.
@@ -131,15 +133,20 @@ fork(void)
int i, pid;
struct proc *np;
+ acquire(&ptable.lock);
+
// Allocate process.
- if((np = allocproc()) == 0)
+ if((np = allocproc()) == 0){
+ release(&ptable.lock);
return -1;
+ }
// Copy process state from p.
if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
kfree(np->kstack);
np->kstack = 0;
np->state = UNUSED;
+ release(&ptable.lock);
return -1;
}
np->sz = proc->sz;
@@ -158,9 +165,8 @@ fork(void)
pid = np->pid;
- // lock to force the compiler to emit the np->state write last.
- acquire(&ptable.lock);
np->state = RUNNABLE;
+
release(&ptable.lock);
return pid;
@@ -233,11 +239,11 @@ wait(void)
kfree(p->kstack);
p->kstack = 0;
freevm(p->pgdir);
- p->state = UNUSED;
p->pid = 0;
p->parent = 0;
p->name[0] = 0;
p->killed = 0;
+ p->state = UNUSED;
release(&ptable.lock);
return pid;
}