Sponsored Content
Top Forums UNIX for Advanced & Expert Users Discussion on buffering of standard I/O library Post 302501200 by Edward114 on Thursday 3rd of March 2011 01:17:57 AM
Old 03-03-2011
I just found out the work of input buffer in glibc2.2.5.
At std input, like fgetc(), that finally call the __read() in fillbuf().
Code:
fillbuf (register FILE *fp)
{
......
  if (fp->__buffer == NULL)
    {
      /* We're unbuffered, so we want to read only one character.  */
      buffer = (char *) &c;
      to_read = 1;
    }
  else
    {
      /* We're buffered, so try to fill the buffer.  */
      buffer = fp->__buffer;
      to_read = fp->__bufsize;
    }
  while (!ferror (fp) && !feof (fp) && nread <= buffer_offset)
    {
      /* Try to fill the buffer.  */
      int count = (*fp->__io_funcs.__read) (fp->__cookie, buffer, to_read);//Edward. at this ,call __read,the buffer is  "buffer",buffer size is "to_read"    
...
    }
...
}

The following results:
fillbuf() check unbuffered, check fully buffered. but it doesn't chek line buffer.So
Unbuffered: read a character every time.
Fully buffered:__read() try to fill the buffer, except for encounter a EOF.
Line buffered:it doesn't affect the __read().If stream fp have the buffer,it behavior like Full buffer. If have no buffer, like Unbuffered.

For a stream, the terminal default use line buffer. And else default use fully buffered. If you want to use a nubuffered, you nee set the pf->__userbuf of FILE struct(use setvbuf()) before any other I/O operation is performed on the stream.

I have another question, When I view the source code for glibc2.2.5, many functions can be found in. But in the higher version, such as glibc2.9, many functions can't be found,Such as fgetc(),why? Is it in assembly?
 

We Also Found This Discussion For You

1. Solaris

Standard Template Library in Solaris 8.0

Hi Everybody, Can anyone guide me how to install SGI's STL Library on Solaris 8.0 ? Out of SGI's STL and STLPort, which third party STL is suitable, stable and thread-safe for UNIX Platform ? Please guide me over the above issue . Thanks & Regards Dinesh-Ahuja (0 Replies)
Discussion started by: md7ahuja
0 Replies
LIBSTDBUF(3)						   BSD Library Functions Manual 					      LIBSTDBUF(3)

NAME
libstdbuf -- preloaded library to change standard streams initial buffering DESCRIPTION
The libstdbuf library is meant to be preloaded with the LD_PRELOAD environment variable to as to change the initial buffering of standard input, standard output and standard error streams. Although you may load and configure this library manually, an utility, stdbuf(1), can be used to run a command with the appropriate environ- ment variables. ENVIRONMENT
Each stream can be configured independently through the following environment variables (values are defined below): _STDBUF_I Initial buffering definition for the standard input stream _STDBUF_O Initial buffering definition for the standard output stream _STDBUF_E Initial buffering definition for the standard error stream Each variable may take one of the following values: "0" unbuffered "L" line buffered "B" fully buffered with the default buffer size size fully buffered with a buffer of size bytes (suffixes 'k', 'M' and 'G' are accepted) EXAMPLE
In the following example, the stdout stream of the awk(1) command will be fully buffered by default because it does not refer to a terminal. libstdbuf is used to force it to be line-buffered so vmstat(8)'s output will not stall until the full buffer fills. # vmstat 1 | LD_PRELOAD=/usr/lib/libstdbuf.so STDBUF_1=L awk '$2 > 1 || $3 > 1' | cat -n See also the manpage of stdbuf(1) for a simpler way to do this. HISTORY
The libstdbuf library first appeared in FreeBSD 8.4. AUTHORS
The original idea of the libstdbuf command comes from Padraig Brady who implemented it in the GNU coreutils. Jeremie Le Hen implemented it on FreeBSD. BSD
April 28, 2012 BSD
All times are GMT -4. The time now is 12:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy