perl - sysread returns undef, errno is EINVAL, but working? -
I have some code set for non-blocking input from the system event file.
Sysopen (FILE, $ targetInput, O_NONBLOCK | O_RDONLY) or die "failed to open $ targetInput, quit. \ N"; Binmode (file); # More assignments and preparations here ... while (1) {# code which makes non-blocking I / O appropriate ... $ rBytes = sysread (FILE, $ buffer, 16); Printf ("% d vs% d \ n", $ !, EAGAN); Print Defined ($ rBytes)? "Determined! \ N": "Egg! \ N"; If (defined! ($ RBytes) & $! == EAGAIN) {# nothing read in non-blocked mode: sleep (1000); } And {# an event, on the go: print "got it! \ N"; Print "$ rBytes! \ N"; last; }} Here's a beautiful standard setup, using dozens of $ buffer ... ... There are dozens of examples available for this, however, I have found that, 100% of the time , $ rBytes
remains undefined, and $!
is set to Code 22, Envol (Invalid Argument). Many tests have gone into ensuring that this is the sysread
function which is especially due to this, and certainly nothing before that. This is the catch, it works . As you can see, the combination of my code not $ rBytes
is undefined (always true) and $!
is happening EAGAIN
(always false) Just say everything is okay, because I have not added any error handling followed by this code block, what is a huge switch / case;
When a valid input is , is received, $ rBytes / code> still undefined $!
is not yet EGNE
, even then the rest of the rest of the program and tasks are done in the same way, like what $ buffer
should do this If I was not looking at my CPU usage meter, then in fact I would have not seen it at all, and probably would not try to fix it if it is a quick shot shot script.
I can safely say that the charge of "invalid argument" is a fake, the question is why is this error code giving and why $ rBytes
Always undefined?
As I thus add a quick script to make a duplicate draft of the assumptions so far Had tried, I actually rejected myself on this reply; Of course, all the necessary information was present in the question.
A typical system event file works with messages that are 24 bytes in length This block of code was trying to remove 16-bit timestamps from those events, and for the rest Without it was rejected. For reasons I can not understand myself, sysread
do not like it.
By ensuring that the desired length is greater than or equal to 24, sysread
is read by the reported bytes and $! Documents can be as 24 magical numbers, or there may be a problem reading a partial message from non-blocking resources (there was no error in blocking mode) it still has the appropriate data to be
$ The buffer is inserting in
, possibly because the underlying code passes it by reference and does not zero it in the case of an error, and that is why everything that works after the block shown is OK. Yes.
Comments
Post a Comment