Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: sysread/syswrite wrappers

by BrowserUk (Pope)
on Oct 12, 2016 at 15:10 UTC ( #1173855=note: print w/replies, xml ) Need Help??


in reply to Re^4: sysread/syswrite wrappers
in thread sysread/syswrite wrappers

No, primary reason to use sysread/syswrite with select, is because readline and print are not working with select.

Of course they do:

#! perl -sw use strict; use Win32::Socketpair qw[ winsocketpair ]; my( $fd1, $fd2 ) = winsocketpair(); my $v = ''; vec( $v, my $fn1 = fileno( $fd1 ), 1) = 1; vec( $v, my $fn2 = fileno( $fd2 ), 1) = 1; while( 1 ) { if( select( my $vin = $v, my $vout = $v, undef, undef ) > 0 ) { if( vec( $vout, $fn1, 1 ) ) { print( $fd1 "hello\n" ) or last; } if( vec( $vin, $fn2, 1 ) ) { defined( my $buffer = <$fd2> ) or last; print "read: $buffer"; } } } __END__ C:\test>junk88 read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello read: hello Terminating on signal SIGINT(2) read: hello

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^6: sysread/syswrite wrappers
by vsespb (Chaplain) on Oct 12, 2016 at 15:22 UTC

      Yes, they do!

      print is just syswrite + a little; readline is just sysread with a loop looking for the current value of $/.

      In other words, your wrappers are exactly equivalent to those built-ins; except that they are in C and thus vastly more efficient; and well tried and tested thus more likely to be correct.

      As for that; there is a huge difference between quoting the manual and understanding what it says.

      The *ONLY REASON* print and readline are said to "not work with select", is because they will block if they receive a partial message, thus preventing the code from getting back to the select. IE. They do exactly what your wrappers do; except they're more likely correct.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Yes, they do!
        It's not proof, because code which happens to work without race condition does not proof, that there is no race condition.
        The *ONLY REASON* print and readline are said to "not work with select", is because they will block if they receive a partial message, thus preventing the code from getting back to the select
        You don't give proof for that.
        Now my proof is select man page and this: http://perl.plover.com/FAQs/Buffering.html
        For efficiency, Perl uses a trick called buffering. The first time you ask to read from the file, Perl has to make a system call. Since system calls are expensive, it plans ahead. If you tried to read a little bit of text, you will probably want to read the rest later. The blocks on your disk are probably about 8K bytes, and your computer hardware is probably designed to transfer an entire block of data from the disk at once. So instead of asking the system for a little bit of text, Perl actually asks the system for an entire blockful, which hardly takes longer to get than a little bit would have. Then it stores this block of data in a region of memory that is called a buffer, and gives you back the one line you asked for. The next time you ask for a line, Perl already has the line you want in memory in the 8K buffer. It doesn't have to make another system call; it just gives you the next line out of the buffer. Eventually you read up to the end of the buffer, and then Perl makes another system call to get another bufferful of data.
        You can see this when executing eof() of FH with data and running select then. Select won't return "can read", while there was data in FH. But readline will return this data. That's because readline reads not only from file, but from buffer as well.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2019-12-11 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?