This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54


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

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]