SPUG: Dying Children

Jim Ludwig jsl at blarg.net
Mon Sep 20 14:26:17 CDT 2004


I assume you're keeping track of your child
processes with a hash or something:

    my %file_writers = ();

How is your child dying?  Along the lines of the
following?:

    $SIG{'CHLD'} = \&file_writer_died;

    # ...

    my $pid = fork();
    if ( $pid ) {
        # parent
        $file_writers{$pid} = 1;  # or whatever
    }
    else {
        # child writes or whatever
    }

    # ...

    sub file_writer_died {
        local( $SIG{'CHLD'} ) = \&file_writer_died;
        my $pid = wait();
        delete( $file_writers{$pid} );
    }

Or maybe your STDOUT output is buffered, and you
want to force a flush immediately after every
write/print?

    select(( select( STDOUT ), $| = 1 )[0] );

Hope that's useful,
  jim


>> Date: Mon, 20 Sep 2004 08:56:33 -0700 (PDT)
>> From: Dan Ebert <mathin at mathin.com>
>> To: <spug-list at mail.pm.org>
>> Subject: SPUG: Dying Children
>> 
>> I have a script which forks of a child process
>> to write a file while the parent script goes on
>> to collect the data for the next file.  I'm
>> keeping track of the number of file writing
>> children so I don't have too many writers at
>> one time.  I have a debug print statement right
>> before the exit in the child process:
>> 
>> print "done writing file\n";
>> exit;
>> 
>> I am noticing that there are sometimes several
>> seconds between the print and the actual death
>> of the child.  This is delaying the forking of
>> the next child which is slowing the script
>> down.  Has anyone seen this before?
>> 
>> Thanks,
>> 
>> Dan.


More information about the spug-list mailing list