fork :
int gval = 1;
int main(int argc , char ** argv , char ** env)
{
int localvar = 1;
pid_t child = 0;
if((child = fork()) == 0){
++gval;
++localvar;
exit(0);
}
waitpid(child,NULL,0);
printf("gval:%d, localvar : %d\n",gval,localvar); // 1 1 , fork 将复制代码段
return 0;
}
vfork:
int gval = 1;
int main(int argc , char ** argv , char ** env)
{
int localvar = 1;
pid_t child = 0;
if((child = vfork()) == 0){
++gval;
++localvar;
exit(0);
}
waitpid(child,NULL,0);
printf("gval:%d, localvar : %d\n",gval,localvar); // 2 , 2 vfork在exec之前将在父进程中运行
return 0;
}