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);
...
withQuote:}
void MainCleanupProcess(void)
{
if (child_pid != 0)
{
/* destroy semaphores */
}
but I can't shake the feeling that there is probably a betterQuote:}
way of doing this. Can anyone shed any light, please?
(Using portable UNIX...)
Thanks in advance,
Chris.