Hi,
My idea is that the parent pop's the stack right after the fork, so that the value the child process got is no longer in the stack. Is this the right thing to do, or is there a "right thing to do"?
I'm not all that familiar with fork() (I spend most my time on Win32). But wasn't Fastolfe's suggestion basically what you asked for? except more efficient.
The while (host = pop array_of_hosts)does the pop before the child is created, all the child needs to know is the value of $host, it doesn't need to pop, because the parent has already taken care of it.
Update: I've just read up a bit on fork. If I were you, I would develop Merlyn's snippet. It's a little less pseudocode than Fastolfe's but basically the same. You could add a sleep() in there for the parent so you don't spawn too many children and overload the CPU which concerned geektron.
To just explain Merlyn's a bit further (and alter it a bit to utilise pop):
while (my $host = pop @arrayofhosts) { # $host is no longer in the
+array
defined (my $pid = fork) or die "Cannot fork: $!"; # self-explanato
+ry
unless ($pid) {
# only the child will execute this code from here ...
# do some things with $host
exit 0; # very important!!! don't let it get past here
# to here. At this point, the child process will terminate ...
}
# The parent continues here after forking the child
# here it can do what it wants.
# you can rest here a while so you don't have too many zombies
sleep (10);
# now do some more things before forking the next child process
}
| [reply] [d/l] |