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 :slight_smile:
}

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:

In the above example, the join loop will not move past the first thread until that thread is completed.

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:

my @joinable = threads->list(threads::joinable)

threads - perldoc.perl.org

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.