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

#!/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;

In reply to Behavior of threads on XP-- system() works, backtic & popen don't... by Sync

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.