avoid hardcoding init as pid 1 proc[0]

This commit is contained in:
rsc 2007-08-23 14:35:28 +00:00
parent 1d7839a1da
commit 3a057d12ae

20
proc.c
View file

@ -11,6 +11,8 @@ struct spinlock proc_table_lock;
struct proc proc[NPROC]; struct proc proc[NPROC];
struct proc *curproc[NCPU]; struct proc *curproc[NCPU];
static struct proc *initproc;
int nextpid = 1; int nextpid = 1;
extern void forkret(void); extern void forkret(void);
extern void forkret1(struct trapframe*); extern void forkret1(struct trapframe*);
@ -162,7 +164,7 @@ userinit(void)
p->tf->eflags = FL_IF; p->tf->eflags = FL_IF;
p->tf->esp = p->sz; p->tf->esp = p->sz;
// Push dummy return address to placate gcc. // Make return address readable; needed for some gcc.
p->tf->esp -= 4; p->tf->esp -= 4;
*(uint*)(p->mem + p->tf->esp) = 0xefefefef; *(uint*)(p->mem + p->tf->esp) = 0xefefefef;
@ -170,6 +172,8 @@ userinit(void)
memmove(p->mem, _binary_initcode_start, (int)_binary_initcode_size); memmove(p->mem, _binary_initcode_start, (int)_binary_initcode_size);
safestrcpy(p->name, "initcode", sizeof(p->name)); safestrcpy(p->name, "initcode", sizeof(p->name));
p->state = RUNNABLE; p->state = RUNNABLE;
initproc = p;
} }
//PAGEBREAK: 42 //PAGEBREAK: 42
@ -346,7 +350,7 @@ proc_exit(void)
struct proc *p; struct proc *p;
int fd; int fd;
if(cp->pid == 1) if(cp == initproc)
panic("init exiting"); panic("init exiting");
// Close all open files. // Close all open files.
@ -362,17 +366,19 @@ proc_exit(void)
acquire(&proc_table_lock); acquire(&proc_table_lock);
// Wake up our parent. // Wake up waiting parent.
for(p = proc; p < &proc[NPROC]; p++) for(p = proc; p < &proc[NPROC]; p++)
if(p->pid == cp->ppid) if(p->pid == cp->ppid)
wakeup1(p); wakeup1(p);
// Reparent our children to process 1. // Pass abandoned children to init.
for(p = proc; p < &proc[NPROC]; p++) for(p = proc; p < &proc[NPROC]; p++){
if(p->ppid == cp->pid){ if(p->ppid == cp->pid){
p->ppid = 1; p->ppid = initproc->pid;
wakeup1(&proc[1]); // init if(p->state == ZOMBIE)
wakeup1(initproc);
} }
}
// Jump into the scheduler, never to return. // Jump into the scheduler, never to return.
cp->killed = 0; cp->killed = 0;