Forking question

2012-08-10 Thread G M

Hi,

I'm working on a forking process and I need a way to check if any of the 
processes have failed and if they have restart them.  Can anyone point me in 
the right direction to a tutorial or explanation of how to do this?


Thanks in advance,

Graeme
  

Re: Forking question

2012-08-10 Thread Shlomi Fish
Hi Graeme,

On Fri, 10 Aug 2012 10:10:10 +0100
G M iamnotregiste...@hotmail.com wrote:

 
 Hi,
 
 I'm working on a forking process and I need a way to check if any of
 the processes have failed and if they have restart them.  Can anyone
 point me in the right direction to a tutorial or explanation of how
 to do this?

How do you expect to know when a process has failed? Will it die in this
case? If so, you'll receive a SIGCHLD signal and can trap it and handle it
appropriately. There's some rudimentary coverage of that on 
http://perldoc.perl.org/perlipc.html (though I can warn you that the perl code 
is
no longer considered idiomatic - see 
http://perl-begin.org/tutorials/bad-elements/ ),
and you may also wish to look at 
https://metacpan.org/module/Parallel::ForkManager for
a useful wrapper over the nitty-gritty details.

If that's not what you want, then you'll have to explain.

Regards,

Shlomi Fish

 
 
 Thanks in advance,
 
 Graeme
 


-- 
-
Shlomi Fish   http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://shlom.in/sw-quality

The KGB used to torture their victims by having them look at scrolling XSLT
code.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking question

2012-08-10 Thread pangj

 Hi,

 I'm working on a forking process and I need a way to check if any of the
 processes have failed and if they have restart them.  Can anyone point me
 in the right direction to a tutorial or explanation of how to do this?


Or use a server framework like POE which manages forking for you.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: Forking question

2012-08-10 Thread David Christensen

On 08/10/12 02:10, G M wrote:

I'm working on a forking process and I need a way to check if any of the 
processes have failed and if they have restart them.  Can anyone point me in 
the right direction to a tutorial or explanation of how to do this?


Network Programming with Perl by Lincoln D. Stein covers forking (and 
threading):


http://www.pearsonhighered.com/educator/product/Network-Programming-with-Perl/9780201615715.page


David

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Forking question.

2002-08-26 Thread Chad Kellerman

Good morning, afternoon, night,

I have been trying to work on a script that does forking.  But
the script dies in the fork.  Here is what I have:

I push some information about a server into an array.

snip
use POSIX sys_wait_h;
my $child_limit = 1;
my $child_pids = 0;
$SIG{CHLD} = \CHILD_COUNT($child_limit);


 push @server_list, $href;

  FORK:
 {
while ( $#server_list  -1 ) {
 next if $child_pids  $child_limit;
 my $server_todo = pop @server_list;
 if (my $pid = fork) {
 # sleep 1;  # failing due to bad
subroutine? 
 #do the parent
 $child_pids++;
 next;
 }
 elsif (defined $pid) {
 # ok now the child.
 do_stuff($server_todo); #subroutine
 exit;
 }
 elsif ($! =~ /No more process/) {
 print  No more process ...sleeping\n;
 sleep 5;
 redo FORK;
 }
 else {
 die  Can't fork: $! \n;
 }
}   
 }

sub CHILD_COUNT {
my $child_limit = @_;
my $child;
$child = waitpid(-1, WNOHANG);
while ( $child  0  ( $child_pids  0)) {
$child_pids-- if ( $child_pids  0);
$child = waitpid(-1,WNOHANG);
}
}



/snip


  I get the error:
Not a subroutine reference at serverbackup.pl line 65.

Line 65 is the next if statement.

  I am at a loss here.  It seems that the more I read about this the
more I get confused.

Can anyone give me a hand with this?  Or a push in the right direction? 
It would be much appreciated.

Thanks
--chad

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Forking question.

2002-08-26 Thread Felix Geerinckx

on Mon, 26 Aug 2002 14:15:02 GMT, [EMAIL PROTECTED] (Chad
Kellerman) wrote: 

 I have been trying to work on a script that does forking. 
 But the script dies in the fork.  Here is what I have:

 [...]

 my $child_limit = 1;
 my $child_pids = 0;
 $SIG{CHLD} = \CHILD_COUNT($child_limit);

You can't have arguments here. This line should be:

