Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: sysread blocking ??

by tybalt89 (Monsignor)
on Nov 06, 2020 at 15:04 UTC ( [id://11123452]=note: print w/replies, xml ) Need Help??


in reply to Re^2: sysread blocking ??
in thread sysread blocking ??

The error code is in $!.
If a handle is set to non-blocking and there is no data present and a sysread returns undef, the error should be either EWOULDBLOCK or EAGAIN. Any thing else is a real error.
The error text you were seeing "Resource temporarily unavailable" was from EAGAIN.
sysread() ultimately calls the C function "read", see "man 2 read" for information about read errors includiung EAGAIN.

Setting $parent to non-blocking does not cause an error, did you mean $child ?

Another problem with your code is the sleep in the receiver section. If data arrives on the socket during that sleep, response will be delayed until that sleep completes. In general, there should be no sleeps in a receiver except for the timeout in can_read or select. This is why select() and IO::Select were invented!

In general, any attempt to use non-blocking should be avoided, and should require exceptional justification before it is allowed.

Following is a set of suggested "tweaks" for eliminating that problem and several others.

#!/usr/bin/perl use diagnostics; use strict 'subs'; use strict 'refs'; use Socket; use IO::Handle; use IO::Select; my $child; # filehandle to child process my $parent; # filehandle to parent process my $pid; # Process ID of child process # w r i t e L i n e # Writes a buffer to the filehandle. sub writeLine { my ($fh, $buf) = @_; print $fh $buf; # while( length $buf ) # alternate to yours, but single print is the + same # { # my $stat = syswrite $fh, $buf; # $stat and substr $buf, 0, $stat, ''; # } } #writeLine() socketpair($child, $parent, AF_UNIX, SOCK_STREAM, PF_UNSPEC) or die "socketpair: $!"; $child->autoflush(1); $parent->autoflush(1); if ($pid = fork()) { #parent close $parent or die "close: $!\n"; my $sel = IO::Select->new($child); my @handles; my $buf = ''; while ($sel->count) { # print STDOUT time%100, ": polling child\n"; for my $fh ( @handles = $sel->can_read(1) ) { if( sysread $fh, $buf, 1e6, length $buf ) { print STDOUT time%100, ": received <$1>\n" # because you may + get more while $buf =~ s/(.*)\n//; # than one line a +t a time } else { $sel->remove($fh); } } @handles or print STDOUT time%100, ": no input from child at thi +s time\n"; } my $stat = wait; die "wait returned $stat\n" unless $stat == $pid; print STDOUT time%100, ": child reaped, parent exiting\n"; exit 0; } else { die "cannot fork: $!" unless defined $pid; close $child or die "close: $!\n"; writeLine($parent, time%100 . ": child started\n"); sleep 4; writeLine($parent, time%100 . ": child wrote again\nwith two lines +\n"); sleep 2; writeLine($parent, "E_O_F\n"); close $parent or die "close: $!\n"; #causes termination print STDOUT time%100, ": child exiting\n"; exit; }

EDIT: removed overlooked ->blocking call.

Replies are listed 'Best First'.
Re^4: sysread blocking ??
by ikegami (Patriarch) on Nov 07, 2020 at 00:25 UTC

    With the select, there's no point in making the handle non-blocking anymore.

    That said, I don't understand why a non-blocking handle or select is being used at all.

      The non-blocking call should have been removed, but was overlooked. It's gone now :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11123452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-28 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found