Apply some corresponding bug fixes from wq branch here

This commit is contained in:
Frans Kaashoek 2019-07-03 15:18:55 -04:00
parent ccf299850b
commit cee830af24

View file

@ -288,6 +288,22 @@ fork(void)
return pid; return pid;
} }
void reparent(struct proc *p) {
struct proc *pp;
// Pass p's abandoned children to init.
for(pp = ptable.proc; pp < &ptable.proc[NPROC]; pp++){
acquire(&pp->lock);
if(pp->parent == p){
pp->parent = initproc;
if(pp->state == ZOMBIE) {
wakeup1(initproc);
}
}
release(&pp->lock);
}
}
// Exit the current process. Does not return. // Exit the current process. Does not return.
// An exited process remains in the zombie state // An exited process remains in the zombie state
// until its parent calls wait(). // until its parent calls wait().
@ -315,37 +331,22 @@ exit(void)
iput(cwd); iput(cwd);
end_op(); end_op();
reparent(p);
acquire(&p->parent->lock);
acquire(&p->lock); acquire(&p->lock);
p->cwd = 0; p->cwd = 0;
// Jump into the scheduler, never to return.
p->state = ZOMBIE; p->state = ZOMBIE;
sched();
panic("zombie exit");
}
void reparent(struct proc *p) { release(&p->parent->lock);
struct proc *pp;
struct proc *parent = p->parent;
acquire(&parent->lock);
// Parent might be sleeping in wait(). // Parent might be sleeping in wait().
wakeup1(parent); wakeup1(p->parent);
// Pass p's abandoned children to init. // Jump into the scheduler, never to return.
for(pp = ptable.proc; pp < &ptable.proc[NPROC]; pp++){ sched();
if(pp->parent == p){ panic("zombie exit");
pp->parent = initproc;
if(pp->state == ZOMBIE) {
acquire(&initproc->lock);
wakeup1(initproc);
acquire(&initproc->lock);
}
}
}
release(&parent->lock);
} }
// Wait for a child process to exit and return its pid. // Wait for a child process to exit and return its pid.
@ -421,9 +422,6 @@ scheduler(void)
// It should have changed its p->state before coming back. // It should have changed its p->state before coming back.
c->proc = 0; c->proc = 0;
release(&p->lock); release(&p->lock);
if(p->state == ZOMBIE) {
reparent(p);
}
} else { } else {
release(&p->lock); release(&p->lock);
} }