This is really weird…

I removed the sleep commands and printed the $pos in the start message.

Both processes tell the same startposition. Nevertheless I also get non-15 lines of output every now and then. 1536 loops were okay, then the parent didn't read any lines.

My modified 123099.pl code:

#!/usr/bin/perl use IO::Handle; use strict; use warnings; my $pos= tell *DATA; my $pid= fork(); die unless defined $pid; if ($pid == 0) { my $x= IO::Handle->new(); $x->fdopen(fileno(DATA), "r"); seek $x, $pos, 0; print "Start c $pos\n"; while (<$x>) { print "c: $_"; } print "stop c\n"; } else { my $y= IO::Handle->new(); $y->fdopen(fileno(DATA), "r"); seek $y, $pos, 0; print "Start p $pos\n"; while (<$y>) { print "p: $_"; } print "waiting for child\n"; wait; print "stop p\n"; } __DATA__ a b c d e

And my modified version of your test-bash:

#!/bin/bash i=1 while : ; do o=$( ./1230099.pl ) n=$( echo "$o" | wc -l ) (( n == 15 )) || break printf '%d\r' $((i++)) done echo "$i:$n" echo "$o"

Update I've run it several times now and this is the result:

2182: 10 1504: 10 560: 10 14: 10 919: 10 360: 10 908: 10 200: 10 65: 10 108: 10 43: 10 105: 10 296: 10 345: 10 100: 10 1084: 10 1278: 10 224: 10 6: 10 8: 10 125: 10 684: 10 96: 10 8: 10 579: 10

So after an unpredictable amount of iterations (6 to 2182) the scripts prints 10 instead of 15 lines…


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

In reply to Re^7: How can I read DATA in parent and child? by Skeeve
in thread How can I read DATA in parent and child? by Skeeve

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.