This is the mail archive of the cygwin@cygwin.com 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]

Re: fork()


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@cygwin.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/


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