In need of multi threaded perl assistance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting In need of multi threaded perl assistance
# 1  
Old 01-28-2009
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  
Old 01-28-2009
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  
Old 01-28-2009
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  
Old 01-28-2009
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  
Old 01-29-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

script for multi-threaded bash processes

hey everyone, I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit. for example if there are 4 machines to be backed up,... (2 Replies)
Discussion started by: terrell
2 Replies

2. Programming

Deallocating memory in multi-threaded environment.

I'm having a hard time figuring out how to manage deallocation of memory in multithreaded environments. Specifically what I'm having a hard time with is using a lock to protect a structure, but when it's time to free the structure, you have to unlock the lock to destroy the lock itself. Which will... (5 Replies)
Discussion started by: gngrwzrd
5 Replies

3. UNIX for Dummies Questions & Answers

Assistance with combining, sorting and saving multi files into one new file

Good morning. I have a piece of code that is currently taking multiple files and using the CAT.exe command to combine into one file that is then sorted in reverse order based on the 3rd field of the file, then displayed on screen. I am trying to change this so that the files are being combined into... (4 Replies)
Discussion started by: jaacmmason
4 Replies

4. Programming

multi-threaded memory leak

Hello All : I write a .c program to test the exactually resource the memory leak as follows: 1 #include <stdio.h> 2 #define NUM 100000 3 void *Thread_Run(void * arg){ 4 //TODO 5 //pthread_datch(pthread_self()); 6 int socket= (int)arg; 7 ... (1 Reply)
Discussion started by: aobai
1 Replies

5. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

6. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

7. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

8. Shell Programming and Scripting

Assistance with Perl and HTTP

I need to query a http site and then parse the xml results, this works well if I use the string in IE but I require an automated solution. I have tried using the following as well as HTTP::Request, nothing seems to work any suggestions would be appreciated, I have tried diffrnt things I found on... (7 Replies)
Discussion started by: bryanthomas
7 Replies

9. Programming

HOWTO: Calculate the balance of work in multi-threaded app.

I was wondering if anyone could give me a good idea how to calculate how balanced the threading is on a multi-threaded application. I want a percentage, such as "threads are 80% balanced." This is the way I am currently going about it, maybe it is good, maybe not. First, whenever a thread... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

10. Programming

multi-threaded server, pthreads, sleep

I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a solution but it is not how it must be... i think. When threads working without sleep(1) I can't... (0 Replies)
Discussion started by: Parahat Melayev
0 Replies
Login or Register to Ask a Question