Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!aplcen!uunet!stealth.acf.nyu.edu!brnstnd From: brnstnd@stealth.acf.nyu.edu Newsgroups: comp.unix.questions Subject: Re: fork() and redirection Message-ID: <15031:23:59:19@stealth.acf.nyu.edu> Date: 15 Feb 90 23:59:20 GMT References: <7638@tank.uchicago.edu> <12137@smoke.BRL.MIL> Reply-To: brnstnd@stealth.acf.nyu.edu (Dan Bernstein) Distribution: usa Organization: IR Lines: 15 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