• vfork保证子进程先运行 ,而fork的父子进程运行顺序是不定的,它取决于内核的调度算法
example:
(1)
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{ int i=0;
pid_t pid;
pid = vfork();
if (pid <0)
printf(“vfork failed\n”);
else if (pid ==0) {
sleep(1);
i++;
printf(“hello I am child,%d\n”,i); }
else
printf(“hello I am parent,%d\n”,i);
exit(0);
}
运行结果:
hello I am child,1
hello I am parent,1
把红色字体的换成 pid = fork();
则运行结果为:
hello I am parent,0
hello I am child,1