# i,o,e are std in/out/err
$p = open3( $i, $o, $e, @cmdline );
$hr->{'alive'} = 1;
$hr->{'pid'} = $p;
$hr->{'stdout'}= $o;
$hr->{'stdin'} = $i;
$hr->{'stderr'} = $e;
####
# The reader is given the hash reference, the 'o'
# indicates this is the 'stdout' reader
$hr->{'o_thread'} = threads->create( \&reader_thread, $hr, 'o' );
####
sub reader_thread {
my ($hr,$who) = @_;
my $c;
my $h;
my $r;
my $q;
threads->detach();
# determine handle.
if( $who eq 'o' ){
$h = $hr->{'stdout'};
} else {
$h = $hr->{'stderr'};
}
# Get the output queue
$q = $hr->{'Q'};
while( 1 ){
if( $hr->{'alive'} == 0 ){
last;
}
# Try to read *ONE* byte
$r = read( $h, $c, 1 );
if( $r > 0 ){
if( length($c) ){
$q->enqueue($c);
}
} else {
threads->yield();
}
}
}