Hi again,

thank you for your support, in return this code which works for me quite well.

It migth be a good idea to put that in FAQ, but carfully look for key words as I sometimes do not know for which words to look for!

Some remarks to this code
Using the STDIN within the function of the kid I didn't get anything from STDIN. Only this way I was able to get the lines from 'dad'.

Unblocking the read of course forces you to do a sleep or wait (like my function to wait for full minute) otherwise the kid will heat up the pc ;-). It is a bit self adapting to keep the buffer reasonable small..

Not to have so many loops in loops in loops, I first push all readables in @LINES and the treat them.

Who ever wants/will write this FAQ-topic may use/change it.

Thanks for all
Carl

use IO::Handle; my ($readPIPE,$writePIPE, $pid); pipe $readPIPE, $writePIPE; $writePIPE->autoflush(1); $pid = fork; if ( !$pid ) { # child close($writePIPE); nonBlockingRead( $readPIPE ); exit; } else { # parent close($readPIPE); my (n,$s) = (0,0); while (1) { $s = (int(rand( 50 ))); sleep (int(rand( 50 ))); print $writePIPE "Dad: line ",++$n," after $s s\n"; } } sub nonBlockingRead { use IO::Select; my $PIPE = ( shift ); my $SEL = IO::Select->new( $PIPE ); my @LINES = qw ( ini deafault array size ); IO::Handle->new_from_fd(fileno( $PIPE ),"r")->blocking(0); while ( $SEL->exists( $PIPE ) ) { ($sleep,$send) = doWait( $sleep, scalar @LINES ); @LINES = (); foreach my $h ( $SEL->can_read(0) ) { push @LINES, ( <$h> ); } foreach my $l ( @LINES ) { &doSomething( $l ); } } } sub doWait { my $sec = (shift); my $NoL = (shift); #: every whole min $sec = ($NoL>20)?($sec/2):(($NoL< 5)?($sec+1):$sec); my $m = 60 - (time%60); if ( ( $m - $sec/2 ) < $sec ) { sleep ($m-0.2); return ($sec, 1); # ok full Min } else { sleep ($sec); return ($sec, 0); # default } }

In reply to Re^4: forked kid can't read from IO::Select->can_read by Anonymous Monk
in thread forked kid can't read from IO::Select->can_read by Anonymous Monk

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.