X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:in-reply-to:references:date
	:message-id:subject:from:to:content-type; q=dns; s=default; b=s9
	fZCFyYBm1iJRRHRcToJquKkcNEs+MUeUFmsx1eRHi0a7fQgthz6FahDMS439b4B4
	TIemfBahK6HqtHBzZ2noyDU8gfrY/hiOVaWBhlW+Y7yXiR9V7i/6ju5mkWb1b4qV
	hL4CZI5QyDrF6Y1zrTJLbAvBmgYemDgc6hWx1duq4=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:mime-version:in-reply-to:references:date
	:message-id:subject:from:to:content-type; s=default; bh=DaakeDTO
	tg4yG4bSFkrRYikrcS8=; b=aIjsCORoU3llN4SDpZeqWwzvs+vH8AGEC2KMJc3q
	EWYgSryd+9eaw9VwbEPd03Ugcjs2Smfmse5n+5B/0T5UojFUGckNr1B/nEIBZXVQ
	jfrXDVwjYIZ34rr58N5QQcG+TJtBaiFyn3bUe5HHkZpdh02dlqsJc9Rr3qOBNDe9
	uu4=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2
X-HELO: mail-qa0-f52.google.com
MIME-Version: 1.0
X-Received: by 10.229.179.5 with SMTP id bo5mr8015475qcb.21.1390001143581; Fri, 17 Jan 2014 15:25:43 -0800 (PST)
In-Reply-To: <52D9A589.2050102@redhat.com>
References: <831845.98759.bm@smtp116.sbc.mail.ne1.yahoo.com>	<52D55D96.8070407@redhat.com>	<946338.89157.bm@smtp116.sbc.mail.ne1.yahoo.com>	<52D98E1D.8010907@redhat.com>	<52D9A589.2050102@redhat.com>
Date: Fri, 17 Jan 2014 18:25:43 -0500
Message-ID: <CAG9p0OSzOUWf=VyK+f+Q2wBYUd2yiz9uwbtQOkTMOc4arvcMXg@mail.gmail.com>
Subject: Re: fork() + file descriptor bug in 1.7.27(0.271/5/3) 2013-12-09 11:54
From: Lord Laraby <lord.laraby@gmail.com>
To: Cygwin Mailing List <cygwin@cygwin.com>
Content-Type: text/plain; charset=UTF-8
X-IsSubscribed: yes

Well, that is some interesting stuff. So, in POSIX, the child gets the
same FD in the same place and it is actually a second reference to the
kernels open file table. The same entry as the parent uses (via FD) to
determine the offset, flags, etc.
That would explain why the child calling exit() flushes the parents
input file as well as its own. They are the same file as far as the
kernel maintains it. As well, the buffering on the file determines how
much of the file the parent loses when the child flushes the buffer.
For a standard 4K buffer, if the input file is less than a full
buffer, the parent would see an EOF even though it had last read one
or more short lines of input. Now, if the input file had been
unbuffered at the kernel level, this would not cause the problem we
see. Perhaps we'd lose the next character read and buffered.
I must remember to call _exit() when I use fork.
The above makes sense when you consider stdout and stderr, for they
keep the parent and child from clobbering each other's output. I'm not
so sure it's useful on input files so much, though. JMHO.

LL

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

