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);
}
From the specification those two lines in the child's code
provoke undefined behaviuor.
From Linux
man 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):