TG has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to fork a program. However I seem to be experiencing some strange behavior. I found the following reference to the use of fork:

http://www.tutorialspoint.com/perl/perl_fork.htm

From where I copied the following code snippet:

#!/usr/bin/perl $pid = fork(); if( $pid == 0 ){ print "This is child process\n"; print "Chile process is existing\n"; exit 0; } print "This is parent process and child ID is $pid\n"; print "Parent process is existing\n"; exit 0; #This will produce following result This is parent process and child ID is 16417 Parent process is existing This is child process Chile process is existing
The expected output is shown above and is what I expected from reading the definition of fork.

However my code ran and gave the following output:

This is child process Chile process is existing This is parent process and child ID is 16417 Parent process is existing
In other words the reverse order. This is exactly the same behavior I have been experiencing.

I am running on Linux debian with perl 5.08.08

Any help would be much appreciated.

Replies are listed 'Best First'.
Re: Perl fork problem
by lidden (Curate) on Aug 13, 2008 at 06:26 UTC
    fork creates a new process and your os can choose to run them in any order it want to.
      fork creates a new process and your os can choose to run them in any order it want to.
      Nitpicking here:

      It's undefined (and probably unpredictable on most OSs) whether the child or parent process gets scheduled first after a call to fork(). On multi-processor systems they could in theory even get scheduled at the same time.

      In any case, it's misleading to call this "(running) in any order" since the processes are scheduled independently and neither is waiting on the other - for most intents and purposes they're running "simultaneously".

Re: Perl fork problem
by Skeeve (Parson) on Aug 13, 2008 at 07:20 UTC

    May I ask why you choose to use fork? Do you understand what fork is for? Would you be surprised if the output were:

    This is parent process and child ID is 16417 This is child process Chile process is existing Parent process is existing
    or even
    This is child process This is parent process and child ID is 16417 Chile process is existing Parent process is existing

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Perl fork problem
by cdarke (Prior) on Aug 13, 2008 at 13:36 UTC
    Also remember that buffering plays a part in exactly when a message appears. Setting $|=1 will make STDOUT unbuffered, and might alter the order: as it may if you print to STDERR instead.

      Unless the OP has done something we can't see, STDOUT is line-buffered by default, in which case buffering doesn't matter here at all and is not the issue.

Re: Perl fork problem
by Lawliet (Curate) on Aug 13, 2008 at 06:32 UTC
    print "Chile process is existing\n";

    Technology these days..now incorporating food.

    On a related note (related to the problem, i.e.), I get the same output as the OP (Ubuntu, 5.8.8)

    I'm so adjective, I verb nouns!

    chomp; # nom nom nom