LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2015, 04:02 PM   #1
olga_chotinun
LQ Newbie
 
Registered: Feb 2015
Location: SF Bay Area
Distribution: Scientific Linux 2.6.32
Posts: 14

Rep: Reputation: Disabled
making child process die on SIGKILL to parent


Hi!
I am trying to ensure child processes exit when parent is killed. I have registered signal handler for all the "bad" signals to ensure clean exit, but I also want parent to kill children when it gets SIGKILL.

I have tried the following:
  1. polling for getppid() == 1 on child, then exit
  2. prctl(PR_SET_PDEATHSIG, SIGTERM);
  3. setpgid() for child to join parent's process group, called in child process after fork() before exec()

Still, when I kill my parent process with -9, the child processes keep running.

I would appreciate any suggestions.
Thank you!
Olga

Last edited by olga_chotinun; 02-26-2015 at 05:38 PM.
 
Old 02-26-2015, 04:30 PM   #2
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
from prctl man page
Code:
       PR_SET_PDEATHSIG (since Linux 2.1.57)
              Set the parent process death signal of the calling process to arg2 (either a signal value in the range
              1..maxsig, or 0 to clear).  This is the signal that the calling process will get when its parent dies.
              This value is cleared for the child of a fork(2) and (since Linux 2.4.36 / 2.6.23)  when  executing  a
              set-user-ID or set-group-ID binary.  This value is preserved across execve(2).

       PR_GET_PDEATHSIG (since Linux 2.3.15)
              Return  the  current  value  of the parent process death signal, in the location pointed to by (int *)
              arg2.
linux only
 
Old 02-26-2015, 04:34 PM   #3
olga_chotinun
LQ Newbie
 
Registered: Feb 2015
Location: SF Bay Area
Distribution: Scientific Linux 2.6.32
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thank You! This is the first thing I tried actually, doesn't work for me, I will try to debug.
Thank You!
Olga
 
Old 02-26-2015, 05:11 PM   #4
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
np, np

from what the man page says arg2 to prctl's PR_GET_PDEATHSIG is a pointer to that (childs, in this case) memory
so you would have to periodically check if the value of that int changed

something like (never tried)

int asd = 0;
prctl(PR_GET_PDEATHSIG, &asd);

then check every once in a while if that int holds a SIG (non-null) value


PS thank YOU, didn't know about this call and it seems like fun
 
Old 02-26-2015, 05:16 PM   #5
olga_chotinun
LQ Newbie
 
Registered: Feb 2015
Location: SF Bay Area
Distribution: Scientific Linux 2.6.32
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks! I am very frustrated because I think PR_SET_PDEATHSIG() alone should have worked for me when I call it on the child! I will try polling and report here on my success/failure
 
Old 02-26-2015, 07:02 PM   #6
olga_chotinun
LQ Newbie
 
Registered: Feb 2015
Location: SF Bay Area
Distribution: Scientific Linux 2.6.32
Posts: 14

Original Poster
Rep: Reputation: Disabled
Allright
prctl(PR_SET_PDEATHSIG, SIGKILL); alone works if I put it right after fork() in child process, and before exec()

Code:
    pid_t cpid = fork();
    if (cpid == 0) {
        ...
        // When parent exists, send SIGKILL to all children
        prctl(PR_SET_PDEATHSIG, SIGKILL);

        // Pass argv[0], arg1, arg2, ..., NULL  to execl()
        int execReturn = execl(_...);
        ...
    }
}
 
Old 02-27-2015, 09:29 AM   #7
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
good that you didn't follow my example, i misunderstood a thing

glad it works but it should work even after exec
i didn't play with this, but that sounds weird
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
killng parent process without killing child process (Linux C programming) lettuce84@naver.com Linux - Newbie 3 07-24-2015 08:37 AM
Forked Child Process Inherits Library Linkage from Parent Process (All Unix Flavors) Embedded_guy Linux - General 3 11-14-2012 08:12 AM
Sending Signal from Child Process to Parent Process : Not getting desired output thelink123 Linux - General 4 10-26-2012 09:05 PM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration