使用代码验证linux子进程与父进程的关系
2014-02-21 16:39
501 查看
******* fork.c *********
#include "basic.h"
int main(){ int pid = Fork(); int x = 2;
if (pid == 0) { printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x); sleep(3); printf("child: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), ++x); exit(0); }
printf("parent: pid = %d, ppid = %d, x = %d\n", getpid(), getppid(), --x);
}
通过 gcc fork.c basic.c -o fork 编译即可的 fork 程序。 运行 ./fork
可以看出父进程首先退出,退出前child的PPID为12256, 退出后子进程的PPID变为了 1.说明父进程退出后的子进程由 init 超级进程1领养。而该进程是不绝不会退出的。
相关文章推荐
- Linux下守护进程(daemon)和管道的结合使用(代码保留)
- linux下使用C或C++实现进程管理代码
- linux下进程-进程组-会话之间的关系-代码说明一切
- Linux中,子进程与父进程的继承关系
- [Linux进程]在父进程和子进程中分别使用execl函数
- [Linux进程]使用vfork创建子进程
- [Linux进程]使用vfork创建子进程并且执行命令
- linux下使用c++创建守护进程
- [Linux进程]在父进程和子进程中分别修改变量
- [Linux进程]使用waitpid函数退出进程
- Linux 下进程与线程的关系
- 使用Shell脚本对Linux系统和进程资源进行监控 http://www.csdn.net/article/2012-06-26/2806898
- linux系统编程之进程(七):system()函数使用
- 使用bootstrap validator的remote验证代码经验分享(推荐)
- Linux寻找和杀掉僵尸进程代码
- Linux c==使用父子进程实现TCP通信(15)
- 使用Shell脚本对Linux系统和进程资源进行监控
- 使用恶意软件将隐藏代码注入已知进程的攻击研究
- linux的父进程向子进程发kill信号例子以及对子进程的状态进行判断
- linux进程通信--管道(pipe)(含实例代码)