Unexpected sysread block

S

Swarna

When I execute test1.pl, test2.pl is blocking.

Any ideas as to why the sysread is not returning immediately.

Thanks
Swarna



########## test1.pl #########
#!perl

open (FD, "|-", "perl test2.pl");
sleep (100);


########### test2.pl ##########

#!perl

my $buffer;

while (1) {

print STDERR "before sysread \n";
$bytes = sysread(STDIN, $buffer, 524288) || last;
die "undef bytes \n" unless defined $bytes;
print STDERR "after sysread \n";

}
print STDERR "last statement\n";
 
B

Ben Morrow

Quoth (e-mail address removed) (Swarna):
When I execute test1.pl, test2.pl is blocking.

Any ideas as to why the sysread is not returning immediately.

########## test1.pl #########
#!perl

open (FD, "|-", "perl test2.pl");
sleep (100);


########### test2.pl ##########

#!perl

my $buffer;

while (1) {

print STDERR "before sysread \n";
$bytes = sysread(STDIN, $buffer, 524288) || last;
die "undef bytes \n" unless defined $bytes;
print STDERR "after sysread \n";

}
print STDERR "last statement\n";

Ummm... because you aren't sending it any data? sysread is
un-*buffered*, not non-*blocking*. If you want a non-blocking read
(which will return 0 immediately if there is no data in the pipe to
read) then you will need to do

use IO::Handle;

STDIN->blocking(0);

in test2.pl. This is more portable than setting non-blocking mode
yourself using fcntl.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top