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

Hi,
I am new to fork, In below code parent wait for child to finish its job and then parents:
print "Still Processing...$cmd\n";
I am wondering while child is busy running the command is it possilble to make parent to:
print "Still Processing...$cmd\n";
my $pid = fork(); if ($pid == 0) { `$cmd`; if($?) { print "Error: $cmd\n"; } exit(0); } else { print "Still Processing...$cmd\n"; waitpid($pid, 0) }
Thanks

Replies are listed 'Best First'.
Re: fork running parent and child parallel
by GrandFather (Saint) on Sep 02, 2010 at 09:07 UTC

    Consider:

    use strict; use warnings; use POSIX ":sys_wait_h"; my $pid = fork (); if ($pid == 0) { for (1 .. 10) { sleep 1; print 'c'; } } else { while (waitpid ($pid, WNOHANG) == 0) { print 'p'; sleep 1; } } print "\nDone\n";

    Prints:

    ppcpcpccpcpcpcpcpcpc Done p Done
    True laziness is hard work