LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-03-2010, 08:06 AM   #1
DevonB
LQ Newbie
 
Registered: Dec 2009
Posts: 27

Rep: Reputation: 1
Perl - How to do multiple child processes in parallel?


I'm looking for a way in Perl to be able to take a list of servers, ssh multiple commands to it and store the results. If I do this process serially, sometimes one server will hang the whole script and if it doesn't, it still takes hours to complete.

I'm thinking what I need to do is make a parent loop that calls out a separate process that passes the server name to the child sub process and then executes all the commands I have defined in its own process. If one server 'hangs', at least that won't stop the script from doing all the other servers in the list.

I'm guessing using the fork() command would serve me best, however, all the online descriptions I have found have been vague at best. Any suggested reading that gives a better description, or anyone have a better suggestion? I've done this type of thing plenty on Windows with pushing psexec out in a loop, just not Unix.

Devon
 
Old 09-03-2010, 05:11 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by DevonB View Post
I'm looking for a way in Perl to be able to take a list of servers, ssh multiple commands to it and store the results. If I do this process serially, sometimes one server will hang the whole script and if it doesn't, it still takes hours to complete.

I'm thinking what I need to do is make a parent loop that calls out a separate process that passes the server name to the child sub process and then executes all the commands I have defined in its own process. If one server 'hangs', at least that won't stop the script from doing all the other servers in the list.

I'm guessing using the fork() command would serve me best, however, all the online descriptions I have found have been vague at best. Any suggested reading that gives a better description, or anyone have a better suggestion? I've done this type of thing plenty on Windows with pushing psexec out in a loop, just not Unix.

Devon
When launching a process with 'system' you can launch it in the background - that's what the final '&' is for.
 
Old 09-03-2010, 08:51 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Why should he launch with system(), if all he needs is a SSH connection? system() launches a shell, which is a waste. DevonB,, do you understand the semantics of the fork + exec process, in general? If no, then you can find quite a lot of literature online to explain it. Ask back here for help with the parts that aren't clear.
In Perl, the usage is almost identical to what you would do in C, only easier (like most things in Perl vs.C).
The basic premise is that you can create an arbitrary number of child processes (using fork()), and each of them can exec() another process, thereby turning the child into a different program. That's how all processes are launched in Unix/Linux.

To get you started:
Code:
#	When we launch child processes, we don't care when they die; we
#	just don't want their corpses littering the process table...
$SIG{CHLD} = 'IGNORE';

my $kidPid = fork();
if( $kidPid ){  # I'm the parent
   print "Process launched....\n";
}
else{           # I'm the child: my child PID is zero; I don't have a child
   exec "/usr/bin/ssh", "username@123.123.123.123";
}
You can put this in a loop, launching as many child processes as you need.

--- rod.

Last edited by theNbomr; 09-03-2010 at 08:59 PM.
 
Old 09-03-2010, 09:04 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by theNbomr View Post
Why should he launch with system(), if all he needs is a SSH connection? system() launches a shell, which is a waste. ...
Well, it really depends how the whole problem is solved. I solve it using 'system', creating a wrapper script and log file per job, and monitoring the log file contents. The point is debuggability, especially on not very reliable networks.
 
Old 09-03-2010, 10:15 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Still, if one exists on forking, there is a convenient module: http://search.cpan.org/~rybskej/forks-0.34/lib/forks.pm .
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stopping both parent child processes in perl matt007 Programming 1 09-30-2009 02:46 AM
Multiple Perl Sockets + Child Processes (fork) Reion Programming 3 10-13-2008 07:17 PM
Apache child processes GavB Linux - Newbie 3 08-12-2007 05:32 PM
forking 7 child processes ianomc Programming 5 11-07-2004 01:33 PM
parent and child processes skora Programming 5 11-02-2003 11:41 AM


All times are GMT -5. The time now is 04:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration