From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1763 invoked by alias); 28 Aug 2012 07:06:47 -0000 Received: (qmail 1629 invoked by uid 22791); 28 Aug 2012 07:06:43 -0000 X-SWARE-Spam-Status: No, hits=-0.6 required=5.0 tests=AWL,BAYES_20,KHOP_THREADED,RP_MATCHES_RCVD,URI_HEX X-Spam-Check-By: sourceware.org Received: from postman.abacus.ch (HELO postman.abacus.ch) (77.74.4.142) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 28 Aug 2012 07:06:24 +0000 Received: from localhost (lena.abanet.ch [77.74.4.230]) by postman.abacus.ch (Postfix) with ESMTP id 5989B29889F for ; Tue, 28 Aug 2012 09:06:22 +0200 (CEST) Received: from localhost ([77.74.4.142]) by localhost (lena.abanet.ch [77.74.4.230]) (amavisd-new, port 10026) with ESMTP id rJzIdGpvSIXR for ; Tue, 28 Aug 2012 09:06:22 +0200 (CEST) Received: from postman.abacus.ch ([127.0.0.1]) by localhost (postman.abacus.ch [127.0.0.1]) (amavisd-new, port 10026) with LMTP id 5JP63POckwId for ; Tue, 28 Aug 2012 09:06:22 +0200 (CEST) Received: from [172.16.17.126] (geldmacher-pc.int.abacus.ch [172.16.17.126]) by postman.abacus.ch (Postfix) with ESMTP id 2BA3729889A for ; Tue, 28 Aug 2012 09:06:22 +0200 (CEST) Message-ID: <503C6DEB.3090101@abacus.ch> Date: Tue, 28 Aug 2012 08:12:00 -0000 From: Wolf Geldmacher User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: cygwin@cygwin.com Subject: Re: fork() and file descriptors with un-flushed output References: <1346135219441-92349.post@n5.nabble.com> In-Reply-To: <1346135219441-92349.post@n5.nabble.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-IsSubscribed: yes Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2012-08/txt/msg00609.txt.bz2 This is not a bug - it's a feature ;-) The "issue" you are describing is in fact the standard behaviour expected of fork() in any unix/posix compliant implementation. From the fork man page on Linux: > ... > fork() creates a new process by duplicating the calling process. The > new process, referred to as the child, is an exact duplicate of the > calling process, referred to as the parent, > ... and yes, "exact duplicate" includes all data in buffers not yet flushed. The difference in behaviour when you run your program from the terminal vs. from Emacs stems from the "intelligence" built into the stdio library that looks at type of the file a stream is connected to and automagically turns on full buffering if it is not connected to a terminal in order to optimize performance. To avoid the duplication of data you can either explicitly turn off buffering with setbuf() (and pay the associated performance penalty), fflush() your open files before you fork (usually the easiest to implement), or revert to the use of the basic OS functions open(), read(), write(), close() (useful for special cases when not much of stdio is needed - make sure you don't mix the two). Cheers, Wolf On 28.08.2012 08:26, thoni56 wrote: > Maybe this is an FAQ but I could not find it in it ;-) or in the lists I > searched: > > In cygwin, when you fork() process shares file descriptors. If there happens > to be unflushed output in such a shared file descriptor buffer, would that > be output by both processes? > > I have some empirical evidence to support this theory. I support cgreen, a C > unit test and mock framework, which runs every test case in its own > processes using fork(). > > For many years I have seen the effect that when running in a command window > every thing works as expected, But running in Emacs created multiple > outputs. That has not bothered me that much but know I implemented some > further output routines in the reporting code, and everything just blew up! > > The test case is run in a separate processes using fork() which then > messages back and then dies. The output from the runner (parent process) > written to the file before the fork() is then output twice. > > This behaviour changed to the expected (only printed once) if a fflush() was > added after the printf() in the parent process before the fork. I'm > suspecting this happens because of unflushed output in the file buffer which > is shared by the two processes, first flushed when the child dies, then > flushed by the parent at some point, not only duplicating output, but also > garbling it. > > Is this a known behaviour? Unavoidable in cygwin? (Obviously not, if I'm on > the right track with my guesswork...) If it is a bug, will it be fixed?¨ > > > > -- > View this message in context: http://cygwin.1069669.n5.nabble.com/fork-and-file-descriptors-with-un-flushed-output-tp92349.html > Sent from the Cygwin list mailing list archive at Nabble.com. > > -- > 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 > -- 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