prctl(PR_SET_PDEATHSIG,SIGNAL)在父线程退出时调用,而不是父进程退出 [英] prctl(PR_SET_PDEATHSIG, SIGNAL) is called on parent thread exit, not parent process exit

查看:4606
本文介绍了prctl(PR_SET_PDEATHSIG,SIGNAL)在父线程退出时调用,而不是父进程退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个进程是分叉到子进程。如果父进程存在,则子进程不应存在。所以,我调用:: prctl(PR_SET_PDEATHSIG,SIGKILL)在子进程中杀死它,如果父死亡。最终发生的是父线程调用pthread_exit,并且该线程最终成为杀死子进程的催化剂。

I have a process that is forking to a child process. The child process should not exist if the parent process exists. So, I call ::prctl(PR_SET_PDEATHSIG, SIGKILL) in the child process to kill it if the parent dies. What ends up happening is the parent thread calls pthread_exit, and that thread ends up being the catalyst that kills the child process.

这是我的代码:

parent.cpp:

parent.cpp:

#include <sys/prctl.h>
#include <signal.h>
#include <unistd.h>
#include <pthread.h>
#include <iostream>

void* run(void* ptr) {

    std::cout << "thread:" << getpid() << ":" << std::hex << pthread_self() << ":" << std::dec << getppid() << std::endl;
    auto pid = fork();
    if ( pid != 0 ) {
        sleep(1);
    }
    else {
        char* arg = NULL;
        execv("./child", &arg);
    }
    return NULL;
}

int main() {

    std::cout << "main:" << getpid() << ":" << std::hex << pthread_self() << ":" << std::dec << getppid() << std::endl;

    pthread_t threadid;
    pthread_attr_t attr;

    ::pthread_attr_init( &attr );
    ::pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED );
    ::pthread_create(&threadid,&attr,run,NULL);

    sleep(6);

    return 0;
}

child.cpp:

child.cpp:

#include <sys/prctl.h>
#include <signal.h>
#include <unistd.h>
#include <iostream>

int main() {
    std::cout << "child:" << getpid() << ":" << std::hex << pthread_self() << ":" << std::dec << getppid() << std::endl;
    ::prctl( PR_SET_PDEATHSIG, SIGKILL );
    sleep(6);


    return 0;
}

在命令行上运行以下命令:

Run the following on the command line:

$ ./parent

同时,运行以下命令查找孩子的状态:

At the same time, run the following to find the status of child:

$ for i in {1..10000}; do ps aux | grep child ; sleep .5; done

孩子走了。

http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2。 html 似乎描述了当父进程死掉时,这个调用应该调用SIGKILL,而不是父线程。有没有办法让prctl杀死孩子当父进程死而不是父线程?

The prctl page at http://www.kernel.org/doc/man-pages/online/pages/man2/prctl.2.html seems to describe that this call should call SIGKILL when the parent process dies, not the parent thread. Is there any way to make prctl kill the child when the parent process dies instead of the parent thread?

推荐答案

因为它在父线程死亡时收到 PR_SET_PDEATHSIG 信号。它的意思是,它获得一个信号,当线程创建它死了。所以如果你想让孩子依赖父进程(我假设你的意思是当main函数死亡)fork从你的父进程的主线程执行。如果您在 Linux prct(2)手册页上查找手册页< a>他们明确声明它是创建此进程的线程,将信号传递给调用(在您的情况下是子进程)。

The child process dies because it receives PR_SET_PDEATHSIG signal when the parent thread dies. What it means is that it gets a signal when the thread that created it dies. So if you want the child to depend on the parent process (I assume you mean when the "main" function dies) fork from the main thread of execution of your parent process. If you look up the man pages at Linux prct(2) man page they specifically state that it is the thread which created this process, delivers the signal to the calling (In your case the child) process.

底线:
从主线程执行,如果你想要依赖于父进程的执行。更简单的话,不要创建一个线程来子进程,只需从主线程fork它。

Bottomline: Fork from the main thread of execution if you want it to depend on the parent process's execution. In simpler words, don't create a thread to fork the child process, just fork it from the main thread.

希望这有助于。

这篇关于prctl(PR_SET_PDEATHSIG,SIGNAL)在父线程退出时调用,而不是父进程退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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