Find the answer to your Linux question:
Results 1 to 5 of 5
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. ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15

    Question How to use non-blocking fread()?

    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. 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.

    But the problem is that fread() blocks by default too and there is no O_NONBLOCK flag in fopen().

    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. Now, how can i make fread() non-blocking?

  2. #2
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    As I understand it, fread(3) is just a wrapper on top of read(2), so if read(2) won't work for you, I don't think fread(3) can either. I can't answer your specific question, but FYI fileno() will convert a FILE * to an int fd, although mixing read and fread functions is generally A Bad Idea.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    so fread() is just a wrapper for read? so if read is returning 3 bytes, fread should also return 3 bytes. I'll examine the problem at my end.

    Thanks for info about fileno() I am not mixing fread and read. I have to use only one of them but i want to know if using one or the other makes any difference.

    waiting for ur reply

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    I didn't mean to imply you were using both read() and fread(), just that it's not good to mix and match the 'f'*()' library functions with the non-'f*()' system calls.

    I'm not familiar with the details of how Linux implements fread() but in UNIX fread() would read blocks from the input stream and drip-feed the results via fread() as required, reading fresh blocks as necessary. This makes it ideal to do formatted reads of disk-held structures but, as you're finding out, not so good for interactive devices. I'd guess that the read() is returning 3 bytes because that's all the mouse has generated (I doubt it'd return a structure so there may be a middleware function to call to get that data), while fread() is blocking because it hasn't had a line terminator to send the buffer to the user program.

    I'll be interested in your solution when you find it!

  5. #5
    Just Joined!
    Join Date
    May 2008
    Location
    India
    Posts
    15
    hey, thanks for the reply
    i think you are right in saying that the mouse is generating only 3 bytes.
    Actually when i run the program on normal PC, read() call on mouse returns the entire linux "input_event" structure i.e. 16 bytes, but on the embedded device, read() returns only 3 bytes. I guess this is probably because the event structure might be different for the embedded device.

    Actually i don't have the hardware with me right now. I am just sending the code to a person who has the hardware and he is testing the code. I should get the hardware in some days, then i can test the code myself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...