vfork和fork区别:
1.vfork直接使用父进程的存储空间,不拷贝
2.vfork保证子进程先运行,当子进程调用exit退出后,父进程才执行
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
printf(“father pid is %d\n”,getpid());
int cnt = 0;
pid = vfork();
if(pid>0){
while(1){
printf("this is father pid:%d\n",getpid());
sleep(1);
}
}else if(pid == 0){
while(1){
printf("this id child pid:%d\n",getpid());
sleep(1);
cnt++;
if(cnt==3){
exit(0);
}
}
}
return 0;
}
运行结果为: