LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2008, 07:56 AM   #1
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Rep: Reputation: 30
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.

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

Now, how can i make fread() non-blocking?
 
Old 07-11-2008, 11:31 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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()"?
 
Old 07-11-2008, 01:54 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 07-12-2008, 04:56 AM   #4
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by paulsm4 View Post
Hi -

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 View Post
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.
 
Old 07-13-2008, 12:40 AM   #5
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
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.
 
Old 07-13-2008, 08:36 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 07-14-2008, 12:54 AM   #7
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by bigearsbilly View Post
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
can u please elaborate on this a bit further?

@theNbomr, thanks for the info u gave
 
Old 07-14-2008, 09:04 AM   #8
smoked kipper
Member
 
Registered: May 2008
Location: UK
Distribution: Slackware,Slamd64
Posts: 81

Rep: Reputation: 15
As paulsm4 said, you can turn off buffering with setvbuf, note that's "setvbuf", one 'f' not two.
 
Old 07-14-2008, 09:16 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
can u please elaborate on this a bit further?
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 *)
man fileno
 
Old 07-15-2008, 12:38 AM   #10
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
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().

Thanks for the info about fileno()
 
Old 07-15-2008, 02:21 PM   #11
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 07-16-2008, 05:41 AM   #12
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by paulsm4 View Post
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
 
  


Reply

Tags
nonblocking



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best way of using fread and fwrite rohanak Programming 1 05-02-2008 07:26 AM
Using fread in linux... jaepi Programming 1 11-14-2007 02:34 AM
how to undo an fread simasimon Programming 19 04-25-2007 01:45 PM
problem with fread function nesta Programming 4 04-02-2007 01:10 PM
using fread with fscanf andystanfordjason Programming 4 12-20-2006 09:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration