我们都知道在Linux上孤儿进程和僵尸进程都会被进程号为1的init进程收养,收尸,但这在使用Systemd来管理系统的发行版上比如Ubuntu上就不是那么靠谱了
首先我们写一个简单的孤儿进程的例子
#include
#include
#include
int
main(void)
{
switch(fork()){
case 0:
printf("Child process\n");
printf("child process: %d\n", getpid());
printf("father pid:%d\n", getppid());
sleep(5);
printf("\nnow pid: %d\t ppid:%d \n",getpid(),getppid());
break;
case -1:
printf("Fork failed");
exit(-1);
default:
printf("Father Process\n");
sleep(1);
printf("Father exit\n");
break;
}
return 0;
}
首先我们在图像界面上伪终端中运行
发现孤儿进程会被进程号为1391的systemd进程收养,而不是pid为1的systemd进程
然后我们在终端中运行该例子,发现成功父进程成功变为1
很显然问题应该时出在systemd上面,查看源码可以发现在systemd中调用了prctl()系统调用
systemd源码src/core/main.c
现在看一下Linux中特有的系统调用prctl( )
#include
int prctl(int option, unsigned long arg2, unsigned long arg3,
unsigned long arg4, unsigned long arg5);
option参数指示prctl如何操作进程
PR_SET_CHILD_SUBREAPER为内核3.4版本中新增的选项
第二个参数若为非零时,调用进程设置'child subreaper'属性
第二个参数若为零则取消该属性
详细参数可以查看The Linux man-pages project
孤儿进程成会被祖先中距离最近的supreaper进程收养
现在我们使用自己的进程来收养他后代的孤儿进程
#include
#include
#include
#include
void descendant_process(void);
int
main(void)
{
prctl(PR_SET_CHILD_SUBREAPER,1);
//设置的child subreaper不会被继承
switch(fork()){
case 0:
descendant_process();
break;
case -1:
printf("Fork failed");
exit(-1);
default:
printf("Subreaper Process:%d\n",getpid());
sleep(1);
for(;;);
}
return 0;
}
void
descendant_process(void)
{
switch(fork()){
case 0:
printf("Child process\n");
printf("child process: %d\n", getpid());
printf("father pid:%d\n", getppid());
sleep(5);
printf("now pid: %d\t ppid:%d \n",getpid(),getppid());
break;
case -1:
printf("Fork failed");
exit(-1);
default:
printf("Father Process\n");
sleep(1);
printf("Father exit\n");
break;
}
return;
}
成功收养~