cforkvfork

why vfork() causes the parent process crash(segment fault)?


The vfork can change variables in parent process, but why can't it increase the stack?

void f1()
{
    vfork();
}

f2() leads to the crash.

void f2()
{
    char buf[100];
}


int main()
{
    f1();
    f2();
    _exit(0);                                                                                                                                    
}

If I change vfork() to fork(), the crash won't happen.


Solution

  • The only thing you're allowed to do after calling vfork() is execute a file. It's right in the documentation:

    The vfork() function shall be equivalent to fork(), except that the behavior is undefined if the process created by vfork() either modifies any data other than a variable of type pid_t used to store the return value from vfork(), or returns from the function in which vfork() was called, or calls any other function before successfully calling _exit() or one of the exec family of functions.

    ... > The use of vfork() for any purpose except as a prelude to an immediate call to a function from the exec family, or to _exit(), is not advised.

    To wit, the only legal calls are _exit and exec*.