Confused with fork & vfork - Linux

This is a discussion on Confused with fork & vfork - Linux ; I have 2 C programs where 1 program uses fork and second uses vfork. The behaviuor of the program has confused me... int a; //uninitialized data segment int main() { pid_t pid; pid = vfork(); if(pid == 0) //child { ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Confused with fork & vfork

  1. Confused with fork & vfork

    I have 2 C programs where 1 program uses fork and second uses vfork. The
    behaviuor of the program has confused me...

    int a; //uninitialized data segment
    int main()
    {
    pid_t pid;

    pid = vfork();

    if(pid == 0) //child
    {
    a = 10;
    printf("\n Child Process a=%d \n",a);
    exit(0);
    }
    else if(pid > 0) //parent
    {
    printf("\n Parent Process a=%d \n",a);
    exit(0);
    }
    else
    {
    printf("\n Could not create child process \n");
    }
    }

    Since vfork is used child executes first and prints 10. But parent process
    also prints 10. Since vfork uses COW and child process is changing value of
    a should a new page be not allocated to the child process and value of
    parent must still be 0?

    When i replace vfork by fork the value of a in parent is 0 & in child is 10.
    Im confused with this. Plz tell me what is happening?

    Regards,
    Samrat




  2. Re: Confused with fork & vfork

    "Samrat Kannikar" writes:

    > I have 2 C programs where 1 program uses fork and second uses vfork. The
    > behaviuor of the program has confused me...
    >
    > int a; //uninitialized data segment
    > int main()
    > {
    > pid_t pid;
    >
    > pid = vfork();
    >
    > if(pid == 0) //child
    > {
    > a = 10;
    > printf("\n Child Process a=%d \n",a);
    > exit(0);
    > }
    > else if(pid > 0) //parent
    > {
    > printf("\n Parent Process a=%d \n",a);
    > exit(0);
    > }
    > else
    > {
    > printf("\n Could not create child process \n");
    > }
    > }
    >
    > Since vfork is used child executes first and prints 10. But parent process
    > also prints 10. Since vfork uses COW and child process is changing value of
    > a should a new page be not allocated to the child process and value of
    > parent must still be 0?


    Some highlights from man vfork

    Standard Description
    (From SUSv2 / POSIX draft.) The vfork() function has the same effect
    as fork(2), except that the behavior is undefined if the process cre-
    ated 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(2) or one of the exec(3) family of
    functions.

    Notice -- behavior is undefined in the circumstance you describe.
    Another snippet from the man page:

    vfork() differs from fork(2) in that the parent is suspended until the
    child makes a call to execve(2) or _exit(2). The child shares all mem-
    ory with its parent, including the stack, until execve(2) is issued by
    the child. The child must not return from the current function or call
    exit(3), but may call _exit(2).

    So the parent and child share their memory. Where did you find
    documentation saying vfork puts pages in COW?

+ Reply to Thread