jeudi 24 février 2011

Stdio buffering

Nice article about stdio buffering:

http://www.pixelbeat.org/programming/stdio_buffering/

I recently had the following problem: I launch p1 | p2 (piped processes, p2 is the receiver), and p2 isn't receiving on its stdin the data (a few bytes) p1 is writing to its stdout. After a while I discovered that the data is buffered (4096 bytes if the process output is not redirected to a terminal, else it is line buffered).

To make it line-buffered all the time, use setlinebuf:

#include <stdio.h>
[...]
setlinebuf(stdout);

Since there is no setlinebuf in windows, a portable manner to do this is:

#include <stdio.h>
[...]
printf(line);
fflush(stdout); //to do after each printed line

Aucun commentaire:

Enregistrer un commentaire