On 01/17/2014 01:10 PM, Eric Blake wrote: >> However.. >> >> Do I understand that to say that if the first thing my child does is >> >> fclose(fp); >> >> everything should be hunky-dory? > > No. You have to fix things _in the parent, before the fork()_ for > everything to be hunky-dory. The easiest way to do that is to > fflush(NULL) before fork()ing.
The exception to needing to fflush() before forking is when the child will call exec*() or _exit(), as those paths discard any partially-read buffers without reflecting them back to the underlying fd, and thus don't interfere with the parent's notion of where to continue reading from the fd. And as it is, all this discussion about fflush(NULL) before fork() depends on the POSIX folks fixing this bug which I just filed: http://austingroupbugs.net/view.php?id=816 without that fix in POSIX, you could argue that fflush(NULL) won't do anything to input streams, and that you would have to explicitly call fflush(stream) for every input stream, for cases where you know your child is going to exit() rather than _exit() or exec*(). -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature