#!/usr/bin/perl -w use strict; my @LIST = 'A'..'E'; my $i=0; $|++; # Unbuffered print on STDOUT for (@LIST) { my $pid; if ($pid = fork) { # Parent, $pid is true, is certainly defined print "Mother says $_ is in position $i, with pid $pid\n"; # waitpid serializes the kids waitpid $pid, 0; } # $pid is 0 in the child elsif (defined $pid) { print "Child says That's what I said... $_ is in position ", "$i, with pid $$\n"; exit 0; } # $pid undefined -- error else { die "Error in forking at $i: $!"; } $i++; } =pod That's what I said... A is in position 0, with pid 19041 A is in position 0, with pid 19041 B is in position 1, with pid 19042 That's what I said... B is in position 1, with pid 19042 That's what I said... C is in position 2, with pid 19043 C is in position 2, with pid 19043 D is in position 3, with pid 19044 E is in position 4, with pid 19045 That's what I said... D is in position 3, with pid 19044 That's what I said... E is in position 4, with pid 19045 =cut