Am running ActiveState perl v5.8.7 on Windows XP Pro. Tried the following
test script. The idea here is to run two parallel threads that process a
common queue of todo items. The processing however, requires running some
external executables. Had first tried the backtic as I'd like to get the
output. Was hanging so I tried some other combinations.
Finally arrived at the following test script. It sorts a small file called
testfile with a couple of words to sort in it (anything will do). I can run
it on Debian Linux (using the appropriate $XTCMD definition uncommented) and
all three settings of $EXEMODE work fine, and pretty much identically.
On Windows XP however, only $EXEMODE = 0 works. The others both hang on the
external call.
I'd really like to avoid the temporary file technique that I'm limited to
with system(), and use EITHER the backtic or the popen version (or something
else that would work if I'm not aware of it)...
Any idea what's happening here? Windows pseudo-fork anomalies perhaps? Any
other known alternatives?
-- Sync
<code>
#!/usr/bin/perl -w
use strict;
use threads;
use threads::shared;
# EXEMODE: 0 - system, 1 - bactic, 2 - popen
# Try all three to compare
my $EXEMODE = 0;
#my $XTCMD = "/usr/bin/sort testfile"; # linux test
my $XTCMD = "c:\\windows\\system32\\sort.exe testfile"; # windows tes
+t
my $queueindex : shared;
share $queueindex;
my $thearg = 0;
my $qsize = 10;
$queueindex = 0;
sub inc_qindex
{
# print "Locking index\n";
lock $queueindex;
$queueindex++;
# print "Returning index $queueindex\n";
return $queueindex;
}
sub dothread
{
my ( $v, $m, @m );
my $taskctr = 0;
my $tmpfile = "tmp" . $thearg . ".out";
while (1)
{
$v = &inc_qindex; # get next queue item
last if ( $v > $qsize ); # end of queue
print "I am thread [" . $thearg . "] index = [" . $v . "]\n";
if ( $EXEMODE eq 0 ) # this works OK
{
system("$XTCMD >$tmpfile");
open XT, "$tmpfile" or die "$!: Can't open $tmpfile!";
@m = (<XT>);
close XT;
$m = join( "", @m );
print $m; # will see this
}
if ( $EXEMODE eq 1 ) # this hangs
{
$m = `$XTCMD`;
print $m; # never see this
}
if ( $EXEMODE eq 2 ) # this also hangs
{
open XT, "$XTCMD|" or die "$!: trying to run $XTCMD!";
@m = (<XT>);
close XT;
$m = join( "", @m );
print $m; # never see this
}
$taskctr++;
}
return $taskctr;
}
sub test_threaded
{
my ( $ta, $tb, $r );
$thearg = 1;
$ta = threads->new("dothread");
$thearg = 2;
$tb = threads->new("dothread");
$r = $ta->join;
print "Thread 1 returned [$r]\n";
$r = $tb->join;
print "Thread 2 returned [$r]\n";
}
#dothread; # test it unthreaded to make sure it works
test_threaded;
</code>
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs