Cleaning up after your children

josh hoblitt jhoblitt at
Mon Oct 15 21:02:58 CDT 2001

I'm working on a small preforking (like apache) webserver. I'd like to
have the children pass information about their present status to the
parent.  After having trouble getting IO::Select to work correctly I
posted to begginers at perl.  With some help I'v gotten it atleast somewhat
working with examples from the perl cookbook and perlipc thrown
together.  I'm still having two difficulties... one the only way I can
seem to avoid having some defunct children is to set $SIG{CHLD} =
'IGNORE'.  I do not want to do this because I'm dependant on the signal
to know when to fork a replacement child.  Is there someway to guarentee
trapping signals and they won't be ignored while dealing with a prevous
signal.  And two the IO::Select method can_read never seems to block
even after all the children have died and closed they're end of the pipe
it stills returns a list of all the added handles.  To recreate the
defunct children just swap the sig chld handlers in this code blurb.

#!/usr/bin/perl -w

use IO::Handle;
use IO::Select;
use POSIX ":sys_wait_h";
use strict;

our $select = IO::Select->new();

sub REAPER {
	my $pid = wait;
	print "$pid died\n";


for (1 .. 30) {

while (1) {
	my @tmp = $select->can_read;
	print @tmp;
	my $line;
	foreach my $key (@tmp) {
		if (defined($line = <$key>)) {
			print $line;
		} else {
	#		close ($key);

sub make_new_child {
	my $pid;
	my $sigset;

	my $parent_rdr = new IO::Handle;
	my $child_wtr = new IO::Handle;

	pipe($parent_rdr, $child_wtr);

	if (!defined ($pid = fork)) {
		die "Unable to fork: $!\n";

	if ($pid) {
#		waitpid( -1, &WNOHANG );
	} else {
		print $child_wtr "this is a test\n";
		sleep 2;
		print $child_wtr "this is a test too\n";
		sleep 2;
		print $child_wtr "";
		close ($child_wtr);


-Joshua Hoblitt

jhoblitt at


More information about the Pdx-pm-list mailing list