Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I’m stumped with a problem where STDIN gets terminated early only when fork’ed children use ssh and only when the parent sleeps between children. Below is a working stub. On 3 different SLES boxes (perl 5.10.0) it prints either 1 line, 2 lines, or 4096 bytes (terminating line 41 mid-line).

If I comment out the sleep OR remove the ssh (and just sleep), there is no problem and all 50 lines are processed. I don’t understand what the combination of ssh on the children and sleep of the parent have to do with preventing a full read of STDIN.

(The sleep is needed because without them I get several “ssh_exchange_identification: Connection closed by remote host” errors. The amount of sleep doesn’t appear to make any difference. The real code is ssh’ing to real nodes, localhost is just to show the error.)

Execute: ./p.pl < LIST

p.pl:

#!/usr/bin/perl use strict; use Time::HiRes qw(usleep); my $max = 0; while(my $prog = <STDIN>) { print STDERR "$prog"; $max++; die "Unable to fork\n" unless(defined(my $pid = fork)); if($pid == 0) # child { die "Unable to exec\n" unless(exec("/usr/bin/ssh localhost /bi +n/sleep 10")); # die "Unable to exec\n" unless(exec("/bin/sleep 10")); exit(0); } usleep(100000); } for(my $i = 0; $i < $max; $i++) { wait; } print STDERR "\nDONE\n";

LIST (each line is 99 chars + \n):

001 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 002 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 003 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 004 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 005 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 006 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 007 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 008 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 009 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 010 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 011 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 012 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 013 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 014 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 015 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 016 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 017 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 018 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 019 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 020 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 021 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 022 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 023 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 024 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 025 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 026 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 027 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 028 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 029 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 030 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 031 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 032 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 033 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 034 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 035 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 036 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 037 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 038 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 039 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 040 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 041 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 042 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 043 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 044 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 045 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 046 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 047 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 048 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 049 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345 050 123456789012345678901234567890123456789012345678901234567890123456 +78901234567890123456789012345

strace (reads and writes line 2, finds no more STDIN, moves to wait statements):

… read(0, "002 1234567890123456789012345678"..., 4096) = 4096 write(2, "002 1234567890123456789012345678"..., 100002 123456789012345 +678901234567890123456789012345678901234567890123456789012345678901234 +56789012345 ) = 100 lseek(0, 200, SEEK_SET) = 200 lseek(0, 0, SEEK_CUR) = 200 clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIG +CHLD, child_tidptr=0x2aaaabb1c1d0) = 2120 nanosleep({0, 100000000}, NULL) = 0 read(0, "", 4096) = 0 wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 2117 …


In reply to Early termination of STDIN with combination of ssh and sleep by orleans704

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-03-29 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found