Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Syntactic Confectionery Delight
 
PerlMonks  

How to run the child process parellel to the parent process with fork

by srlbharu (Acolyte)
on Mar 15, 2013 at 11:11 UTC ( #1023667=perlquestion: print w/ replies, xml ) Need Help??
srlbharu has asked for the wisdom of the Perl Monks concerning the following question:

I have created the child process by using fork as shown because I need to run the child process and parent should not wait for child process's completion. It should be run as a independent process but spawned.

unless(fork){ print "Child Process created\n"; exit 0; } foreach $i (1..3) { print "Parent Process Created\n"; }

I need to run the child process parallel to the parent. but from the code shown parent is executed first and then child. Please let me know what am i doing wrong and If this is not the proper way to run the child process parallel to parent.

Comment on How to run the child process parellel to the parent process with fork
Download Code
Re: How to run the child process parellel to the parent process with fork
by hdb (Parson) on Mar 15, 2013 at 11:33 UTC

    From your example, you will not know the order of the processes. If you replace (1..3) with (1..1000), ie make the parent run longer, you will find that the printout from the child is somewhere in the middle. From which I conclude that the processes run in parallel.

    At least this is what happens on my Windows machine with ActivePerl.

    You might also want to switch on autoflush to make sure the output from print reaches the screen when it is executed: $|=1;

      Sorry for my wrong assumtion. It is proven both processes will be running simultanously. Thanks to all

Re: How to run the child process parellel to the parent process with fork
by nagalenoj (Friar) on Mar 15, 2013 at 11:37 UTC
    Hi,

    When a process is forked, new process(child) and the existing process(parent) both run simultaneously. It is not like one after another. It is not like, what you said("but from the code shown parent is executed first and then child.")

    Can you tell what is the output and your expected output?

Re: How to run the child process parellel to the parent process with fork
by bart (Canon) on Mar 15, 2013 at 12:01 UTC
    What you do wrong? You don't have $| on, so the output is buffered.

    Also, your code executes very shortly. Too short to reliably state which comes first.

    Let the run for a while longer. Use else to have the child and parent run side by side.

    $| = 1; use Time::HiRes qw(sleep); # allows sleep in fractions of seconds if(fork){ foreach my $i (1 .. 10) { print "I am the parent\n"; sleep 1; } print "parent exiting now.\n"; } else { foreach my $i (1 .. 30) { print "I am the child\n"; sleep 0.3; } print "Bye.\n"; }
Re: How to run the child process parellel to the parent process with fork
by bojinlund (Curate) on Mar 15, 2013 at 23:04 UTC

    This shows two other alternatives of parallel processing (not explicitly using fork). Save this as MyChameleon.pm:

    package MyChameleon; use strict; use warnings; use threads; $| = 1; # autoflush STDOUT my $used_as_script = not caller(); if ($used_as_script) { print "Start script\n"; my $thread_to_do = threads->create( \&to_do, 'thread', 5 ); my $thread_sys = threads->create( sub { system $^X, "-MMyChameleon", "-e", "MyChameleon::to_do('sy +stem')"; } ); to_do( 'script', 3 ); $thread_to_do->join; $thread_sys->join; exit; } sub to_do { my ( $tag, $times ) = @_; $times //= 10; print "$tag: Hello World!\n"; map { print "$tag ($_)\n"; sleep 1 } ( 1 .. $times ); print "$tag: is done\n"; } !!1;

    Run it like this: “perl MyChameleon.pm”.

    See also How a script becomes a module

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023667]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-04-21 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (492 votes), past polls