pretty much you can just make minor changes and say
my $pid;
for my $node(@devices) {
if( $pid = fork ) {
next;
}
elsif( defined( $pid ) ) {
#do subroutines here on $node
}
else {
die "Could not fork!\n";
}
}
This way, you will spin off one process for each node in the device list.
If you have many nodes in your device list, this might not be optimal. You
might want to have each forked process work on N devices at a time...for
example:
my $N = 5;
for( my $i = 0; $i < scalar(@devices); $i += $N ) {
if( $pid = fork ) {
next;
}
elsif( defined( $pid ) ) {
#do subroutines here on $devices[i..i+$N]
}
else {
die "Could not fork!\n";
}
}
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: "Perl Discuss" <[EMAIL PROTECTED]>
Sent: Tuesday, January 29, 2002 12:33 PM
Subject: forking perl
> hello all,
> i am thinking of adding the ability to fork into a script i've written
> recently. the script acts on a list of nodes. currently it does this, one
> at a time, which could prove to be a lengthy process for longer lists.
>
> i was thinking that fork would allow me to spawn child pids and speed up
> the process. i am reading from the camel book p167, however i'm a network
> guy that somehow got involved in writing perl, so my understanding of
> forks and processes is limited.
>
> i'll skip putting in the whole of my code, but it can be summed up as:
>
> for my $node(@devices) {
>
> ##subroutines
>
> }
>
> i'm not certain where i would fork to allow for X number of child
> processes to be started at once, or whether the 'for' statement would be
> rewritten. the example i am using to get familiar with fork is:
>
> #!/usr/bin/perl -w
>
> use strict;
>
> my $i = "0"; my $pid;
>
> while ($i < 5) {
>
> FORK: {
> if ($pid=fork) {
> next;
>
> } elsif (defined $pid) {
> print "hello\n";
> exit;
>
> } else {
> die "Cant fork! : $!\n";
>
> }
>
> }
>
> $i++;
> }
>
> print "The End\n";
>
>
>
> i dont think that i am drawing the lines very well. i'm not grasping how
> fork can tie in with my existing code. any pointers are appreciated.
>
> regards -c
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]