您的位置:首页 > 其它

[学习笔记]Vfork深入理解

2015-04-15 15:15 344 查看
#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
---------------------------------------
*/


复制去Google翻译翻译结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: