For Programmers: Free Programming Magazines  


Home > Archive > ithreads > February 2006 > Executing commands in the system via threads









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Executing commands in the system via threads
ukmay

2006-01-10, 12:22 am

I have been experimenting to figure out how to use the threads module
and I have run into a problem that I can not figure out. I need to call
an executable program multiple times and I will then need to process
the returns to stdout. If I run the following code with the system
command there are no problems.

use strict;
use warnings;
use Env;
use POSIX;
use Thread;

my $thr1 = new Thread \&sub1, "vsim_1133390003.fcdb";
my $thr2 = new Thread \&sub1, "vsim_1133390008.fcdb";
my $thr3 = new Thread \&sub1, "vsim_1133390025.fcdb";

foreach my $thr (threads->list) {
my @ReturnData = $thr->join;
print "Thread returned @ReturnData\n";
}

sub sub1 {
my @InboundParameters = @_;
my $FileName = $InboundParameters[0];
print "Processing $FileName\n";
my $sys = system "vcover stats $FileName";
return 1 ;
}

However I need to get hold of the return information from the vcover
command that I run from system. system() itself doesn't return the
stdout so I have to use
the 'tick method I believe. However as soon as I change the line above
in sub1 to be :

my @value = `vcover stats $FileName `;

The script hangs. I have added a redirect to the vcover command and
know that they are all running and finish ok, but they still hang the
script every time. Does anybody have any ideas ?

Many Thanks

Darron

Jerry D. Hedden

2006-02-27, 7:21 pm

darronm (ukmay) wrote:
> I need to call an executable program multiple times and I
> will then need to process the returns to stdout.
> ...
> However as soon as I change the line above in sub1 to be:
> my @value = `vcover stats $FileName `;
> The script hangs.


Well, one problem I noted is that you're trying to use the old
'Thread' module. I tried something similar using 'threads',
and it worked fine:

#!/usr/bin/perl

use strict;
use warnings;
$| = 1;

use threads;

MAIN:
{
my @thr1 = threads->create(\&sub1, "ls -l /c");
my @thr2 = threads->create(\&sub1, "ls -l /var");
my @thr3 = threads->create(\&sub1, "ls -l /usr");

foreach my $thr (threads->list) {
my @output = $thr->join();
print("Thread returned:\n", @output, "\n");
}
}

exit(0);

### Subroutines ###

sub sub1 {
my $cmd = shift;
print "Command: $cmd\n";
my @output = `$cmd`;
return (@output);
}

# EOF

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com