$SIG{CHLD} = \CHILD_COUNT;

 [...]

 sub CHILD_COUNT {
 my $child_limit = @_;
  ^

Remove this line, so you essentially work with the my-variable
$child_limit declared at file scope at the top of your program. 

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Forking question.

2002-08-26 Thread david

you have:

 my $child_limit = 1;
 my $child_pids = 0;
 $SIG{CHLD} = \CHILD_COUNT($child_limit);

the $child_limit thingy is ignored by Perl. this means when Perl calls your
CHIL_COUNT subroutine, the value of the variable $child_limit is not passed 
in to your subroutine.

and then in the CHIL_COUNT subroutine, you have:

 sub CHILD_COUNT {
 my $child_limit = @_;

my $child_limit declares another variable, which happen to have the same 
name as the $child_limit variable declared at the beginning of your program 
but they are completely different thing. besides, i don't see you reference 
$child_limit anywhere else inside your CHIL_COUNT subroutine, what's the 
purpose of this $child_limit variable then?

 my $child;
 $child = waitpid(-1, WNOHANG);
 while ( $child  0  ( $child_pids  0)) {
 $child_pids-- if ( $child_pids  0);
 $child = waitpid(-1,WNOHANG);
 }
 }

another thing:
 while ( $#server_list  -1 ) {

this might never be true.



if you try to archive:

1. given a list of servers
2. for each individual server, fork off a different child process(but stop 
creating more child process once a certain number of them has been created) 
to handle each server.
3. the parent will keep track the number of child process and wait for them 
to finish.

then you might want to structure something like:

my @servers = ('server1','server2','server3');
my $child_max = 10; #-- allow 10 child process at once
my $child_process = 0;

$SIG{CHLD} = sub {
while((my $pid = waitpid(-1,WNOHANG))  0){
$child_process--; #-- one less child process
print $pid finished\n;
}
};

while(@servers){
if($child_process  $child_max){sleep(1);next;} #-- wait a little
my $pid = fork;
next unless(defined $pid); #-- fork success?
my $server = pop @servers;
if($pid){
#-- parent
$child_process++; #-- one more process
print $pid created.\n;
}else{
#-- child
handle_server($server);
print $server done. about to exit\n;
exit;
}
}

i hope that will help you a little.

david

Chad Kellerman wrote:

 Good morning, afternoon, night,
 
 I have been trying to work on a script that does forking.  But
 the script dies in the fork.  Here is what I have:
 
 I push some information about a server into an array.
 
 snip
 use POSIX sys_wait_h;
 my $child_limit = 1;
 my $child_pids = 0;
 $SIG{CHLD} = \CHILD_COUNT($child_limit);
 
 
  push @server_list, $href;
 
   FORK:
  {
 while ( $#server_list  -1 ) {
  next if $child_pids  $child_limit;
  my $server_todo = pop @server_list;
  if (my $pid = fork) {
  # sleep 1;  # failing due to bad
 subroutine?
  #do the parent
  $child_pids++;
  next;
  }
  elsif (defined $pid) {
  # ok now the child.
  do_stuff($server_todo); #subroutine
  exit;
  }
  elsif ($! =~ /No more process/) {
  print  No more process ...sleeping\n;
  sleep 5;
  redo FORK;
  }
  else {
  die  Can't fork: $! \n;
  }
 }
  }
 
 sub CHILD_COUNT {
 my $child_limit = @_;
 my $child;
 $child = waitpid(-1, WNOHANG);
 while ( $child  0  ( $child_pids  0)) {
 $child_pids-- if ( $child_pids  0);
 $child = waitpid(-1,WNOHANG);
 }
 }
 
 
 
 /snip
 
 
   I get the error:
 Not a subroutine reference at serverbackup.pl line 65.
 
 Line 65 is the next if statement.
 
   I am at a loss here.  It seems that the more I read about this the
 more I get confused.
 
 Can anyone give me a hand with this?  Or a push in the right direction?
 It would be much appreciated.
 
 Thanks
 --chad

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Forking question.

2002-08-26 Thread david

David wrote:
 another thing:
 while ( $#server_list  -1 ) {
 
 this might never be true.

i cut and paste too much! please ignore that!

david


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Forking question.

2002-08-26 Thread Felix Geerinckx

on Mon, 26 Aug 2002 18:21:16 GMT, David wrote:

 another thing:
 while ( $#server_list  -1 ) {
 
 this might never be true.

It's true for as long as there are elements in @server_list.
It's equivalent to the (imho more readable)

while (@server_list) {
# do stuff (shifting or popping)
}


-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]