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? |
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.
|
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. |
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 |
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. |
You can use prctl() to send the child a SIGTERM when the parent dies.
Code:
int pid; |
Quote:
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. |