Interesting. I thought syswrite would only write as much as it could without blocking, but my test (on FreeBSD 6.1) shows otherwise.

Test:

use strict; use warnings; use IO::Handle qw( ); use IO::Select qw( ); { pipe(my $fh_r, my $fh_w) or die("Unable to create pipe: $!\n"); $fh_w->autoflush(1); my $sel_w = IO::Select->new($fh_w); { print("Filling up the pipe...\n"); my $buf = 'x'; for (;;) { my @ready_w = $sel_w->can_write(0.5); # Pipe is full! last if not @ready_w; die if @ready_w != 1; die if $ready_w[0] != $fh_w; my $rv = syswrite($fh_w, $buf, length($buf)); if (not defined $rv) { die("Unable to write to the pipe: $!\n"); } if (not $rv) { die("Unable to write to the pipe: 0 bytes written??\n"); } } } { print("Opening up one byte in the pipe...\n"); my $buf = ''; my $rv = sysread($fh_r, $buf, 1); if (not defined $rv) { die("Unable to read from the pipe: $!\n"); } if (not $rv) { die("Unable to read from the pipe: 0 bytes read??\n"); } } { print("Make sure it's safe to write to the pipe...\n"); my @ready_w = $sel_w->can_write(); die if @ready_w != 1; die if $ready_w[0] != $fh_w; } { print("Writing more chars to the pipe than the pipe can handle.. +.\n"); my $buf = 'x' x (1024*1024); my $rv = syswrite($fh_w, $buf, length($buf)); if (not defined $rv) { die("Unable to write to the pipe [2]: $!\n"); } if (not $rv) { die("Unable to write to the pipe [2]: 0 bytes written??\n"); } printf("Wrote %d bytes to the pipe\n", $rv); } close($fh_w); close($fh_r); }

Output:

Filling up the pipe... Opening up one byte in the pipe... Make sure it's safe to write to the pipe... Writing more chars to the pipe than the pipe can handle... [blocks]

That's unfortunate.


In reply to Re^8: Pipes and IO::Select's method can_write() (blocking) by ikegami
in thread Pipes and IO::Select's method can_write() by almut

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.