On Thu, Oct 6, 2011 at 11:30 AM, Vishal Gupta
<vishal.knit2...@hotmail.com>wrote:

>
>
>
>
>
> Hi,
>
> I have to write a perl program (Parent script) which does the below 4 tasks
> simultaneously:
>
> 1. Executing a perl script in one shell. (Parent script)
> 2. Invoking a thread performing some tasks periodically lets say once in 15
> min, and send a message when any task is completed, to parent shell.
> 2. Invoking a thread which is continuously checking the memory and cpu
> usage using "top" command and inform to parent script if it exceeds 50%.
> 3. Doing "ls -l" for a directory (containing 4 files) per minute and check
> if size of any file increases.
> 4. Invoking a "shell script" which has a execute in every 10 min and
> sending the result each time to file.
>
> The parent script has to execute continuously for 12 hours.
>
> Could you please tell me, how can i create this script ? Which concepts of
> Perl should I use for the above script? Please share with me the link, where
> can I get the tutorial/sample code of those concepts?
>
> Appreciate your help.
>
> Thanks & regards,
> Vishal
>


Hi Vishal,

I would say have a look at http://perldoc.perl.org/threads.html which is
what will help you do the trick. In very very basic terms you run a main
loop, which calls 4 different subs one for each task assigning each sub its
own thread. Then the main loop happily does what it name says it loops over
and over checking the output of the threads and kicking of new once at the
time interval your specified.

The whole thing would look a bit look a bit like this:


Start task 1
Start task 2
Start task 3
Start task 4
Start schedule thread

Main loop
 Check for output from thread 1
  Schedule a new start of thread 1
  Deal with output form thread 1
 Check for output from thread 2
  Schedule a new start of thread 2
  Deal with output form thread 2
 Check for output from thread 3
  Schedule a new start of thread 3
  Deal with output form thread 3
 Check for output from thread 4
  Schedule a new start of thread 4
  Deal with output form thread 4
loop

Your schedule thread, is there to make sure that when it is time to start
one of the threads you can fire it up again. Keeping the threads in a hash
so you can simply when you start a new instance of thread 1 assign this to
%hash( key=thread1, value=reference to your thread) this way your code for
dealing with the output from a thread does not have to be very complex in
terms of finding the thread you are looking for.

I would advise make each of the tasks work on their own as a one off, then
make the main loop and the starting of a thread work for just on thread and
grow the complexity from there. A firs time working wit threads can be a
little complex and starting with to many of them at once might just cause
you more trouble then its worth.

Regards,

Rob

Reply via email to