UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Processes
  • » Guaranteed auto-cleanup of grandchild processes?

#1 2008-01-24 11:36 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Guaranteed auto-cleanup of grandchild processes?

Hi all,

I have a small problem... my code launches a child process via fork() and execvp(), and that child process (which I don't have control over the source code to) may or may not launch some of its own child processes, and those might or might not launch their own child processes, and so on.

The problem is this:  in the event that my code's child process is killed (more or less violently, via SIGHUP/SIGINT/SIGTERM/SIGQUIT/whatever), I want to guarantee that none of the processes it spawned (directly or indirectly) are left hanging around. 

Is there a way to ensure this?  I don't think that just installing signal handlers would be sufficient, since (a) I would need access to the source code of the child processes to do that, and (b) there's no guarantee that they would get called in all cases, since e.g. SIGKILL cannot be handled by non-init processes.

Do I need to declare a "process group" to do this?  If so, how does that work?

Thanks,
Jeremy

Offline

#2 2008-01-25 01:54 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: Guaranteed auto-cleanup of grandchild processes?

If a process's parent gets killed, the process will be adopted by init...  So, if you're
just worried about them zombifying later when they exit, no need to worry, because
init will reap them...  However, if you want to make sure the children die when their
parent dies, then yes you want a process group/session, and make their parent (your
direct child) the pgroup leader...  In that case, when the pgroup leader dies, all others
in the pgroup will get hit with SIGHUP...  Of course, if you have no control over the
code of those (grand)children, you have no way to make sure they don't block/ignore
SIGHUP, so that's really no guarantee of taking them down when the leader goes...
But, I think it's about the best you can really do...

Anyway, you want to just do a setsid() in your child before exec*()'ing...  That'll give
you a brand new session and process group, with you being the leader...  That'll be
inhereted by the exec*()'d process, so it'll become leader of the pgroup...

Offline

#3 2008-01-25 06:53 PM

jfriesne
Administrator
From: California
Registered: 2005-07-06
Posts: 348
Website

Re: Guaranteed auto-cleanup of grandchild processes?

Thanks Rob, calling setsid() in the child process before calling execvp() did the trick.  Well, that plus killing the child process with kill(-pid) instead of kill(pid), so that it's the entire process group that gets killed instead of just that process... in any case, it's working now and I'm happy :^)

-Jeremy

Offline

#4 2015-01-13 10:10 AM

newbie
Guest

Re: Guaranteed auto-cleanup of grandchild processes?

jfriesne,,,,,, can you please post the code which you finally obtained after modification??

  • Index
  • » Processes
  • » Guaranteed auto-cleanup of grandchild processes?

Board footer

Powered by FluxBB