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

Hi All,
I've been reading the docs and I can't see a way to do this:-

I am forking a process which may or may not return some output on its STDOUT and
I want the parent to wait for upto 3 seconds and then kill the child if there is not output.

Any ideas

Cheers
Cheshire Cat

Replies are listed 'Best First'.
Re: Forking Question
by data64 (Chaplain) on Feb 08, 2002 at 17:57 UTC

    Open a pipe to your child process then using fcntl do non-blocking reads (sysread) from the pipe. Then you can poll for 3 seconds and if nothing is read from the pipe, kill the child process

    .

    Just a tongue-tied, twisted, earth-bound misfit. -- Pink Floyd

Re: Forking Question
by dmmiller2k (Chaplain) on Feb 08, 2002 at 20:46 UTC

    Use IO::Select on a pipe.

    use IO::Select; my $s = new IO::Select(); my $pid = open PH, "arbitrary command|" or die "arbitrary command: $!" +; $s->add( \*PH ); my @ready = $s->can_read( 3000 ); if (@ready == 0) { # nothing to read, kill the child and close the pipe kill( $pid); close PH; } else { while (<PH>) { # do something with the output } }

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime