note
enoch
Okay, this may be difficult to follow along to; but I will do my best.<BR><BR>
When you fork, your children processes get an exact replica of the process. So, your first child gets spawned. Now, it has a copy of <CODE>$i</CODE> which has the value zero. So, it is spawned, it runs the <CODE>phork()</CODE> subroutine, and it returns to the <CODE>for</CODE> loop with a <CODE>$i</CODE> of 0. So, it beings to execute the loop; and your first child process forks its own child and that child (the first child of the first child of the parent) is spawned and it gets a copy of the memory block who has a <CODE>$i</CODE> of 1. This stuff happens some more with the second child of the parent and so on. What you want is something along these lines: (<B>warning</B>: untested)
<CODE>
#!/usr/bin/perl -w
use strict;
my $childLimit = shift @ARGV; #get total number of children
my @pids = (); #array of pids of children
for(my $i = 0; $i < $childLimit; $i++)
{
if( !( $pids[$i] = fork() ) )
{
# in the child
callChildSub();
exit(0); # exit from the child process IMPORTANT
}
}
foreach my $pid (@pids)
{
waitpid $pid, 0;
}
sub callChildSub()
{
print "I am the child with pid $$.\n";
}
</CODE>
Of course, that needs some error-checking and such, but that should get you started.<BR><BR>
Jeremy
88513
88600