Sub-optimal libc's read-ahead buffering behaviour

Andrey Chernov ache at FreeBSD.ORG
Thu Aug 4 08:25:34 GMT 2005

On Thu, Aug 04, 2005 at 05:57:11PM +1000, Peter Jeremy wrote:
> >In case SEEK_CUR still uses 
> >the buffer, it probably should not for character device.

As I look at the fseek code now, _any_ non-regular file seek is not 
optimized, which is right things (and BSD traditional).

> I can't see any reason for the current stdio behaviour:
> - If you're accessing a device with "magic" behaviour then it's not safe
>   to read(2) 4KB (or whatever) when userland asks to fread(3) 512 bytes.

It is safe to read more. You may hit EOF, but it handles by stdio 
internally. It is not safe to read again from the buffer. In that case 
fseek to needed position helps to re-read.

> - If the device doesn't have "magic" behaviour then you can just seek
>   within the stdio buffer.
> That said, I've seen similar behaviour on other systems so it could be
> a subtle side-effect of POSIX.

It is traditional BSD behaviour. Compare this place with less touched 
NetBSD fseeko.c f.e.


More information about the freebsd-current mailing list