#!/usr/bin/env perl use warnings; use strict; print "[$$] Starting main program\n"; my @childs; for my $count (1..3) { if ( my $pid = fork ) { # parent print "[$$] child pid is $pid\n"; push @childs, $pid; } elsif ( defined $pid ) { # child sub1($count); exit 0; } else { die "[$$] couldn't fork: $!" } } for my $pid (@childs) { my $tmp = waitpid($pid, 0); print "[$$] done with pid $tmp\n"; } print "[$$] End of main program\n"; sub sub1 { my $num = shift; print "[$$] started child process for $num\n"; sleep $num; print "[$$] done with child process for $num\n"; return $num; } #### [7685] Starting main program [7685] child pid is 7686 [7686] started child process for 1 [7685] child pid is 7687 [7687] started child process for 2 [7685] child pid is 7688 [7688] started child process for 3 [7686] done with child process for 1 [7685] done with pid 7686 [7687] done with child process for 2 [7685] done with pid 7687 [7688] done with child process for 3 [7685] done with pid 7688 [7685] End of main program