hubb0r has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing around with SGI::FAM.pm for the last few days for a project of mine. I've decided that I want to fork off subprocesses of my main program to deal with the files that SGI::FAM reports.

This is all works great when I set:
$SIG{CHLD} = IGNORE;
but when I set:

$SIG{CHLD} = \&reap_the_children; sub reap_the_children { my $child; while (($child = waitpid(-1, &WNOHANG)) > 0) { my $file = $seen{$child}; print "Deleting $file from seen hash\n"; print "Deleting $child from seen hash\n"; delete $seen{$file}; delete $seen{$child}; } $SIG{CHLD} = \&reap_the_children; }
Here is my output:

Forking child from 18263 (parent process)
Forked 19241 (child process)
19241:pureftpd.vevo3w (child does stuff here and then exits)
Deleting pureftpd.vevo3w from seen hash (cleanup here)
Deleting 19241 from seen hash (cleanup here)
SGI::FAM: No child processes at ./watcher.pl line 30 (program dies here)

When I set $SIG{CHLD} to ignore, the program runs indefinitely doing what it does. I really think I need the ability to do some extra things in the reaping of the children, but it seems to be interfering with the normal operation of SGI::FAM.pm.

Can anyone help??
Thanks alot.

Replies are listed 'Best First'.
Re: Fork and Reap
by hubb0r (Pilgrim) on Jul 31, 2004 at 16:11 UTC
    Just wanted to clarify, SGI::FAM probably does some forking itself, and what I imagine is happening is that my sub for dealing with my children is killing off the SGI::FAM children as well. Is there any way to limit the scope of my sub to my main program, but not the modules that I'm using? Or would I be better off keeping track of the children I've spawned and attempting only to catch them? Not sure of how to do this since I think I still need to trap $SIG{CHLD} and do something with it, which still would override the SGI::FAM method for dealing with it's children. Thanks again.