LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how do I make the children gone with the parents (https://www.linuxquestions.org/questions/programming-9/how-do-i-make-the-children-gone-with-the-parents-216449/)

jpan 08-11-2004 08:22 PM

how do I make the children gone with the parents
 
how to make the child process "die with its parent" when the user
terminate the parent process?

cppkid 08-12-2004 12:31 AM

I think that there can be many senarios, First is that a signal will be generated and all child's will have a signal handler that will kill the childs, If you are talking about threads, then you can send the termenate signal to the child threads, all you have to do is to get their ThreadID, Make it global or just return them to parent thread.

kev82 08-12-2004 06:23 AM

i dont know a specific way to do it(there probably is one though) but some thoughts that come to mind are:

a) in the child process get the process id of parent, if it changes, then parent has been killed, this will only work though if parent is alive when getppid() is called.

b) use pipe() and make each child regularly communicate with the parent, and handle SIGPIPE - should work every case but will quickly open lots and lots of file descriptors

c) method b implemented with any other type of ipc you care to come up with.

d) a combination of method a and b, where you just create the pipe until you know the value returned by getppid() is correct then close it.

bruce ford 08-12-2004 01:42 PM

hi,

it may sound heretic but if it is possible for your application to change the roles of parent and child process the handling would be straight forward (SIGCHILD).

Don't know if that helps but sometimes one needs only a kick to think the other way round... ;)

So long...
bruce

jpan 08-12-2004 02:25 PM

also, if i do

// in the child process
close(STDIN_FILENO);
execl(".....ssh","ssh",.....);


in my code, then after running the program, a message says that
"Pseudo terminal will not be allocated because stdin is not a terminal."

so using close(STDIN_FILENO) before execl() seems not working for my case.

dougpotter 09-18-2004 08:30 PM

You can use prctl() to send the child a SIGTERM when the parent dies.

Code:

int pid;

pid = fork();
if ( pid == 0 ) {
    // This will cause a SIGTERM to be sent to the child if
    // the parent dies for some reason.
    prctl( PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0 );

    execl( ... ); // Run your program here

    perror("FAILED");
    exit(EXIT_FAILURE);
}


foo_bar_foo 09-19-2004 11:40 PM

Quote:

Originally posted by bruce ford
hi,

it may sound heretic but if it is possible for your application to change the roles of parent and child process the handling would be straight forward (SIGCHILD).

Don't know if that helps but sometimes one needs only a kick to think the other way round... ;)

So long...
bruce

this is exactly how to clean up children when they finish so they don't lie around as zombies
while parent is still running
but the original post was

[quote]
"die with its parent" when the user
terminate the parent process
[quote]

when a program exits it's children are inherited automatically by init and init always cleans them up
no need to do nothing (automatic filicide)


All times are GMT -5. The time now is 05:31 AM.