fork后,如何让父进程在子进程后结束?

fork后,如何让父进程在子进程后结束?

fork后,如何让父进程在子进程后结束?
fork 了10个子进程,想在父进程中收割10个子进程后退出
下面的代码怎么父进程在子进程前就结束了.

[Copy to clipboard] [ - ]
CODE:
#!/usr/bin/perl
use strict;
use POSIX qw(WNOHANG setsid);
my $maxchild=10;
my @child_pid;
foreach my $item (1..$maxchild) {
        defined (my $child=fork()) or die "Can't fork:$!";
        if ($child==0){
                sleep 1;
                push @child_pid,$$;
        }
}
foreach(@child_pid){
        print "Wait:$_\n\n";
        waitpid($_,0);
}

You need to check to make sure child_pid is done, and probably you want to check how pid is completed, good or bad?

something like this:

$exitpid = wait;
if ($? != 0) {
   # pid exits poorly, done something
}
else {
   $maxchild --;
}

if ($maxchild == 0) {
   print "all done\n";
}
$SIG{CHLD}
please catch this signal in your parent process,
if all childs done it's job , the parent can die..