Requesting help on intricacies of duplicated file descriptors.

Carl Miller chaz at energoncube.net
Sun Jul 23 23:06:00 UTC 2006


On Sun, Jul 23, 2006 at 06:50:39PM +0000, Garrett Kajmowicz wrote:
> On Sunday 23 July 2006 03:35, Rich Felker wrote:
> 
> <snip>
> 
> > Also it's a bad idea to use non-blocking io with stdio streams (FILE
> > *) unless you really know what you're doing. If no input is available,
> > the read operation will return with error and set the error flag for
> > the stream. It can be cleared with fclearerr() and presumably you can
> > continue reading then, but nonblocking io is really the most useful in
> > connection with the non-buffering unix-style fd io functions.
> 
> Thanks for the primer.  The case I need to address is reading a minimum of one 
> character from a stream (blocking until the character is available or 
> returning error if we are EOF).  At the same time, I want to read as many 
> characters as possible (up to a limit) to go into some buffering code.  Yes, 
> stdio does buffering, however I need to do my own limited buffering for 
> uClibc++.  The current implementation switches between blocking and 
> non-blocking when doing reads, but I take it that this isn't a good idea.  
> 
> Instead, what is my best approach to read a minimum of 1 byte from the stream, 
> blocking only if needed to get that first byte?

I'd skip stdio entirely, since you're doing your own buffering.  Nothing
worse than competing buffers.  Set the file descriptor non-blocking, and
before you read, do a poll() or select() on it, with infinite timeout.
You'll block until there's either at least one character to read, or the
fd has hit EOF.


                             -----Carl



More information about the uClibc mailing list