Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

[SOLVED] File handle passing issue under *nix

by vsespb (Hermit)
on Sep 30, 2013 at 17:56 UTC ( #1056402=perlquestion: print w/ replies, xml ) Need Help??
vsespb has asked for the wisdom of the Perl Monks concerning the following question:

Some code:

main.pl:
use strict; use warnings; use IO::Handle; $^F = 100; pipe READHANDLE,WRITEHANDLE; binmode READHANDLE; binmode WRITEHANDLE; WRITEHANDLE->autoflush; READHANDLE->autoflush; unless (fork()){ exec $^X, $ARGV[0], fileno(READHANDLE); die; } print WRITEHANDLE "Test!\n"; close WRITEHANDLE; wait;
subcommand1.pl:
open IN, "<&", $ARGV[0] or die $!; binmode IN; while(defined($_ = <IN>)) { print "line $_"; } close IN;
subcommand2.pl:
open IN, "<&", $ARGV[0] or die $!; binmode IN; my $s = <IN>; print "line: $s"; close IN;
Problem:
perl main.pl subcommand1.pl
hangs after first line, while
perl main.pl subcommand2.pl
works fine.
Also
cat subcommand1.pl | perl subcommand1.pl 0

works fine, meaning subcommand1.pl reads from file correctly.

So, I cannot understand why there is some kind of deadlock in subcommand1.pl?


UPD: SOLVED Re: File handle passing issue under *nix

Comment on [SOLVED] File handle passing issue under *nix
Select or Download Code
Re: File handle passing issue under *nix
by McA (Curate) on Sep 30, 2013 at 19:16 UTC

    Hi vsespb,

    as I have to do penance for my sarcastic answer in another thread of today, here my points:

    1) You subcommand2.pl test is IMHO a false positive. As soon as you read twice you also get this stuck behaviour. Try this:

    open IN, "<&", $ARGV[0] or die $!; binmode IN; my $s = <IN>; print "line: $s"; print "Attempt to read twice:"; $s = <IN>; print "line: $s"; close IN;

    So, it's the same as subcommand1.pl. You're just kidding yourself.

    2) As soon as you modify your main.pl to the following, it works:

    use strict; use warnings; use IO::Handle; $^F = 100; pipe READHANDLE,WRITEHANDLE; binmode READHANDLE; binmode WRITEHANDLE; WRITEHANDLE->autoflush; READHANDLE->autoflush; unless (fork()){ close WRITEHANDLE or die $!; exec $^X, $ARGV[0], fileno(READHANDLE); die; } close READHANDLE or die $!; print WRITEHANDLE "Test!\n"; close WRITEHANDLE; wait;

    You see, that the unnecessary ends of the pipe are closed. See http://man7.org/linux/man-pages/man2/pipe.2.html for the description.

    Best regards
    McA

      1) You subcommand2.pl test is IMHO a false positive.
      Indeed, but my intention was to proof that it at least can read something and not hang (so, exec() call works like expected etc).
      2) As soon as you modify your main.pl to the following, it works
      Damn! You are right! I was trying version which closes READHANDLE, but forgot about WRITEHANDLE ! Thank you. I think it's solved now!

      p.s.
      I upvoted your sarcastic post, it looks acceptable to me.

        You're welcome. McA

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056402]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-08-01 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls