Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: How can I read DATA in parent and child?

by hdb (Monsignor)
on Feb 18, 2019 at 15:04 UTC ( #1230111=note: print w/replies, xml ) Need Help??


in reply to Re^2: How can I read DATA in parent and child?
in thread How can I read DATA in parent and child?

The docs on fork perlfork say that parent and child share the same seek pointer:

Any filehandles open at the time of the fork() will be dup()-ed. Thus, the files can be closed independently in the parent and child, but beware that the dup()-ed handles will still share the same seek pointer. Changing the seek position in the parent will change it in the child and vice-versa. One can avoid this by opening files that need distinct seek pointers separately in the child.

So I find it surprising that this version works.

  • Comment on Re^3: How can I read DATA in parent and child?

Replies are listed 'Best First'.
Re^4: How can I read DATA in parent and child?
by choroba (Archbishop) on Feb 18, 2019 at 18:18 UTC
    > So I find it surprising that this version works.

    It works sometimes . I've run it 10 times and got the following output twice:

    Start p p: a Start c stop c p: b p: c p: d p: e stop p

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      It works sometimes.

      As I do an fdopen, I think it should work each time. Infact: I'm just running below code and it's giving me the expected result now the 20th time.

      Here is the (slightly modified) code, which I think should work.

      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\n"; sleep 4; while (<$x>) { print "c: $_"; sleep 1; } print "stop c\n"; } else { my $y= IO::Handle->new(); $y->fdopen(fileno(DATA), "r"); seek $y, $pos, 0; print "Start p\n"; while (<$y>) { print "p: $_"; sleep 1; } print "waiting for child\n"; wait; print "stop p\n"; } __DATA__ a b c d e

      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        I've run it 20 times, it returned 15 lines 18 times, but only 10 lines 2 times.
        #!/bin/bash i=1 while : ; do n=$( 1230099.pl | wc -l ) (( n == 15 )) || break printf '%d\r' $((i++)) done echo "$i:$n"

        It stopped after four another iterations with only 10 lines of output.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1230111]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2022-05-25 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?