Operating System - Linux
1827810 Members
1953 Online
109969 Solutions
New Discussion

Re: Parallel Programming with Perl

 
SOLVED
Go to solution
Kalin Evtimov
Regular Advisor

Parallel Programming with Perl

Hello!
I want to write a programm, which should run a command, count 15 minutes and check the status. If the command is still running, it should be canceled.
However, I found out, that waiting 15 mins with sleep() doesn't bring anything, since it runs in the same thread.
What would be a nice approach to implement an independant clock?
Running it in a parallel thread is ok, but only if the machine has multiple CPU's. Otherwise threads are run one after the other.
7 REPLIES 7
Arunvijai_4
Honored Contributor
Solution

Re: Parallel Programming with Perl

Hello,

You can probably use Parallel-ForkManager to start with.

http://search.cpan.org/~dlux/Parallel-ForkManager-0.7.5/ForkManager.pm

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Arunvijai_4
Honored Contributor

Re: Parallel Programming with Perl

Hi Again,

Also, this link http://www.unix.org.ua/orelly/perl/prog3/ch17_01.htm

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
H.Merijn Brand (procura
Honored Contributor

Re: Parallel Programming with Perl

use alarm ()

the process will then receive an ALARM signal after that time, but the process can do whatever it wants in thr meantime.

# perldoc -f alarm

will describe it, and supply you with an example

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steven E. Protter
Exalted Contributor

Re: Parallel Programming with Perl

Shalom Kalin

http://earth.e-raist.com/~raistlin/coding.html

You need to create an independent child process

http://bfr.caseywest.com/archives/005438.html

The child must be independent of its parent and run regardless of the status of the parent.

Be careful you can really tie up a system with this.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Hein van den Heuvel
Honored Contributor

Re: Parallel Programming with Perl



Using Alarm is a geat suggestion, but is in this particular example, why not go for the standard 'fork'.
Specifically used the forked thread to run the main code. The parent receives the pid for the child and can poke and prod it after sleeping for a while.

> Be careful you can really tie up a system with this.

Not with a simple fork.

> Running it in a parallel thread is ok, but only if the machine has multiple CPU's. Otherwise threads are run one after the other.

Not true. _every_ OS scheduler knows how to run more than 1 thread, making it seem like there is more than one cpu. It does this first and foremoste by exploiting wait time (your sleep, newtowrok wait, disk wait). But even if those threads are 100% cpu bound (not your case), it can and will timeslice to allow more tahn one thread a slice of cpu time.

Cheers,
Hein.



Kalin Evtimov
Regular Advisor

Re: Parallel Programming with Perl

In this context, how can I run a command from a perl-programm, say "sleep 15", but call it in a way, that the perl-programm doesn't wait for the "sleep 15", but go on.
system() waits for the call, exec() ends the current script, and qw() does the same as system().

Do I have any other alternative here?

Thank you!
Kalin Evtimov
Regular Advisor

Re: Parallel Programming with Perl

OK, fork() did it for me, thanks to everybody!