#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <unistd.h>
#include<errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
// 演示 vfork
// vfork共享父进程的数据段
// vfork函数必须和execle这类函数在一起或者exit
// 不建议使用vfork函数
//execve一个应用把另一个应用拉起来---> vfork的主要用途
int main(void)
{
pid_t pid;
//int num;
printf("before pid : %d\n", getpid());
pid = vfork();
if (-1 == pid)
{
perror("fork err");
return 0;
}
if (pid > 0)
{
printf("parent pid : %d\n", getpid());
}
if (0 == pid)
{
printf("child pid : %d\n", getpid());
//return 0; vfork下不能用return
// hello的代码段/数据段/内存控制块/堆栈段完全覆盖子进程(子进程PID不被覆盖)
execve("./hello", NULL, NULL);
printf("execve没有执行成功\n");
exit(0);
}
return 0;
}
/*
---------------------------------------
运行结果:
before pid : 6985
child pid : 6986
hellowodfs
parent pid : 6985
---------------------------------------
*/