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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |