CHILD_SUBREAPER位是否在fork()中持续存在? [英] Does the CHILD_SUBREAPER bit persist across fork()?

查看:64
本文介绍了CHILD_SUBREAPER位是否在fork()中持续存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当进程使用 prctl(PR_SET_CHILD_SUBREAPER,1)设置子子收割者位时(解决方案

不,子级次要位不会在分支之间持久.

相关的Linux内核代码位于 kernel/fork.c 中的copy_signal():信号结构初始化为全零,并且永远不会设置 is_child_subreaper 位./p>

但是,已设置 has_child_subreaper :

  sig-> has_child_subreaper =当前->信号-> has_child_subreaper ||current->信号-> is_child_subreaper; 

此测试程序演示了该行为:

  #include< stdio.h>#include< stdlib.h>#include< sys/prctl.h>int main(int argc,char ** argv){int pid;我prctl(PR_SET_CHILD_SUBREAPER,1);prctl(PR_GET_CHILD_SUBREAPER,& i);printf("fork之前:%d \ n",i);pid = fork();如果(pid< 0){返回1;}否则,如果(pid == 0){prctl(PR_GET_CHILD_SUBREAPER,& i);printf(在子项中:%d \ n",i);返回0;}返回0;} 

输出:

 在fork之前:1在儿童中:0 

When a process sets the child subreaper bit with prctl(PR_SET_CHILD_SUBREAPER, 1) (documented here), does it need to use prctl(PR_SET_CHILD_SUBREAPER, 0) to clear it after a fork?

解决方案

No, the child subreaper bit does not persist across forks.

The relevant Linux kernel code is in copy_signal() in kernel/fork.c: the signal struct is initialized to all zeros, and the is_child_subreaper bit is never set.

However, has_child_subreaper is set:

 sig->has_child_subreaper = current->signal->has_child_subreaper ||                  
                            current->signal->is_child_subreaper;

This test program demonstrates the behavior:

#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>

int main(int argc, char** argv) {

        int pid;
        int i;

        prctl(PR_SET_CHILD_SUBREAPER, 1);

        prctl(PR_GET_CHILD_SUBREAPER, &i);
        printf("Before fork: %d\n", i);

        pid = fork();
        if (pid < 0) {
                return 1;
        } else if (pid == 0) {
                prctl(PR_GET_CHILD_SUBREAPER, &i);
                printf("In child: %d\n", i);
                return 0;
        }
        return 0;
}

Outputs:

Before fork: 1
In child: 0

这篇关于CHILD_SUBREAPER位是否在fork()中持续存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