Click here to get back home

Creating a 'load simulator' by calling Perl Programs - or Forking?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Creating a 'load simulator' by calling Perl Programs - or Forking? pgodfrin 04-03-2008
Get Chitika Premium
Posted by pgodfrin on April 3, 2008, 4:02 pm
Please log in for more thread options
Greetings,

I would like to create a 'driver' (simulation) program that calls
existing perl programs, waits for them to complete and provides some
feedback. Ideally I would like to have the driver echo to the screen
that the simulation is still running and perhaps provide some
information. That may be too ambitious, but that is the basic idea.

I have a handful of perl programs that do things - read a database
(kudos to the DBI modules) and output a report, from a few lines to a
few thousand.

Question #1: What is the proper way to execute the perl programs?

I am familiar with exec() and system() and have done this:

system("sallmr1 -n50000 >/dev/null");
system("srngmr1 -sr -es -n10000 >/dev/null");
system("srngmr2 -sr -es -n10000 >/dev/null");

Which executes these three programs sequentially - or by adding '&' at
the end I can execute them at the same time. Of course the 'calling'
program cannot wait for the background tasks because of the shell
return.

Which leads me down the path of fork(). If I were to code the
following:

for(1..2)
{
         if($pid=fork)
         {
                 # parent
         }        elsif (defined $pid)
         {
                 # child
                 exec("sallmr1 -n50000 >/dev/null");
}        #        end child (elsif)
}        #        end for loop
do { $x=wait; print "$x died\n";} until $x==-1;

Then I would get a pair of runs, and wait until each child dies.

Question #2: Is there a way to echo to the screen that the wait() is
happening and provide some indication of progress?

To clarify this question, I had originally tried:
do {$x=wait; print ".";} until $x==-1;

Thinking I would get a period printed during the loop. (silly me -
wait only comes back when a child process dies...).

Of course if this is much to complicated a way to so a simulated load,
then so be it - just a plain old fork and wait works fine... And I
could always remove the /dev/null and actually see the called programs
output, which will tell me that it's still running... But I was
curious if there is a more elegant or at least interesting way of
doing this?

thanks,
phil

Posted by Joost Diepenmaat on April 3, 2008, 4:30 pm
Please log in for more thread options

> Greetings,
>
> I would like to create a 'driver' (simulation) program that calls
> existing perl programs, waits for them to complete and provides some
> feedback. Ideally I would like to have the driver echo to the screen
> that the simulation is still running and perhaps provide some
> information. That may be too ambitious, but that is the basic idea.

It may take a bit of getting used to, but POE::Wheel::Run can do this
very nicely, including monitoring of the fork'd procesesses' STDOUT and
STDERR, catching signals etc. And knowing POE will be useful in many
event-based processes.

See:
http://search.cpan.org/~rcaputo/POE-1.0000/lib/POE/Wheel/Run.pm

and the section "Process Management" in the POE cookbook:
http://poe.perl.org/?POE_Cookbook


--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by pgodfrin on April 3, 2008, 4:41 pm
Please log in for more thread options
> > Greetings,
>
> > I would like to create a 'driver' (simulation) program that calls
> > existing perl programs, waits for them to complete and provides some
> > feedback. Ideally I would like to have the driver echo to the screen
> > that the simulation is still running and perhaps provide some
> > information. That may be too ambitious, but that is the basic idea.
>
> It may take a bit of getting used to, but POE::Wheel::Run can do this
> very nicely, including monitoring of the fork'd procesesses' STDOUT and
> STDERR, catching signals etc. And knowing POE will be useful in many
> event-based processes.
>
> See:http://search.cpan.org/~rcaputo/POE-1.0000/lib/POE/Wheel/Run.pm
>
> and the section "Process Management" in the POE
cookbook:http://poe.perl.org/?POE_Cookbook
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/

I was looking at IPC::Run as well. What do you think of it?
pg

Posted by Joost Diepenmaat on April 3, 2008, 4:49 pm
Please log in for more thread options

> I was looking at IPC::Run as well. What do you think of it?

Haven't used it all. And to be honest, from skimming the docs I can't
really see if it will even do what you want it to do if you have more
than one child process running, which I can guarantee you POE will.

But as I implied, POE has a bit of a learning curve, and it requires you
to (re)structure your code in a event handling way. YMMV, probably :-)

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/

Posted by pgodfrin on April 3, 2008, 4:57 pm
Please log in for more thread options
> > I was looking at IPC::Run as well. What do you think of it?
>
> Haven't used it all. And to be honest, from skimming the docs I can't
> really see if it will even do what you want it to do if you have more
> than one child process running, which I can guarantee you POE will.
>
> But as I implied, POE has a bit of a learning curve, and it requires you
> to (re)structure your code in a event handling way. YMMV, probably :-)
>
> --
> Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/

Yeah - I agree. As I've mentioned in the past, I'm just a lowly DBA
(out of work btw) who's trying to not only learn Perl real good, but
use it simply to drive my databases so I can do DBA stuff - you know
like look at CPU util, I/O, funky SQL, etc.

So - I'm certain the POE will do the trick, on your say-so, but I'm
not sure if it is worth the learning curve at this juncture. But I
have bookmarked it and I will at least peruse it to see what it's all
about.

You never know...

smiles,
pg

Similar ThreadsPosted
Calling functions by name and load modules on demand May 19, 2006, 4:20 am
Simulator using perl ? September 2, 2005, 4:04 pm
how to load a picture to an oracle database with the Load a pictureto Oracle with DBI perl module December 3, 2004, 3:37 pm
Book: Higher-Order Perl: Transforming Programs with Programs July 9, 2005, 5:11 pm
Re: Book: Higher-Order Perl: Transforming Programs with Programs July 9, 2005, 11:10 pm
network simulator - NS2 January 5, 2007, 1:14 pm
Load information in Solaris (Perl Script.) November 4, 2004, 6:09 pm
Need help with Perl and MySQL database data load January 14, 2005, 11:23 pm
load modules that are not part of the perl dist June 21, 2005, 3:03 pm
Load perl module dynamically (use $object) March 5, 2006, 12:36 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap