Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sources DOT redhat DOT com Delivered-To: mailing list cygwin AT sources DOT redhat DOT com Date: Mon, 16 Jul 2001 15:20:27 +0200 From: Corinna Vinschen To: CygWin Subject: Re: fork() Message-ID: <20010716152027.U25442@cygbert.vinschen.de> Mail-Followup-To: CygWin References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: ; from ronald@landheer.com on Mon, Jul 16, 2001 at 02:59:18PM +0200 On Mon, Jul 16, 2001 at 02:59:18PM +0200, Ronald Landheer wrote: > Hello all, > > Being used to DJGPP - not CygWin - I may have lost my hand on the forking > idea a while back, so I thought I'd try the following piece of code: > > -- BEGIN CODE SNIPLET -- > #include > #include > > int main(void) { > int rVal, i; > rVal = fork(); > if (!rVal) { > for (i = 0; i < 100; i++) printf("0"); > } else { > for (i = 0; i < 100; i++) printf("1"); > } // if > printf("\n"); > > return(0); > } // main() > --- END CODE SNIPLET --- > This compiles without warnings or errors (phew!) but produces an output > somewhat different than I expected: > -- BEGIN OUTPUT SNIPLET -- > 0000000000000000000000000000000000000000000000000000000000000000000000000000 > 0000000000000000000000001111111111111111111111111111111111111111111111111111 > 111111111111111111111111111111111111111111111111 > --- END OUTPUT SNIPLET --- > I'd expected the 0's and 1's to be jumbled.. Your example is wrong. The output to stdout is line buffered. That means, the actual output is generated only after the last `printf("\n"); Add a `setbuf(stdout, NULL)' right before the fork() and your little application will suddenly behave as you've expected... Corinna -- Corinna Vinschen Please, send mails regarding Cygwin to Cygwin Developer mailto:cygwin AT cygwin DOT com Red Hat, Inc. -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Bug reporting: http://cygwin.com/bugs.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/