The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2009
SandmanCL SandmanCL is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 9
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 !
  #2 (permalink)  
Old 01-28-2009
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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.
  #3 (permalink)  
Old 01-28-2009
SandmanCL SandmanCL is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 9
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:
foreach (@cmds) {
print "starting thread $i\n";
$thread[$i] = threads->create(\&myexec, $_);
++$i;
}

foreach (@threads) {
system("date");
print "joining ",$_->tid(),"\n";
$_->join();
print "result: ", $?,"\n";
}
In the above example, the join loop will not move past the first thread until that thread is completed.
  #4 (permalink)  
Old 01-28-2009
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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)
threads - perldoc.perl.org

Do let us know if they work for you.
  #5 (permalink)  
Old 01-29-2009
SandmanCL SandmanCL is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 9
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
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0