#!/usr/bin/env perl use strict; use warnings; my @dirs = (qw/dir1 dir2 dir3 dir4/); my @kids; for my $i (1..4) { my $pid = fork(); if ($pid) { # parent push @kids, $pid; print "spawned child $pid\n"; } elsif ($pid == 0) { print "I am child $$. \@kids count = $#kids. my dir is $dirs[$#kids+1]\n"; sleep 5; exit(0); } else { die "couldn.t fork: $! \n"; } print "end loop $i\n"; } for my $pid (@kids) { print "reaping $pid\n"; waitpid($pid,0); }