4

I don't understand what the buffer is doing and how it's used. (Also, if you can explain what a buffer normally does) In particular, why do I need fflush in this example?

int main(int argc, char **argv) { int pid, status; int newfd; /* new file descriptor */ if (argc != 2) { fprintf(stderr, "usage: %s output_file\n", argv[0]); exit(1); } if ((newfd = open(argv[1], O_CREAT|O_TRUNC|O_WRONLY, 0644)) < 0) { perror(argv[1]); /* open failed */ exit(1); } printf("This goes to the standard output.\n"); printf("Now the standard output will go to \"%s\".\n", argv[1]); fflush(stdout); /* this new file will become the standard output */ /* standard output is file descriptor 1, so we use dup2 to */ /* to copy the new file descriptor onto file descriptor 1 */ /* dup2 will close the current standard output */ dup2(newfd, 1); printf("This goes to the standard output too.\n"); exit(0); } 
3
  • I already googled it but still didn't understand it... Commented Mar 20, 2015 at 22:23
  • OK, so how would 'About 8,490,001 results' help any? Commented Mar 20, 2015 at 22:41
  • 3
    Well, that added result would help if you can answer my question Commented Mar 20, 2015 at 22:43

2 Answers 2

5

In a UNIX system the stdout buffering happens to improve I/O performance. It would be very expensive to do I/O every time.

If you really don't want to buffer there's some options:

  1. Disable buffering calling setvbuf http://www.cplusplus.com/reference/cstdio/setvbuf/

  2. Call flush when you want to flush the buffer

  3. Output to stderr (that's unbuffered by default)

Here you've more details: http://www.turnkeylinux.org/blog/unix-buffering

I/O is an expensive operation, so to reduce the number of I/O operations the system store the information in a temporary memory location, and delay the I/O operation to a moment when it has a good amount of data.

This way you've a much smaller number of I/O operations, what means, a faster application.

Sign up to request clarification or add additional context in comments.

1 Comment

So when you try to print to the terminal, does what you want to go to it go into a buffer or something? I don't understand how the buffer is used. And how does it improve performance?
2

danielfraca answers most of the question, but there's another part: what is the default buffering on a stream?

An output stream is line-buffered by default if and only if it refers to a terminal. Otherwise it is full-buffered. Also note that both kinds of buffering will auto-flush if more than BUFSIZ bytes are written (usually a power of two between 512 and 8192).

So this program:

#include <stdio.h> #include <unistd.h> int main() { puts("Hello"); fork(); puts("World"); } 

produces this output:

% ./fork Hello World World % ./fork | cat Hello World Hello World 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.