summaryrefslogtreecommitdiff
path: root/kernel/proc.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/proc.c')
-rw-r--r--kernel/proc.c40
1 files changed, 16 insertions, 24 deletions
diff --git a/kernel/proc.c b/kernel/proc.c
index 087d504..b9babed 100644
--- a/kernel/proc.c
+++ b/kernel/proc.c
@@ -55,7 +55,7 @@ mycpu(void) {
return c;
}
-// Return the current struct proc *.
+// Return the current struct proc *, or zero if none.
struct proc*
myproc(void) {
push_off();
@@ -314,13 +314,12 @@ void
exit(void)
{
struct proc *p = myproc();
- int fd;
if(p == initproc)
panic("init exiting");
// Close all open files.
- for(fd = 0; fd < NOFILE; fd++){
+ for(int fd = 0; fd < NOFILE; fd++){
if(p->ofile[fd]){
struct file *f = p->ofile[fd];
fileclose(f);
@@ -337,14 +336,14 @@ exit(void)
acquire(&p->lock);
- // Give our children to init.
+ // Give any children to init.
reparent(p, p->parent);
- p->state = ZOMBIE;
-
// Parent might be sleeping in wait().
wakeup1(p->parent);
+ p->state = ZOMBIE;
+
release(&p->parent->lock);
// Jump into the scheduler, never to return.
@@ -416,7 +415,7 @@ scheduler(void)
c->proc = 0;
for(;;){
- // Give devices a brief chance to interrupt.
+ // Avoid deadlock by ensuring that devices can interrupt.
intr_on();
for(p = proc; p < &proc[NPROC]; p++) {
@@ -505,12 +504,6 @@ sleep(void *chan, struct spinlock *lk)
{
struct proc *p = myproc();
- if(p == 0)
- panic("sleep");
-
- if(lk == 0)
- panic("sleep without lk");
-
// Must acquire p->lock in order to
// change p->state and then call sched.
// Once we hold p->lock, we can be
@@ -538,17 +531,6 @@ sleep(void *chan, struct spinlock *lk)
}
}
-//PAGEBREAK!
-// Wake up p if it is sleeping in wait(); used by exit().
-// Caller must hold p->lock.
-static void
-wakeup1(struct proc *p)
-{
- if(p->chan == p && p->state == SLEEPING) {
- p->state = RUNNABLE;
- }
-}
-
// Wake up all processes sleeping on chan.
// Must be called without any p->lock.
void
@@ -565,6 +547,16 @@ wakeup(void *chan)
}
}
+// Wake up p if it is sleeping in wait(); used by exit().
+// Caller must hold p->lock.
+static void
+wakeup1(struct proc *p)
+{
+ if(p->chan == p && p->state == SLEEPING) {
+ p->state = RUNNABLE;
+ }
+}
+
// Kill the process with the given pid.
// The victim won't exit until it tries to return
// to user space (see usertrap() in trap.c).