Terminating parent vs terminating children

Terminating parent vs terminating children

Post by Chris Ranki » Tue, 19 Nov 1996 04:00:00



Hello all,

Consider the following situation:

A parent process allocates a set of semaphores. It then begins to
spawn children (which use the semaphores). The semaphores are
a global resource and so need to be explicitly destroyed; however
if a *child* process destroys them accidentally then things could
get *. So the question becomes: where is the best place to
clean up?

There seem to be two places to put cleanup code: in exit
procedures, which are installed using atexit(), and in signal
functions. In the code that I am patching, the signal functions
for SIGTERM, SIGTRAP, SIGQUIT and SIGINT etc all call
exit() and so atexit() would seem like a good idea. However,
I think I am correct in saying that both signal handlers and
exit procedures are inherited by child processes.

I *could* do the following:

int child_pid = -1;
atexit(MainCleanupProcess);
...
child_pid = fork();
if (child_pid==0)
{
  atexit(CleanupChildProcess);
  ...

Quote:}

with

void MainCleanupProcess(void)
{
  if (child_pid != 0)
  {
    /* destroy semaphores */
  }

Quote:}

but I can't shake the feeling that there is probably a better
way of doing this. Can anyone shed any light, please?
(Using portable UNIX...)

Thanks in advance,
Chris.

 
 
 

Terminating parent vs terminating children

Post by Matna M Bala » Wed, 20 Nov 1996 04:00:00



: Hello all,

: Consider the following situation:

: A parent process allocates a set of semaphores. It then begins to
: spawn children (which use the semaphores). The semaphores are
: a global resource and so need to be explicitly destroyed; however
: if a *child* process destroys them accidentally then things could
: get *. So the question becomes: where is the best place to
: clean up?

Why don't you just destroy them in the parent after you have waited for
the children?

G.

 
 
 

Terminating parent vs terminating children

Post by Chris Ranki » Thu, 21 Nov 1996 04:00:00





> : A parent process allocates a set of semaphores. It then begins to
> : spawn children (which use the semaphores). The semaphores are
> : a global resource and so need to be explicitly destroyed; however
> : if a *child* process destroys them accidentally then things could
> : get *. So the question becomes: where is the best place to
> : clean up?

> Why don't you just destroy them in the parent after you have waited for
> the children?

This is what I do, but that's not the point. I was asking for the official/
orthodox method (as compared to what I posted) for separating the
parent's cleanup from a child's cleanup, since fork()-ing makes a child
inherit the same signal handlers and exit procedures as the parent.

In other words: my "wait for the children to terminate" code (and all of
the other cleanup clauses with it) automatically becomes part of any
child process's cleanup too, unless I use some hack that probably
involves a race condition.

Chris.

 
 
 

Terminating parent vs terminating children

Post by Andrew Gier » Fri, 22 Nov 1996 04:00:00


 Chris> [...] I was asking for the official/ orthodox method (as
 Chris> compared to what I posted) for separating the parent's cleanup
 Chris> from a child's cleanup, since fork()-ing makes a child inherit
 Chris> the same signal handlers and exit procedures as the parent.

Personally I've done this by checking in the cleanup code that
getpid() == creator_pid. I'm not aware of any more 'orthodox' method.

--

"Ceterum censeo Microsoftam delendam esse" - Alain Knaff in nanam