• vfork() 使用中父子进程共享虚拟内存空间,fork()则不是
• vfork保证子进程先运行,而fork 的父子进程运行顺序是不定的,它取决于内核的调度算法
• vfork保证子进程先运行,而fork 的父子进程运行顺序是不定的,它取决于内核的调度算法
例子:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
pid = vfork();
if (pid <0)
printf(“vfork failed\n”);
else if (pid ==0) {
sleep(1);
prntf(“hello I am child\n”);
}
else
printf(“hello I am parent\n”);
exit(0);
}
#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);
}