K
kdd21
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. I can run it on Debian
Linux (using the appropriate $XTCMD definition uncommented) and all
three flavors work fine, and pretty much identically.
On windows however, only $EXEMODE = 0 works. The others both hang on
the external.
I'd really like to avoid the temporary file technique, 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 alternatives?
#!/usr/bin/perl -w
use strict;
use threads;
use threads::shared;
my $EXEMODE = 0; # 0: system 1: bactic 2: popen Try all three to
compare
#my $XTCMD = "/usr/bin/sort testfile"; # linux test
my $XTCMD = "c:\\windows\\system32\\sort.exe testfile"; # windows test
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;
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. I can run it on Debian
Linux (using the appropriate $XTCMD definition uncommented) and all
three flavors work fine, and pretty much identically.
On windows however, only $EXEMODE = 0 works. The others both hang on
the external.
I'd really like to avoid the temporary file technique, 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 alternatives?
#!/usr/bin/perl -w
use strict;
use threads;
use threads::shared;
my $EXEMODE = 0; # 0: system 1: bactic 2: popen Try all three to
compare
#my $XTCMD = "/usr/bin/sort testfile"; # linux test
my $XTCMD = "c:\\windows\\system32\\sort.exe testfile"; # windows test
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;