http://blog.csdn.net/kennyrose/article/details/7532758
孤儿进程
在下面的代码执行时,
$ ps -ef |grep test3
harryhare 26918 26335 0 23:08 pts/14 00:00:00 ./test3
harryhare 26919 26918 0 23:08 pts/14 00:00:00 ./test3
harryhare 27055 26430 0 23:10 pts/15 00:00:00 grep --color=auto test
$ pstree
systemd─┬─YDLive───{YDLive}
........此处省略一堆没用的.....
├─sshd─┬─2*[sshd───sshd───bash]
│ ├─sshd───sshd───bash───test3───test3
│ ├─sshd───sshd───bash───pstree
│ └─sshd───sshd
├─systemd───(sd-pam)
├─systemd-journal
├─systemd-logind
├─systemd-udevd
$ kill 26918
# 杀死父进程后,child 的父进程切换为systemd
$ ps -ef |grep test3
harryhare 26919 1 0 23:08 pts/14 00:00:00 ./test3
harryhare 28137 26430 0 23:22 pts/15 00:00:00 grep --color=auto test3
$ pstree
systemd─┬─YDLive───{YDLive}
........此处省略一堆没用的.....
├─sshd─┬─3*[sshd───sshd───bash]
│ └─sshd───sshd───bash───pstree
├─systemd───(sd-pam)
├─systemd-journal
├─systemd-logind
├─systemd-udevd
├─test3
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
void child(){
printf("child...\n");
pid_t pid=getpid();
printf("child: pid = %d\n", pid);
sleep(1000);
}
void parent(){
printf("parent...\n");
pid_t pid=getpid();
printf("parent: pid = %d\n", pid);
sleep(1000);
}
void error(){
perror("fork:");
exit( -1);
}
int main(){
int pid;
pid=fork();
if(pid==0){
child();
}else if(pid>0){
printf("child process id %d\n",pid);
parent();
}else{
error();
}
return 0;
}