[solved]do_exit function schedule will return

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
wellwell
Posts: 8
Joined: Wed Jul 24, 2019 6:26 am

[solved]do_exit function schedule will return

Post 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);
Last edited by wellwell on Sat Nov 02, 2019 5:26 am, edited 1 time in total.
Octocontrabass
Member
Member
Posts: 5580
Joined: Mon Mar 25, 2013 7:01 pm

Re: do_exit function schedule will return

Post by Octocontrabass »

The schedule() function returns when the process has been scheduled. A process that has exited should not be scheduled.
wellwell
Posts: 8
Joined: Wed Jul 24, 2019 6:26 am

Re: do_exit function schedule will return

Post 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.
User avatar
iansjack
Member
Member
Posts: 4705
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: do_exit function schedule will return

Post 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?)
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: do_exit function schedule will return

Post 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.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
User avatar
iansjack
Member
Member
Posts: 4705
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: do_exit function schedule will return

Post by iansjack »

Isn't that what

Code: Select all

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