clinuxgccvfork

vfork() with gcc -O2 seems to have a wrong result.


I took the code from APUE. In Ubuntu 12.04 if I use gcc without any optimization, I will get the results same as the book's:

pid = 4122, glob = 7, var = 89.

If I use gcc -O2, then the var will be 88. Is this because the gcc optimization will do something with vfork()?

#include "apue.h"

int glob = 6;

int
main(void)
{
  int var;
  pid_t pid;
  var = 88;
  printf("before vfork\n");
  if ((pid = vfork()) < 0) {
    err_sys("vfork error");
  } else if ( pid ==0) {
    glob++;
    var++;
    _exit(0);
  }

  printf("pid = %d, glob = %d, var = %d\n", getpid(), glob, var);
  exit(0);
}

Solution

  • From the specification those two lines in the child's code

        glob++;
        var++;
    

    provoke undefined behaviuor.

    From Linux man vfork:

    (From POSIX.1) [...] 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() [...]

    It might be worth noting that vfork() was "marked obsolescent" in the previous version of POSIX (see POSIX link above) and was removed from POSIX with Issue 7 (POSIX.1-2008):

    Applications are recommended to use the fork() function instead of this function.

    The vfork() function was previously under-specified.