#!/usr/bin/perl -w use strict; use POSIX ":sys_wait_h"; $|++; # flush buffers my @dirs = qw ( dir1 dir2 dir3 dir4 dir5 ); my @pids; for my $dir (@dirs) { my $pid = fork(); die "Fork failed" unless defined $pid; push @pids, $pid; next if $pid; # parent returns to loop # child starts here do_dir($dir); exit; # kill child here after it has done work } print "Child pids are:\n"; print "$_\n" for @pids; # wait for all kids to finish my $kids; do{ $kids = waitpid(-1, &WNOHANG); }until $kids == -1; print "My children are all dead!\n"; print "My life is not worth living....\n"; die "I can go on no longer\nGoodbye cruel CPU\n"; sub do_dir { my $dirname = shift; # do dir stuff # we will exit on return sleep ( 2+ rand 5); # simulate processing print "Processed $dirname\n"; }