delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/07/16/09:20:53

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
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 <cygwin AT cygwin DOT com>
To: CygWin <cygwin AT cygwin DOT com>
Subject: Re: fork()
Message-ID: <20010716152027.U25442@cygbert.vinschen.de>
Mail-Followup-To: CygWin <cygwin AT cygwin DOT com>
References: <NEBBJCJLGLCEEOPGCDCEIEJICIAA DOT ronald AT landheer DOT com>
Mime-Version: 1.0
User-Agent: Mutt/1.2.5i
In-Reply-To: <NEBBJCJLGLCEEOPGCDCEIEJICIAA.ronald@landheer.com>; 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 <stdio.h>
> #include <unistd.h>
> 
> 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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019