Killing the children with the parent

后端 未结 7 1535
孤城傲影
孤城傲影 2020-12-08 11:36

I have a program spawning and communicating with CPU heavy, unstable processes, not created by me. If my app crashes or is killed by SIGKILL, I want the subproc

7条回答
  •  孤城傲影
    2020-12-08 12:24

    I know it's been years, but I found a simple (slightly hacky) solution to this problem. From your parent process, wrapping all your calls around a very simple C program that calls prctl() and then exec() solves this problem on Linux. I call it "yeshup":

    #include 
    #include 
    #include 
    
    int main(int argc, char **argv) {
         if(argc < 2)
              return 1;
         prctl(PR_SET_PDEATHSIG, SIGHUP, 0, 0, 0);
         return execvp(argv[1], &argv[1]);
    }
    

    When spawning your child processes from Python (or any other language), you can run "yeshup gnuchess [argments]." You'll find that, when the parent process is killed, all your child processes (should) be given SIGHUP nicely.

    This works because Linux will honor the call to prctl (not clear it) even after execvp is called (which effectively "transforms" the yeshup process into a gnuchess process, or whatever command you specify there), unlike fork().

提交回复
热议问题