On Mon, 6 Nov 2006, Kasicass wrote:

> 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.

Not _should_, posix is very clear about that. It warns you not to
depend on those semantics. BTW, I think you swapped your results. 

        -Otto
        
> 
> ---------
> #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

Reply via email to