ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am using read() to read events from a device file. Basically i am reading mouse events. Since read() blocks by default, i used O_NONBLOCK option to make it non-blocking.
But sometimes i am not able to get the entire mouse events using read(), so i am trying to use fread() instead.
But the problem is that fread() blocks by default too and there is no O_NONBLOCK flag in fopen().
The calls you're looking for are either "setbuf (fp, 0)" or "setvbuff ()".
"man setvbuff" for more details.
But...
If you're having problems with "read()", what makes you think you'll have any better luck with "fread()"?
what does setbuff and setvbuf do? I couldn't find find the man pages of these commands in Ubuntu.
Actually i am reading mouse events on an embedded device. Using read() i am only getting 3 bytes of data whereas the event structure is 16 bytes. fread() returns 16 bytes.
Quote:
Originally Posted by bigearsbilly
the f functions use stdio buffering,
you won't get what you want ever.
read won't always read all the bytes in one go if you don't catch it right.
you'll probably need to use select
Earlier when i was using read(), i used it with poll() (which is similar to select) but with fread() i can't use either poll() or select() because both require an integer file descriptor instead of (FILE *). Even fcntl() requires an integer file descriptor.
Basically i am not able to find any solution which makes fread unblockable.
somebody help me please!
Is there no way to make fread unblocking? I don't understand that while read can be made unblocking by 2-3 ways (poll, O_NONBLOCK etc.), there is no way to make fread unblocking.
In article #3, bigearsbilly gave you the answer. You can't get there from here. Buffered reads are incongruent with non-blocking reads. If you want your pie to taste like apples, don't order pumpkin pie.
--- rod.
well, with the fread functions you aren't reading, it reads into it's own buffer
and worries about all the difficult stuff for you. so you get it second-hand as it were.
you do an fread it already has it buffered.
and incidentally FYI
Quote:
i can't use either poll() or select() because both require an integer file descriptor instead of (FILE *)
basically i am not concerned with fread or read. I want to read mouse events on an embedded device. My program returns 16 bytes thru read() on a normal PC but returns only 3 bytes on the embedded device using embedded linux.
Is it possible that the event structure on the embedded device is such that i am getting only 3 bytes. It might not be the problem of read() after all. I think if using read() i am getting 3 bytes, then it should be same as fread() since fread() internally calls read().
I don't think you know yet one way or the other what the mouse is actually returning - you need to find out.
I would definitely look into "setbuf (fp, 0)" or "setvbuf()" (one "f" - sorry about the typo), or even "ioctl()". Which (any, all or none) of these APIs are available to you depends entirely on your vendor's C library.
I would *not* encourage your to mix'n'match standard I/O calls (like "fopen()") with low-level calls (like "read()"). Generally speaking, that's asking for trouble (IMHO).
I don't think you know yet one way or the other what the mouse is actually returning - you need to find out.
I would definitely look into "setbuf (fp, 0)" or "setvbuf()" (one "f" - sorry about the typo), or even "ioctl()". Which (any, all or none) of these APIs are available to you depends entirely on your vendor's C library.
I would *not* encourage your to mix'n'match standard I/O calls (like "fopen()") with low-level calls (like "read()"). Generally speaking, that's asking for trouble (IMHO).
Good luck .. PSM
Thanks for the reply.
I am not sure why i need to use setbuf or setvbuf since these APIs set the buffer options for writing to a device/file. My problem is with reading events not with writing events. Do i need to use setbuf or setvbuf on the mouse device before reading the events? Please elaborate on this a bit.
One more query: Can i use ioctl() to get info about the event structure that the mouse returns? I am new to this ioctl() thing
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.