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

Re: sysread/syswrite wrappers

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


in reply to sysread/syswrite wrappers

why nobody wrote CPAN module with such wrappers?

My take on is that sysread is usually only used for non-blocking reads; and then usually in code dealing with multiple input streams.

In this usual case, you definitely don't want to block waiting for the rest of the expected input from any given stream, because it might be a long time coming; or indeed, never come.

Rather you want the read to return ASAP -- partial or not -- so that you can utilise any time that your wrapper would spend 'blocking' on one stream, checking other streams for input.

You say you are using sysread because you are using select; and presumably you are using select because you want to be doing other things whilst waiting for data to become available. What are those other things?

And what is that makes it important to be doing them, whilst waiting for data to start to arrive, not so important that you can hold off doing them indefinitely, if the pipe only gives you part of what you are expecting and never completes?


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^2: sysread/syswrite wrappers
by vsespb (Chaplain) on Oct 12, 2016 at 09:41 UTC
    Ok, your point very similar to answer above.
    Probably my case limited to IPC communications then. It's pipes, localhost only.
    I don't see reason to do non-blocking reads then. If data sending process is write whole message at once, say with
    syswritefull($fh, sprintf("%08d", length($line))) && syswritefull($fh, $line)
    it will be read by receiving process very fast (unless whole system unresponsive/swapping), and if sending process crashed/dies, there will be eof. Blocking pipes and reading whole message at once looks ok to me.
    With blocking pipes you can still select() between them to determine which process sent you next message to read.
      Blocking pipes and reading whole message at once looks ok to me.

      Then why sysread/syswrite? You'll say because you need select; but the only reason for using sysread/syswrite with select is because they don't wait for complete input.

      If you're confident that every read will be a complete message, just use readline & print and have done with it.

      But, think on this, pipes are just buffers, usually 4K at each end; and data is not passed to the reading process until a full 4K is available. (And setting line buffering won't change that.)

      This is easily demonstrated. The following code writes 122 byte lines every tenth of a second, but you will see no output for 3.35 seconds because that's how long it takes to fill the 4k buffer. It then produces batches of lines every 3.35 seconds until the writer closes the pipe:

      perl -E"select('','','',0.1),say 'test'x30 for 1 ..200" | perl -ple1

      And if your messages are not some exact multiple of 4K, the last line of every 4k block will be a partial message, and your wrapper will therefore block until the next 4k block has been filled and passed through, before that message will be completed.


      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.
        only reason for using sysread/syswrite with select is because they don't wait for complete input.
        No, primary reason to use sysread/syswrite with select, is because readline and print are not working with select.

        And I reason to use select in application, is that I have several processes which send message, and I am not sure which of them will send next message and when, so I need to select between their pipes.
        And if your messages are not some exact multiple of 4K, the last line of every 4k block will be a partial message, and your wrapper will therefore block until the next 4k block has been filled and passed through, before that message will be completed
        That's true for blocking pipes, anyway. Post is about blocking pipes. Yes, with blocking pipes master process performance would be limiting factor for all processes. I didn't see when it's a problem in my IPC applications - either master is fast, or messages are rare/small, or child need a reply anyway, just after sending a message.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2019-12-06 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (159 votes). Check out past polls.

    Notices?