我正在读《Linux编程接口》一书,读到了这句话(在第6.2章的末尾):
如果子进程因为其"出生"父进程终止而成为孤儿,则该子进程由init进程收养,并且对子进程中的getppid()的后续调用返回1.
所以我写了一些代码来查看这一点.以下是代码:
#include <unistd.h>
#include <stdio.h>
int main()
{
printf("starting...\n");
if (fork() == 0)
{
printf("Child PID: %d\n",getpid());
printf("Child PPID: %d\n",getppid());
printf("sleeping till the parent die:(\n");
sleep(5);
printf("Child new PPID: %d\n",getppid());
}
else
{
printf("Parent PID: %d\n",getpid());
}
return 0;
}
运行代码后,我得到了以下输出:
starting...
Parent PID: 16577
Child PID: 16578
Child PPID: 16577
sleeping till the parent die:(
Child new PPID: 2754
不仅getppid()
没有返回1,而且看起来PID1甚至不是init
进程,而是systemd
进程.它们有好几个:当我运行pgrep systemd
时,它输出1和2754.
现在我很困惑,以下是我的问题:
-
init
%的流程在哪里? - 为什么会有多个
systemd
进程?