OSEC

Neohapsis is currently accepting applications for employment. For more information, please visit our website www.neohapsis.com or email hr@neohapsis.com
 
[3.9] About vfork~

From: Kasicass (kasicassgmail.com)
Date: Mon Nov 06 2006 - 02:53:37 CST


Hi all,

I just run the following program in obsd 3.9, but it doesn't work as
expected. As said that child process created by vfork should run in the
address space of the parent, until it calls exec/exit.

---------
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int glob = 6; /* external variable in initialized data */

int
main(void)
{
        int var; /* automatic variable on the stack */
        pid_t pid;

        var = 88;
        printf("before fork\n"); /* we don't flush stdout */

        if ( (pid = vfork()) < 0 )
                ;
        else if (pid == 0)
        { /* child */
                glob++; /* modify variables */
                var++;
                _exit(0); /* child terminates */
        }

        printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
        exit(0);
}
---------
In obsd, result is

before fork
pid = xxx, glob = 7, var = 89

But in Linux 2.6/FreeBSD 5.4-RELEASE, it works fine. The result is

before fork
pid = xxx, glob = 7, var = 89
---------

I've not much time to dig into the kernel code, could anyone give me a
brief description about why it runs like that.

Thx a lot.

--
Best regards#!

Kasicass/sYcini - Coder
http://www.sycini.com