fork() and redirection

brnstnd at stealth.acf.nyu.edu brnstnd at stealth.acf.nyu.edu
Fri Feb 16 10:59:20 AEST 1990


When you use printf, stdio buffers your output and sends it out in big
chunks. In your case you print into the buffer, then fork. Now both the
parent and the child have the same buffer, so the buffer ends up being
printed twice.

On the other hand, if stdout is a terminal, stdio sends the buffer out
as soon as you finish a line. So the lines are printed immediately. When
you fork, the buffer is empty, so nothing is printed twice.

In general, you should flush output buffers before forking, unless you
know the child is going to exec.

Read the setbuf() man page for more information on buffering.

---Dan



More information about the Comp.unix.questions mailing list