Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl-Sensitive Sunglasses
 
PerlMonks  

IO::Pipe hangs

by Anonymous Monk
on Sep 20, 2012 at 19:06 UTC ( #994733=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, why pipe hangs after "last" statement:
use strict; use warnings; use IO::Pipe; my $cmd = "tasklist"; my $pipe = IO::Pipe->new(); $pipe->reader($cmd); while (my $line = $pipe->getline()) { print $line; last if $line =~ /^smss/; } $pipe->close(); C:\>R:\io_pipe_test.pl Image Name PID Session Name Session# Mem + Usage ========================= ======== ================ =========== ====== +====== System Idle Process 0 Services 0 + 24 K System 4 Services 0 + 300 K smss.exe 284 Services 0 1 +.140 K Terminating on signal SIGINT(2) C:\>

Comment on IO::Pipe hangs
Download Code
Re: IO::Pipe hangs
by BrowserUk (Pope) on Sep 20, 2012 at 19:14 UTC

    Because you haven't emptied the pipe, which means that tasklist is blocked waiting to finish writing to it.

    Just closing the pipe won't allow tasklist to end; the pipe won't close until it does. Deadlock.

    The answer is to empty the pipe before closing it:

    use strict; use warnings; use IO::Pipe; my $cmd = "tasklist"; my $pipe = IO::Pipe->new(); $pipe->reader($cmd); while (my $line = $pipe->getline()) { print $line; last if $line =~ /^smss/; } 1 while $pipe->getline; $pipe->close();

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    RIP Neil Armstrong

      OK, thanks.
Re: IO::Pipe hangs
by choroba (Abbot) on Sep 20, 2012 at 19:17 UTC
    On Linux, it works (I used "ps x" as the command and /alpine/ as the regex.)
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      On Solaris it works as well. Something wrong with Windows implementation?
      C:\>perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +64-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2011, Larry Wall Binary build 1402 [295342] provided by ActiveState http://www.ActiveSt +ate.com Built Oct 7 2011 15:19:36 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. C:\>

        Something wrong with Windows implementation?

        It doesn't hang for me with sysinternals pslist, Devel::VersionDump

        Perl version: v5.14.1 on MSWin32 (C:\perl\5.14.1\bin\MSWin32-x86-multi +-thread\perl.exe) Carp - 1.26 Devel::VersionDump - 0.02 Exporter - 5.66 IO - 1.25_04 IO::Handle - 1.31 IO::Pipe - 1.14 SelectSaver - 1.02 Symbol - 1.07 XSLoader - 0.15 strict - 1.04 warnings - 1.12
        Something wrong with Windows implementation?

        More of a platform difference -- a thread used instead of a fork.

        Use this instead:

        use strict; use warnings; my $cmd = "tasklist"; my $pid = open PIPE, "-|", $cmd or die $!; while (my $line = <PIPE> ) { print $line; last if $line =~ /^smss/; } close PIPE;

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.

        RIP Neil Armstrong

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994733]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-04-20 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls