diff options
Diffstat (limited to 'kernel/proc/proc.c')
-rw-r--r-- | kernel/proc/proc.c | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c index ef8245d..73c6aa0 100644 --- a/kernel/proc/proc.c +++ b/kernel/proc/proc.c @@ -211,15 +211,13 @@ proc_t *proc_create(const char *name) if (proc_pid == PID_INIT) { - // if it's init proc, set to global variable + // if it's init proc, set to global variable proc_initproc = proc; } - else - { - // FIXME: ask in hours - // if it's not init proc, set parent proc to the init proc - proc->p_pproc = proc_initproc; - } + + proc->p_pproc = proc_initproc; + list_insert_tail(&curproc->p_children, &proc->p_child_link); + list_insert_tail(&proc_list, &proc->p_list_link); return proc; } @@ -245,36 +243,30 @@ void proc_cleanup(long status) { // NOT_YET_IMPLEMENTED("PROCS: proc_cleanup"); - // free resources of this proc (save slab_obj_free for proc_destroy) - pt_destroy(curproc->p_pml4); - - - // NOT_YET_IMPLEMENTED("PROCS: proc_cleanup"); if (curproc->p_pid == PID_INIT) { // if it's init proc, shutdown weenix initproc_finish(status); } - else - { - // if it's not init proc, reparent child processes to parent proc - if (curproc->p_pproc == NULL) - { - panic("parent process not found in proc_pleanup"); - } - list_link_t *link; - list_iterate(&curproc->p_children, child, proc_t, p_child_link) - { - // remove each child from current process and insert to init process - list_remove(&child->p_child_link); - list_insert_tail(&curproc->p_pproc->p_children, &child->p_child_link); - } + // if it's not init proc, reparent child processes to parent proc + if (curproc->p_pproc == NULL) + { + panic("parent process not found in proc_pleanup"); + } - // update state and status - curproc->p_state = PROC_DEAD; - curproc->p_status = status; + list_link_t *link; + list_iterate(&curproc->p_children, child, proc_t, p_child_link) + { + // remove insert to init process + list_insert_tail(&curproc->p_pproc->p_children, &child->p_child_link); + list_remove(&child->p_child_link); + child->p_pproc = proc_initproc; } + + // update state and status + curproc->p_state = PROC_DEAD; + curproc->p_status = status; } /* @@ -298,8 +290,8 @@ void proc_thread_exiting(void *retval) proc_cleanup((long)retval); curthr->kt_state = KT_EXITED; - sched_broadcast_on(&curproc->p_wait); - sched_switch(&curproc->p_wait); + sched_broadcast_on(&curproc->p_pproc->p_wait); + sched_switch(0); } /* @@ -425,6 +417,8 @@ pid_t do_waitpid(pid_t pid, int *status, int options) return -ENOTSUP; } + dbg(DBG_PROC, "waiting for pid=%d\n", pid); + if (pid > 0) { proc_t *child = proc_lookup(pid); @@ -445,6 +439,7 @@ pid_t do_waitpid(pid_t pid, int *status, int options) } proc_destroy(child); + dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid); return pid; } else if (pid == -1) @@ -454,19 +449,31 @@ pid_t do_waitpid(pid_t pid, int *status, int options) return -ECHILD; } - proc_t *child; - list_iterate(&curproc->p_children, child, proc_t, p_child_link) - { - if (child->p_state == PROC_DEAD) + + while(1) { + proc_t *child; + list_iterate(&curproc->p_children, child, proc_t, p_child_link) { - if (status != NULL) + if (child->p_state == PROC_DEAD) { - *status = child->p_status; - } + dbg(DBG_PROC, "found a dead thread with pid=%d\n", child->p_pid); + if (status != NULL) + { + *status = child->p_status; + } + int child_pid = child->p_pid; - proc_destroy(child); - return child->p_pid; + + list_remove(&child->p_child_link); + proc_destroy(child); + + dbg(DBG_PROC, "exited child pid=%d\n", child->p_pid); + return child_pid; + } } + + dbg(DBG_PROC, "waiting for any child, calling sched_sleep_on\n"); + sched_sleep_on(&curproc->p_wait); } } @@ -480,6 +487,8 @@ void do_exit(long status) { // NOT_YET_IMPLEMENTED("PROCS: do_exit"); + dbg(DBG_PROC, "proc exiting with pid=%d", curproc->p_pid); + kthread_exit((void *)status); } |