Page 1 of 1

[solved]do_exit function schedule will return

Posted: Fri Nov 01, 2019 11:36 pm
by wellwell
I am working with the do_exit() function,

Code: Select all

int
do_exit(int error_code) {
    if (current == idleproc) {
        panic("idleproc exit.\n");
    }
    if (current == initproc) {
        panic("initproc exit.\n");
    }
    
    struct mm_struct *mm = current->mm;
    if (mm != NULL) {
        lcr3(boot_cr3);
        if (mm_count_dec(mm) == 0) {
            exit_mmap(mm);
            put_pgdir(mm);
            mm_destroy(mm);
        }
        current->mm = NULL;
    }
    current->state = PROC_ZOMBIE;
    current->exit_code = error_code;
    
    bool intr_flag;
    struct proc_struct *proc;
    local_intr_save(intr_flag);
    {
        proc = current->parent;
        if (proc->wait_state == WT_CHILD) {
            wakeup_proc(proc);
        }
        while (current->cptr != NULL) {
            proc = current->cptr;
            current->cptr = proc->optr;
    
            proc->yptr = NULL;
            if ((proc->optr = initproc->cptr) != NULL) {
                initproc->cptr->yptr = proc;
            }
            proc->parent = initproc;
            initproc->cptr = proc;
            if (proc->state == PROC_ZOMBIE) {
                if (initproc->wait_state == WT_CHILD) {
                    wakeup_proc(initproc);
                }
            }
        }
    }
    local_intr_restore(intr_flag);
    
    schedule();
    panic("do_exit will not return!! %d.\n", current->pid);
}
My question is why the schedule function will not return?????
It always returned in my do_exit function, and prints the panic line. so what's up????

Many thanks, I solved this problem by using switch_to directly.
switch_to(&_current->context,&current->context);

Re: do_exit function schedule will return

Posted: Sat Nov 02, 2019 1:09 am
by Octocontrabass
The schedule() function returns when the process has been scheduled. A process that has exited should not be scheduled.

Re: do_exit function schedule will return

Posted: Sat Nov 02, 2019 1:36 am
by wellwell
Octocontrabass wrote:The schedule() function returns when the process has been scheduled. A process that has exited should not be scheduled.
Thanks for your reply. I do not known how the schedule works within the exit function.
the panic says it will not return, but in my program it always returns at the time that I want it exit.

Re: do_exit function schedule will return

Posted: Sat Nov 02, 2019 2:33 am
by iansjack
Then there is something wrong with your schedule function. (Is it your function or are you cut and pasting someone else's code?)

Re: do_exit function schedule will return

Posted: Sat Nov 02, 2019 3:04 am
by Octacone
You need to make sure that once the thread exits, it gets removed from the running queue or else it will just be rescheduled.
Once removed from the running queue, place it on some sort of a "dead queue" and have a special thread that picks up the dead threads and removes their metadata.

Re: do_exit function schedule will return

Posted: Sat Nov 02, 2019 3:11 am
by iansjack
Isn't that what

Code: Select all

current->state = PROC_ZOMBIE
does? (Providing the unknown schedule() function handles the state correctly.)