Hi All, I am testing the fork feature in the Linux. In below code out of 3 child parent randomly check one child and wait for it to finish and moves to another. I was wondering is it possible to make parent to report the child which finish first and another child that finish second and the third?

use strict; use warnings; use POSIX ":sys_wait_h"; handler(); sub handler { my %process = ( A => 'sleep 10 && exit 0', B => 'sleep 15 && exit 0', C => 'sleep 25 && exit 1', ); my @child_pids; foreach my $process(keys %process) { my $pid = fork; if($pid) { #Parent. # print "Launched $process | $pid\n"; push(@child_pids, {PID => $pid, PROCES +S => $process{$process}}); } elsif($pid == 0) { exec $process{$process}; die "Can't exec $process"; } else { #Parent # Fork failed. die "cant fork $!\n"; } } foreach my $href (@child_pids) { print "Waiting $href->{PID} | $href->{ +PROCESS}\n"; while (waitpid($href->{PID}, WNOHANG) +== 0) { sleep 1; } print "Failed $href->{PROCESS}\n" unle +ss($? == 0); print "Done $href->{PID} | $href->{PRO +CESS}\n"; } }

In reply to fork report child finish by order child process finish. by tart

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.