The following example can help you.
The main process forks a number of children with logical IDs between 0 and $np. The processes concurrently gain exclusive access to a file using a lock.
lhp@nereida:~/Lperl/src/perl_networking/ch2$ cat -n flock.pl 1 #!/usr/bin/perl -w 2 use strict; 3 use Fcntl qw(:DEFAULT :flock); 4 use POSIX qw(WNOHANG); 5 $| = 1; 6 7 local $SIG{CHLD} = sub { 8 while (my $kid = waitpid(-1, WNOHANG) > 0) {} 9 }; 10 11 sub create_child { 12 my ($id, $task) = splice @_, 0, 2; 13 my $pid; 14 15 return $pid if $pid = fork(); 16 die "Cannot fork $!" unless defined $pid; 17 $task->($id, @_); # do something 18 exit; 19 } 20 21 sub parfor { 22 my $LAST = shift; 23 my $task = shift; 24 my @pid; 25 26 $pid[0] = $$; 27 $pid[$_] = create_child($_, $task, @_) for 1..$LAST; 28 return @pid; 29 } 30 31 sub task { 32 my $id = shift; 33 my $fn = shift; 34 35 sleep(int(rand(2))); 36 open my $f, "+<$fn" or die "Can't open $fn. $!\n"; 37 flock($f, 2); 38 seek $f, 0, 0; 39 my $num = <$f>; 40 warn "Process $id reads $num.\n"; 41 seek $f, 0, 0; 42 my $s = $num+$id; 43 warn "Process $id writing $s.\n"; 44 print $f $s; 45 close($f); 46 exit; 47 } 48 49 #main 50 my $np = shift || 3; 51 my $fn = shift || "sync.txt"; 52 open my $f, "> $fn" or die "Can't open file $fn. $!\n"; 53 print $f 0; 54 close($f); 55 &parfor($np, \&task, $fn); 56 do {} while wait > 0;
The answer to your question is in line 56

Hope it helps

Casiano


In reply to Re: How the parent process knows that all its child process has finished execution? by casiano
in thread How the parent process knows that all its child process has finished execution? by kishore036

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.