Just for the record, a "unixish" implementation of BrowserUk's idea could look something like this:

#!/usr/bin/perl -slw use strict; our $N //= 12; our $I //= 0; my $cmd = qq[exec $^X -E'sleep $N; \$_=<STDIN> if $I; sleep 2; printf +"Kid done (read %d bytes)\\n", length']; my $inInputState = 1; my $timedOut = 0; $SIG{PIPE} = sub { $inInputState = 0; }; $SIG{ALRM} = sub { $inInputState = 0; $timedOut = 1; die }; $SIG{CHLD} = 'IGNORE'; my $pid = open CMD, '|-', $cmd or die $!; alarm 10; eval { syswrite CMD, " \b"x(2**15+1) }; alarm 0; if( $timedOut ) { print "Command timed out"; kill 15, $pid; } if( $inInputState ) { print "Child waiting for input"; } else { print "Kid never entered input state"; } print 'Parent done'; __END__ $ ./detectChildInputState -I=0 -N=2 Kid done (read 0 bytes) Kid never entered input state Parent done $ ./detectChildInputState -I=1 -N=2 Child waiting for input Parent done $ Kid done (read 65538 bytes) $ ./detectChildInputState -I=1 -N=12 Command timed out Kid never entered input state Parent done $ ./detectChildInputState -I=1 -N=9 Child waiting for input Parent done $ Kid done (read 65538 bytes) $ ./detectChildInputState -I=0 -N=9 Command timed out Kid never entered input state Parent done

The main difference revolves around the SIGPIPE signal which on Unix would be delivered to a process if it attempts to write to a broken pipe (this is the case when the child terminates before having read anything).

By default, this signal would terminate the writing process, so it would have to be handled one way or another, anyway (e.g. $SIG{PIPE} = 'IGNORE'). OTOH, we can take advantage of this error notification, in which case we don't need an extra thread (or process) doing the blocking write.  The logic is kind of reversed now: we assume things went ok, unless we know otherwise, which is when

in those cases, $inInputState is set to zero in the respective signal handler.

A couple of more notes:


In reply to Re^3: Testing for a background process waiting for input (use a thread) by Eliya
in thread Testing for a background process waiting for input by SBECK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.