#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
vfork和fork的区别:
1.vfork创建的子进程和父进程共享线性地址空间;fork创建的子进程具有独立的线性地址空间
2.vfork先执行子进程;fork子进程和父进程的执行顺序是随机的
3.vfork创建的子进程中只有在调用exit()或者execve()函数之后,父进程才可能被成功调用
*/
int main(void)
{
pid_t fpid;
int count = 0;
fpid = vfork();
if (fpid == -1) {
perror("MSG");
}
else if (fpid > 0) {
printf("parrent: %d\n", getpid());
count++;
printf("count: %d\n", count)
}
else if (fpid == 0) {
printf("child: %d\n", getpid());
count++;
printf("count: %d\n", count);
exit(0);
}
return 0;
}
/*
1.执行结果:
child: 13729
count: 1
parrent: 13728
count: 2
2.如果子进程没有调用exit(), 执行结果为:
child: 13729
count: 1
parrent: 13728
count: 1
重复上面的打印结果
*/
vfork函数
最新推荐文章于 2025-02-18 09:00:00 发布