in reply to deterministic fork

Update: waitpid added to serialize the kids in example code.

First, I suggest you enable strict and warnings. Multiprocess code has plenty of capacity to confuse about lexical scopes.

The order in which child processes execute is entirely up to the OS scheduler. It cannot even be said whether the parent or the child will run first. You are correct that print buffering is an issue in general, but your code is written so that the only printing is done in the parent.

Your program is actually exiting the parent and the loop is continuing in the child. Is that what you mean to do? That is one reason your prints are getting so out of order. Here's a version which does one print in the parent and the other in the child, with the original parent doing all the forking:

#!/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

After Compline,
Zaxo