Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:38:24+00:00 2026-05-24T04:38:24+00:00

First things first, I’m using cygwin version 1.7.1 on a Windows 7 box. Code

  • 0

First things first, I’m using cygwin version 1.7.1 on a Windows 7 box. Code was compiled with gcc and ran from a bash prompt. Here goes:

I was researching how fork() and exec() work so I was checking out wikipedia. There I found the following straightforward C code for some fork-on-fork action:

#include <stdio.h>   /* printf, stderr, fprintf */
#include <unistd.h>  /* _exit, fork */
#include <stdlib.h>  /* exit */
#include <errno.h>   /* errno */

int main(void)
{
   pid_t  pid;

   /* Output from both the child and the parent process
    * will be written to the standard output,
    * as they both run at the same time.
    */

   pid = fork();
   if (pid == -1)
   {
      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
   }

   if (pid == 0)
   {
      /* Child process:
       * When fork() returns 0, we are in
       * the child process.
       * Here we count up to ten, one each second.
       */
      int j = 0;
      for (j = 0; j < 10; j++)
      {
         printf("child: %d\n", j);
         sleep(1);
      }
      _exit(0);  /* Note that we do not use exit() */
   }
   else
   {
      /* Parent process:
       * When fork() returns a positive number, we are in the parent process
       * (the fork return value is the PID of the newly created child process).
       * Again we count up to ten.
       */

      int i = 0;
      for (i = 0; i < 10; i++)
      {
         printf("parent: %d\n", i);
         sleep(1);
      }
      exit(0);
   }
}

Now when I compile and run it a few times, I seem to get unpredictable behavior… sometimes it runs as expected, sometimes it includes extra newline characters to stdout, sometimes it omits newline characters to std out. Here is a sample of the output:

user@HAL10000 ~/c++/sandbox/src
$ gcc fork_and_stuff.c -o fork_and_stuff

user@HAL10000 ~/c++/sandbox/src
$ ./fork_and_stuff.exe
parent: 0child: 0
parent: 1child: 1
parent: 2child: 2

parent: 3child: 3
parent: 4child: 4
parent: 5child: 5

child: 6
parent: 6
child: 7
parent: 7
child: 8
parent: 8
child: 9
parent: 9

user@HAL10000 ~/c++/sandbox/src
$ ./fork_and_stuff.exe
parent: 0
child: 0
parent: 1
child: 1
parent: 2
child: 2
parent: 3
child: 3
parent: 4
child: 4
parent: 5
child: 5
parent: 6
child: 6
parent: 7
child: 7
parent: 8
child: 8
parent: 9
child: 9

user@HAL10000 ~/c++/sandbox/src
$ ./fork_and_stuff.exe
parent: 0child: 0

parent: 1child: 1

parent: 2child: 2

parent: 3child: 3

parent: 4child: 4

child: 5
parent: 5
parent: 6child: 6

parent: 7child: 7

child: 8parent: 8

parent: 9child: 9

That is some spooky looking output. Is my computer haunted? If so, by what? And how might I exorcise it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:38:25+00:00Added an answer on May 24, 2026 at 4:38 am

    This is normal behvior for fork(). Your code is causing a really nice scheduler interaction. This has, in fact, been used as a low quality RNG.

    Calling fflush() might well decrease the probability but will not actually remove the chance of it happening.

    Your code should do what you want most of the time if you add usleep(500000); to the top of one side; however this behavior should not be depended on.

    The intent of fork() is to create two independent processes. There are standard inter-processing locking mechanisms available to permit your code to work; however using them is almost always an error. There is a reason for the standard UNIX rule of “If a program has nothing surprising to say, it should say nothing.” This allows them to run as background processes trivially.

    Incidentally, you set up something very like the trick we use to demonstrate the unpredictableness of the scheduler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First things first. This application is an ASP.NET application using windows authentication. Situation: The
First things first, here is a little snippet code to help explain my problem:
first things first; I am writing a little LUA-Ide in C#. The code execution
I've started using blocks, and one of the first things I encountered is an
I've installed PowerShell recently and one of the first things I started looking for
Everything is an object was one of the first things I learned about Ruby,
I've just upgraded a native C++ project from VS2005-SP1 to VS2008-SP1 The first thing
NOTE: I am not set on using VI, it is just the first thing
First, let's get the security considerations out of the way. I'm using simple authentication
First things first, I am a Python beginner, with a typical C++/Java background for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.