ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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);
}
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.