Professional Web Applications Themes

pipe - non blocking read? (fork/Win32) - PERL Miscellaneous

Hi, I'm using windows, I want to create a new process using fork (in Activestate's Perl 5.6.x for some x) and communicate between the parent and child using pipes. But I'd like the parent to be able to check if there was anything waiting for it in the pipe, and if not, come back and check later. Is this possible? If not any suggestions on what I can do? Stuart...

  1. #1

    Default pipe - non blocking read? (fork/Win32)

    Hi, I'm using windows, I want to create a new process using fork (in
    Activestate's Perl 5.6.x for some x) and communicate between the parent
    and child using pipes. But I'd like the parent to be able to check if
    there was anything waiting for it in the pipe, and if not, come back and
    check later.

    Is this possible? If not any suggestions on what I can do?

    Stuart

    Stuart Moore Guest

  2. #2

    Default Re: pipe - non blocking read? (fork/Win32)

    On Wed, 2 Jul 2003, Greg Bacon wrote:
    > In article <Pine.SOL.4.44.0307021543450.9290-100000red.csi.cam.ac.uk>,
    > Stuart Moore <stjm2hermes.cam.ac.uk> wrote:
    >
    > : Hi, I'm using windows, I want to create a new process using fork (in
    > : Activestate's Perl 5.6.x for some x) and communicate between the parent
    > : and child using pipes. But I'd like the parent to be able to check if
    > : there was anything waiting for it in the pipe, and if not, come back and
    > : check later.
    > :
    > : Is this possible? If not any suggestions on what I can do?
    >
    > Is there a reason you'd prefer pipes to, say, sockets?
    Um, no. Can Socekts do that? In which case how? A brief trawl through the
    doentation gave me nothing.

    Thank you very much
    Stuart

    Stuart Moore Guest

  3. #3

    Default Re: pipe - non blocking read? (fork/Win32)

    In article <Pine.SOL.4.44.0307021829550.26955-100000red.csi.cam.ac.uk>,
    Stuart Moore <stjm2hermes.cam.ac.uk> wrote:

    : On Wed, 2 Jul 2003, Greg Bacon wrote:
    :
    : > Is there a reason you'd prefer pipes to, say, sockets?
    :
    : Um, no. Can Socekts do that? In which case how? A brief trawl through
    : the doentation gave me nothing.

    At the bottom of the IO::Select manpage is a socket server. Here's
    an adaptation of that:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    use IO::Select;
    use IO::Socket;

    sub child {
    my $port = shift;

    my $s = IO::Socket::INET->new(PeerAddr => "localhost:$port");
    die "$0: failed to create socket in child" unless $s;

    for (1 .. 5) {
    sleep rand 10;

    print $s "message #$_\n";
    }
    }

    sub do_something_else {
    print "parent: Doing something else...\n";
    sleep rand 5;
    print "parent: Done.\n";
    }

    sub check_sockets {
    my $lsn = shift;
    my $sel = shift;

    my ready = $sel->can_read(0);
    foreach my $fh (ready) {
    if ($fh == $lsn) {
    # Create a new socket
    my $new = $lsn->accept;
    $sel->add($new);
    }
    else {
    # Process socket
    my $fd = fileno $fh;

    if (my $input = <$fh>) {
    chomp $input;
    print "parent: fd $fd: [$input]\n";
    }
    else {
    $sel->remove($fh);
    $fh->close;

    $lsn->close;
    }
    }
    }
    }

    ## main
    my $port = 8080;
    my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => $port);
    my $sel = new IO::Select($lsn);

    my $pid = fork;
    die "$0: fork: $!" unless defined $pid;

    unless ($pid) {
    child $port;
    exit 0;
    }

    while ($lsn->opened) {
    check_sockets $lsn, $sel;

    do_something_else;
    }

    Hope this helps,
    Greg
    --
    And when I heard the baby-talk coming out of George W. Bush's mouth in front
    of Congress, there's this Axis of Evil, Iran, Iraq and ... North Korea? I
    mean, he doesn't know where these places are, much less what evil is . . .
    -- Gore Vidal
    Greg Bacon Guest

  4. #4

    Default Re: pipe - non blocking read? (fork/Win32)

    Greg Bacon wrote:
    [snip]
    > my ready = $sel->can_read(0);
    > foreach my $fh (ready) {
    [snip]
    > if (my $input = <$fh>) {
    Don't do that.

    --
    $a=24;split//,240513;s/\B/ => /for=qw(ac ab bc ba cb ca
    );{push(b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop b))&&redo;}
    Benjamin Goldberg Guest

  5. #5

    Default Re: pipe - non blocking read? (fork/Win32)

    In article <3F04BDBD.9633257hotpop.com>,
    Benjamin Goldberg <ben.goldberghotpop.com> wrote:

    : Greg Bacon wrote:
    :
    : [snip]
    : > my ready = $sel->can_read(0);
    : > foreach my $fh (ready) {
    : [snip]
    : > if (my $input = <$fh>) {
    :
    : Don't do that.

    Why not?

    Greg
    --
    What light is to the eyes -- what air is to the lungs -- what love is to
    the heart, liberty is to the soul of man.
    -- Robert Green Ingersoll
    Greg Bacon Guest

  6. #6

    Default Re: pipe - non blocking read? (fork/Win32)

    Greg Bacon wrote:
    >
    > In article <3F04BDBD.9633257hotpop.com>,
    > Benjamin Goldberg <ben.goldberghotpop.com> wrote:
    >
    > : Greg Bacon wrote:
    > :
    > : [snip]
    > : > my ready = $sel->can_read(0);
    > : > foreach my $fh (ready) {
    > : [snip]
    > : > if (my $input = <$fh>) {
    > :
    > : Don't do that.
    >
    > Why not?
    The readline (aka <>) operator does the following:
    1a/ It scans it's internal buffer for the $/ string, and if
    it finds it,
    1b/ Removes (and returns) everything in it's buffer up to and
    including the $/. Anything after the $/ remains in the buffer.
    2/ After we looked for and didn't find $/, it uses the C read()
    function to get data from the filedescriptor and adds it to it's
    internal buffer.
    3/ Go back to step 1.

    The can_read method of IO::Select tells us whether or not the file
    descriptor has more bytes available... that is, whether or not calling
    the C read() function on that fd would block.

    Now, let's suppose that the other end of the pipe sends us data faster
    than we read it... e.g., suppose that it sends us three lines in the
    time it took us to process one.

    What happens?

    Select's can_read says the pipe is ready to read from, then we call
    readline(), which read()s *all* of the bytes that are in the pipe (in
    the file descriptor) into the internal byffer. Then, we remove one $/
    delimited line from that buffer, leaving two in the buffer. Then, we
    call can_read again... and there are no bytes in the pipe! There are
    bytes in the buffer, but none to be read from the file descriptor.

    Alas, can_read knows nothing of the filehandle's internal buffer, it can
    only examine the filedescriptor.

    As a result, we won't call <> the extra two times that we should, and
    thus those two lines never get read and printed out.

    Well, maybe they'll get printed the *next* time a line gets printed to
    the other end of the pipe... but that could be quite a long while after
    those two lines were sent. It wouldn't be a good thing if we were only
    able to read data many minutes after it was sent, would it?

    Further, consider if we're doing two-way communication, not just
    one-way... suppose the process on the other end of the pipe wants to
    read something from us, before it will send anything else to us. We're
    in trouble... we can't see what it sent, until it sends us "extra", and
    it won't send us extra, until we see and respond to what it sent. This
    is known of as deadlock.

    --
    $a=24;split//,240513;s/\B/ => /for=qw(ac ab bc ba cb ca
    );{push(b,$a),($a-=6)^=1 for 2..$a/6x--$|;print "$[$a%6
    ]\n";((6<=($a-=6))?$a+=$_[$a%6]-$a%6:($a=pop b))&&redo;}
    Benjamin Goldberg Guest

  7. #7

    Default Re: pipe - non blocking read? (fork/Win32)

    [email]gbaconhiwaay.net[/email] (Greg Bacon) wrote in message news:<vgh1oclf4ht56acorp.supernews.com>...
    > In article <3F04BDBD.9633257hotpop.com>,
    > Benjamin Goldberg <ben.goldberghotpop.com> wrote:
    >
    > : Greg Bacon wrote:
    > :
    > : [snip]
    > : > my ready = $sel->can_read(0);
    > : > foreach my $fh (ready) {
    > [snip]
    > : > if (my $input = <$fh>) {
    > :
    > : Don't do that.
    >
    > Why not?
    perldoc -f select

    <snip>

    WARNING: One should not attempt to mix buffered I/O (like "read"
    or <FH>) with "select", except as permitted by POSIX, and even
    then only on POSIX systems. You have to use "sysread" instead.


    >
    > Greg
    Bryan Castillo Guest

Similar Threads

  1. [ANN] win32-pipe 0.0.1
    By Daniel Berger in forum Ruby
    Replies: 0
    Last Post: November 22nd, 07:19 AM
  2. named pipe problem on Win32
    By Daniel Berger in forum Ruby
    Replies: 1
    Last Post: November 22nd, 01:48 AM
  3. How to know if read is pending on pipe
    By Helmut Leininger in forum AIX
    Replies: 1
    Last Post: September 8th, 07:54 AM
  4. Multithreading and sockets blocking I/O on Win32
    By Dragos D in forum PERL Miscellaneous
    Replies: 3
    Last Post: August 18th, 02:23 PM
  5. Will Thread blocking in read() leads to process blocking?
    By Loic Domaigne in forum UNIX Programming
    Replies: 4
    Last Post: July 23rd, 12:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139