在 UNIX 中,当父进程消失时,我认为所有子进程都将 init 重置为其父进程。这不是一直正确吗?有任何例外吗?
我的代码正在分叉一个进程并打印每个进程的 PID 和 PPID。我期待孩子的 PPID 与父母的 PID 相同,但事实并非如此。
我正在使用 Ubuntu 14.04。
#include <stdio.h>
#include <sys/wait.h>
int main(){
int pid;
pid = fork();
if(pid==0){
printf("\nI am the child and my parent id is %d and my id %d\n", getppid(), getpid());
}
else
printf("\nI am the parent and my pid is %d and my parent id is %d\n", getpid(), getppid());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
I am the parent and my pid is 29229 and my parent id is 27087
I am the …
Run Code Online (Sandbox Code Playgroud)