![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl script assistance; paste word into external command | bru | Shell Programming and Scripting | 10 | 02-23-2007 04:04 AM |
| multi threaded program is hanging | hikrishn | AIX | 0 | 12-15-2006 08:39 AM |
| Assistance with Perl and HTTP | bryanthomas | Shell Programming and Scripting | 7 | 12-11-2006 09:38 PM |
| HOWTO: Calculate the balance of work in multi-threaded app. | DreamWarrior | High Level Programming | 2 | 11-01-2006 10:54 AM |
| multi-threaded server, pthreads, sleep | Parahat Melayev | High Level Programming | 0 | 03-16-2005 12:38 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
In need of multi threaded perl assistance
I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds.
Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid = execute($cmd, $timeout); push (@pids, $pid); } # pid included above in case it's needed &wait_for_completion_or_timeout; foreach (@commands) { if ($output{$cmd}) { print "Output of $cmd was $output{$cmd}, it comleted in $duration{$cmd} seconds\n"; } else { print "$cmd did not complete in the allowed time and was killed"; } ... what could the execute function look like ? Currently I have something roughly like this sub execute { my $cmd = shift; my $pid; unless ($pid = fork) { exec("$cmd > /tmp/out.$$"); } return $pid; # returning this pid, just in case :-) } This works, but I don't feel it's very elegant. I have to keep track of all pids and kill processes that take more than X seconds using 'ps' to track processes and 'kill' to kill them. Ideally I'd like something platform independent. I've looked into multi threaded programming but I'm unfamiliar with this and I'm stomped. Example of my attempted code to achieve the same as above: sub execute { my $cmd = shift; system("$cmd >/tmp/out.$$"); } foreach $cmd (@commands) { my $thread[$i++] = threads->create(\&execute,$cmd); } If I run this code I get an error like A thread exited while 5 threads were running. ... where do I go from here ? How do I wait until each thread has completed, and how would I kill slow threads ? Should I be looking at a completely different approach ? Any assistance appreciated ! |
|
||||
|
To wait for a thread to exit, you can use the join() method.
perlthrtut - perldoc.perl.org You may try to install a signal handler (such as $SIG{INT}) so that you can intercept the termination so that you can pass stop instruction to individual threads (by means of a shared variable that indicates go/stop condition). I think I have tried this in the past and has worked for me. Unfortunately I don't have the code right now, and I have forgotten most of these threaded stuff already. |
|
||||
|
Thanks for your answer. I did find the join() method but what confuses me is that I can't (as far as I can tell) wait for each thread in parallell, which sort of defeats the purpose:
Quote:
|
|
||||
|
If you have Perl 5.10, life is much more easier for you as a lot of improvements to thread support has been made. Considering installing it if you need to use threads properly (you don't necessarily need to install perl as root or with other admin privilege). 5.8 and earliers' thread support is crippled in a lot of ways that I think I would stay away from it, and use an environment with more comprehensive thread support, such as Java.
Everything below would be 5.10 specific. I haven't actually tried 5.10 before, so you should share your findings with us if you venture it. You can probably check for non-blocking join() using the is_joinable() method. I have not used it before but there are examples citing this on the threads manpage. You can also list the joinable threads so that you don't have to keep checking each thread object for non-blocking join: Code:
my @joinable = threads->list(threads::joinable) Do let us know if they work for you. |
|
||||
|
I should have mentioned that I have to stick with 5.8.5 compatible code here. This needs to get deployed on multiple servers where a perl upgrade is not an option.
Currently I am moving away from threads and just using good old fork & exec. Not as platform independent as I had hoped but I'll cross that barrier when I need to. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|