Re: Fork (not the kind you eat with)

2001-06-29 Thread Chas Owens

On 29 Jun 2001 09:52:16 +0100, Pierre Smolarek wrote:
> Small problem with the below code how can you control the MAX amount of
> children you have?
> 


Well, that is harder.  One way (and there are probably better ones)
would be to work with many subsets of the machines getting the pids for
each set and then waiting for them [the children] to finish before
forking off more.

NOTE: This code has not been tested; there may be typos or logic bugs in
it.


use constant MAX => 10;

my @machines = get_machines();

while (@machines) { #while there are still machines left
my @pids;
foreach my $machine (splice(@machines, 0, MAX)) {
push @pids, fork;
if ($pids[-1] == 0) { #I am a child
check_machine($machine);
} elsif (not $pids[-1]) {
#if parent then $pid = process id, if error then
#$pids[-1] = undef
pop @pids; #get rid of the undef
error();
}
}
#reap the children
foreach my $pid (@pids) {
waitpid $pid, 0; #wait for each fork to finish
}
}

 
--
Today is Setting Orange, the 34th day of Confusion in the YOLD 3167
All Hail Discordia!





Re: Fork (not the kind you eat with)

2001-06-29 Thread Pierre Smolarek

Small problem with the below code how can you control the MAX amount of
children you have?

- Original Message -
From: "Chas Owens" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 28, 2001 7:07 PM
Subject: Re: Fork (not the kind you eat with)


> A parent can fork as many times as it wants to (for that matter a child
> could fork as well).  So your code would look like this:
>
> $SIG{CHLD} = "IGNORE"; #works on unix platforms, auto reaps children
> foreach $machine (get_machines()) {
> $pid = fork;
> if ($pid == 0) { #I am a child
> check_machine($machine);
> } elsif (not $pid) {
> #if parent then $pid = process id, if error then
> #$pid = undef
> error();
> }
> }
>
> On 28 Jun 2001 17:03:39 +0100, Pierre Smolarek wrote:
> > The one thing in perl that gets my head all confused is fork.
> >
> > Can someone point me in the right direction (be it book, website, or
kind
> > enough to offer code).
> >
> > I need to make a script that has to check 16000 servers in around 6
minutes.
> > My rough maths works out that 44 checks a second are needed. Each server
> > check takes about 0.5 seconds to return, so the best bet is to fork each
> > individual check, the result of which gets added to mysql so no need to
have
> > a conversation going on between child and parent. Idealy i would like to
> > control the max amount children i have to, say, around 50.
> >
> > Any help would be greatful.
> >
> > (I have the cook book open but only seems to talk about a single parent
> > child pair..?!)
> >
> >
> > Pierre.
> >
> >
> --
> Today is Prickle-Prickle, the 33rd day of Confusion in the YOLD 3167
> Keep the Lasagna flying!
>




Re: Fork (not the kind you eat with)

2001-06-28 Thread Pierre Smolarek

Thanks a lot and thanks to the guy who mentioned those book, amazon has
my order :)

this code was exactly what i needed...


- Original Message -
From: "Chas Owens" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 28, 2001 7:07 PM
Subject: Re: Fork (not the kind you eat with)


> A parent can fork as many times as it wants to (for that matter a child
> could fork as well).  So your code would look like this:
>
> $SIG{CHLD} = "IGNORE"; #works on unix platforms, auto reaps children
> foreach $machine (get_machines()) {
> $pid = fork;
> if ($pid == 0) { #I am a child
> check_machine($machine);
> } elsif (not $pid) {
> #if parent then $pid = process id, if error then
> #$pid = undef
> error();
> }
> }
>
> On 28 Jun 2001 17:03:39 +0100, Pierre Smolarek wrote:
> > The one thing in perl that gets my head all confused is fork.
> >
> > Can someone point me in the right direction (be it book, website, or
kind
> > enough to offer code).
> >
> > I need to make a script that has to check 16000 servers in around 6
minutes.
> > My rough maths works out that 44 checks a second are needed. Each server
> > check takes about 0.5 seconds to return, so the best bet is to fork each
> > individual check, the result of which gets added to mysql so no need to
have
> > a conversation going on between child and parent. Idealy i would like to
> > control the max amount children i have to, say, around 50.
> >
> > Any help would be greatful.
> >
> > (I have the cook book open but only seems to talk about a single parent
> > child pair..?!)
> >
> >
> > Pierre.
> >
> >
> --
> Today is Prickle-Prickle, the 33rd day of Confusion in the YOLD 3167
> Keep the Lasagna flying!
>




Re: Fork (not the kind you eat with)

2001-06-28 Thread Peter Scott

At 05:03 PM 6/28/01 +0100, Pierre Smolarek wrote:
>The one thing in perl that gets my head all confused is fork.
>
>Can someone point me in the right direction (be it book, website, or kind
>enough to offer code).

"Network Programming with Perl", by Lincoln Stein (Addison-Wesley, 2001) is 
very good on this topic.  If you want the full skinny on what's going on 
underneath, "Advanced Programming in the UNIX Environment" by W. Richard 
Stevens (also Addison-Wesley).
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com




Re: Fork (not the kind you eat with)

2001-06-28 Thread Chas Owens

A parent can fork as many times as it wants to (for that matter a child
could fork as well).  So your code would look like this:

$SIG{CHLD} = "IGNORE"; #works on unix platforms, auto reaps children
foreach $machine (get_machines()) {
$pid = fork;
if ($pid == 0) { #I am a child
check_machine($machine);
} elsif (not $pid) { 
#if parent then $pid = process id, if error then 
#$pid = undef
error();
}
}

On 28 Jun 2001 17:03:39 +0100, Pierre Smolarek wrote:
> The one thing in perl that gets my head all confused is fork.
> 
> Can someone point me in the right direction (be it book, website, or kind
> enough to offer code).
> 
> I need to make a script that has to check 16000 servers in around 6 minutes.
> My rough maths works out that 44 checks a second are needed. Each server
> check takes about 0.5 seconds to return, so the best bet is to fork each
> individual check, the result of which gets added to mysql so no need to have
> a conversation going on between child and parent. Idealy i would like to
> control the max amount children i have to, say, around 50.
> 
> Any help would be greatful.
> 
> (I have the cook book open but only seems to talk about a single parent
> child pair..?!)
> 
> 
> Pierre.
> 
> 
--
Today is Prickle-Prickle, the 33rd day of Confusion in the YOLD 3167
Keep the Lasagna flying!





Fork (not the kind you eat with)

2001-06-28 Thread Pierre Smolarek

The one thing in perl that gets my head all confused is fork.

Can someone point me in the right direction (be it book, website, or kind
enough to offer code).

I need to make a script that has to check 16000 servers in around 6 minutes.
My rough maths works out that 44 checks a second are needed. Each server
check takes about 0.5 seconds to return, so the best bet is to fork each
individual check, the result of which gets added to mysql so no need to have
a conversation going on between child and parent. Idealy i would like to
control the max amount children i have to, say, around 50.

Any help would be greatful.

(I have the cook book open but only seems to talk about a single parent
child pair..?!)


Pierre.