Versions: threads::shared version 1.12 threads version 1.63

Okay. That could explain some of the anomolies. I strongly recommend that you upgrade to the latest CPAN versions of both. There have been so many changes recently that it is impossible to keep track of what worked and what didn't with down-level versions.

However, would you try this both before and after the upgrade and tell me how you get on (the stuff after the __END_ token is examples of it running on my system):

#! perl -slw use strict; use threads qw[ yield ]; use threads::shared; $|++; my $cmd = $ARGV[ 0 ] || 'diskpart'; my $timeout = 10; my @results = timedCommand( $cmd, $timeout ); if( $results[0] ne '**TIMEOUT**' ) { print "Command returned\n", join '', @results; } else { print "Command timed out after $timeout seconds and returned\n", join '', @results; } sub timedCommand { my( $cmd, $timeout ) = @_; my $pid :shared; my( $thr ) = threads->create( \&pipedCmd, $cmd, $timeout, \$pid ); yield until $pid; sleep 1 while $pid and $timeout--; return $thr->join unless $pid; kill 3, $pid if $pid and $timeout; return '**TIMEOUT**', $thr->join; } sub pipedCmd { my( $cmd, $timeout, $pidref ) = @_; my @results; $$pidref = open my $fh, "$cmd |" or die "$!, $^E"; yield; push @results, $_ while defined( $_ = <$fh> ); undef $$pidref; return @results; } __END__ c:\test>timedCommand.pl Command timed out after 10 seconds and returned **TIMEOUT** Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. DISKPART> c:\test>timedCommand.pl exit Command returned Microsoft DiskPart version 5.1.3565 Copyright (C) 1999-2003 Microsoft Corporation. DISKPART> Leaving DiskPart... c:\test>timedCommand.pl cd Command returned c:\test

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^7: How to deal with a forked proces that is waiting for user input? by BrowserUk
in thread How to deal with a forked proces that is waiting for user input? by gepapa

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.