Regarding help in Threading

Regarding help in Threading

am 06.10.2011 11:30:22 von Vishal Gupta

--_0bbc27e4-21fa-4f2b-b17c-1c357b888192_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable






Hi=2C

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=2C and send a message when any task is completed=2C to parent shell.
2. Invoking a thread which is continuously checking the memory and cpu usag=
e 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 sendin=
g the result each time to file.

The parent script has to execute continuously for 12 hours.

Could you please tell me=2C how can i create this script ? Which concepts o=
f Perl should I use for the above script? Please share with me the link=2C =
where can I get the tutorial/sample code of those concepts?

Appreciate your help.

Thanks & regards=2C
Vishal
=

--_0bbc27e4-21fa-4f2b-b17c-1c357b888192_--

Re: Regarding help in Threading

am 06.10.2011 12:14:09 von Rob Coops

--20cf303bf96417c33b04ae9e9702
Content-Type: text/plain; charset=UTF-8

On Thu, Oct 6, 2011 at 11:30 AM, Vishal Gupta
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

--20cf303bf96417c33b04ae9e9702--

Re: Regarding help in Threading

am 06.10.2011 12:46:38 von Shlomi Fish

ׁHello, Vishal,

On Thu, 6 Oct 2011 15:00:22 +0530
Vishal Gupta wrote:

>=20
>=20
>=20
>=20
>=20
> Hi,
>=20
> I have to write a perl program (Parent script) which does the below 4 tas=
ks simultaneously:
>=20
> 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 us=
age 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 chec=
k if size of any file increases.
> 4. Invoking a "shell script" which has a execute in every 10 min and send=
ing the result each time to file.
>=20
> The parent script has to execute continuously for 12 hours.
>=20
> Could you please tell me, how can i create this script ? Which concepts o=
f Perl should I use for the above script? Please share with me the link, wh=
ere can I get the tutorial/sample code of those concepts?

Well, I would suggest against limiting your specification to a certain
implementation - namely using perl's threads here:

http://www.perlmonks.org/?node_id=3D288022

Multi-threading in Perl is very problematic and should usually be avoided. =
You
can usually achieve what you want using either multi-processes or using an
event framework such as POE:

http://perl-begin.org/uses/multitasking/

Regards,

Shlomi Fish =20

>=20
> Appreciate your help.
>=20
> Thanks & regards,
> Vishal
> =20


--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Original Riddles - http://www.shlomifish.org/puzzles/

JATFM == â€=9CJust answer the fabulous manâ€=9D

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

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