Linux系统编程——进程中vfork函数
- 开源代码
- 2025-08-12 20:57:02

函数原型 pid_t vfork(void);//pid_t是无符号整型 所需头文件 #include <sys/types.h> #include <unistd.h> 功能
vfork() 函数和 fork() 函数一样都是在已有的进程中创建一个新的进程,但它们创建的子进程是有区别的。
返回值 成功子进程中返回 0,父进程中返回子进程 ID失败返回 -1 vfork与fork的区别关键区别一:
fork执行时无先后顺序,父进程与子进程会争夺执行
vfork保证子进程先运行,当子进程调用exit退出后,父进程才执行
代码验证
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { int fork_t = 0; fork_t = fork(); if(fork_t > 0) { while(1) { printf("This is father\n"); sleep(1); } } else if(fork_t == 0) { while(1) { printf("This is child\n"); sleep(1); } } return 0; } #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main() { int vfork_t = 0; int count = 0; vfork_t = vfork(); if(vfork_t > 0) { while(1) { printf("This is father\n"); sleep(1); } } else if(vfork_t == 0) { while(1) { printf("This is child\n"); sleep(1); count++; if(count >= 3) { exit(-1);//输出三次子进程,之后退出 } } } return 0; }第一部分代码可见fork函数中的父进程和子进程会争夺输出,而第二部分的vfork函数会在子进程输出3次退出之后再执行父进程。
关键区别二:
fork中子进程会拷贝父进程的所有数据,子进程是父进程的地址空间
vfork中子进程共享父进程的地址空间
代码验证
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { int fork_t = 0; int a = 10; fork_t = fork(); if(fork_t != 0) { printf("This is father,a = %d\n",a); } else { printf("This is child,a = %d\n",a); } return 0; } #include <stdio.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> int main() { int vfork_t = 0; int count = 0; vfork_t = vfork(); if(vfork_t > 0) { while(1) { printf("count = %d\n",count); printf("This is father\n"); sleep(1); } } else if(vfork_t == 0) { while(1) { printf("This is child\n"); sleep(1); count++; if(count >= 3) { exit(0); } } } return 0; }第一部分代码可知,在父进程中定义a,调用fork函数时,父进程与子进程打印a的值一样,说明子进程会拷贝父进程的所有数据(父进程的只打印自己的值,不会收子进程影响);第二部分代码可知,在子进程结束之后,才会执行父进程,且子进程中数值发生改变,在父进程调用时会发生改变(一开始父进程a=0,调用后a=3),会受到子进程影响
Linux系统编程——进程中vfork函数由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Linux系统编程——进程中vfork函数”