in reply to Re: Behavior of threads on XP-- system() works, backtic & popen don't...
in thread Behavior of threads on XP-- system() works, backtic & popen don't...

Interesting. A couple of things:

First, thanks for the response.

Second, it took me some effort to find your response as I had posted the original to "seekers of perl wisdom" but never actually saw it show up anywhere except when I login and look at my "Nodes You Wrote." I finally did a "super search" of everything for "threads XP" in the title and finally discovered your reply-- I still don't know where this discussion thread is actually showing up, as the "super search" didn't seem to tell me *where* it found what it found...

And finally, I'm using AS 813, and your example works for me too. However, what it doesn't do is run multiple threads in parallel-- it just runs a single thread. So, I tweaked it a bit and came up with the attached version of your program. I was running into errors due to sort not being able to access the test.dat file in each thread due to collisions, so I created 4 copies of the test.dat file (test1-4.dat) and also use the scheme for the sorted.dat output file so that the threads won't be using each others files. I then run async 4 times to spawn off 4 threads. I also added a sleep in the thread (probably not necessary) to make sure all 4 threads are running before any of them start doing their thing. After that, the first thread's system() call works, and then the whole thing hangs. If I only run one thread in "spawnem" then it works OK...

#! perl -slw use strict; use threads; sub test { my $t = shift; my $cmd = "c:/windows/system32/sort.exe test$t.dat"; print "Thread $t\n"; # show things are in motion... sleep 1; # kludge to insure all threads are up... system "$cmd >sorted$t.dat"; open F, "<sorted$t.dat" or die $!; print "Via system:\n", do{ local $/; <F> }; close F; print "Via backticks\n", `$cmd`; open F, "$cmd|" or die $!; print "Via pipe\n", <F>; close F; } sub spawnem { my $t1 = async{ \&test(1); }; my $t2 = async{ \&test(2); }; my $t3 = async{ \&test(3); }; my $t4 = async{ \&test(4); }; $t1->join; $t2->join; $t3->join; $t4->join; } &spawnem;
  • Comment on Re^2: Behavior of threads on XP-- system() works, backtic & popen don't...
  • Download Code

Replies are listed 'Best First'.
Re^3: Behavior of threads on XP-- system() works, backtic & popen don't...
by BrowserUk (Patriarch) on Jun 14, 2006 at 17:48 UTC

    Strange. I created the four data files required and ran your code as is and it produced the following output (I've wrapped the numbers onto single lines to save screen space!):

    c:\test>555304 Thread 1 Thread 2 Thread 3 Thread 4 Via system: 1 2 3 4 5 6 7 8 9 Via system: 1 2 3 4 5 6 7 8 9 Via system: 1 2 3 4 5 6 7 8 9 Via system: 1 2 3 4 5 6 7 8 9 Via backticks 1 2 3 4 5 6 7 8 9 Via backticks 1 2 3 4 5 6 Via backticks 1 2 3 4 5 6 7 8 9 7 8 9 Via backticks 1 2 3 4 5 6 7 8 9 Via pipe 1 2 3 4 5 6 7 8 9 Via pipe 1 2 3 4 5 6 7 8 9 Via pipe 1 2 3 4 5 6 7 8 9 Via pipe 1 2 3 4 5 6 7 8 9

    As you can see, all four threads ran all 3 methods without problems. The "sorted" output is a little mixed up as you might expect with four threads all outputting to the screen concurrently, but no hangs or errors. I did this with AS811 and AS817 with the same results. I don't have AS813 installed, but I do not expect it to fail, but if it is, the answer is to upgrade.

    If upgrading doesn't fix your problem, then we'll need to look more closely at your environment to try and understand what is broken on your system/installation.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Hokay-- tried it on another machine, also a 2.8GHZ hyperthreading box like the one that initially showed the problem. There however, I installed AS817. Almost the same problem-- got through the System() portion of the first two threads, then hung when on my original system it only got through the first thread's system() portion.

      As both of these machines are hyperthreading, I decided to disable hyperthreading on the second machine in the BIOS and try it. Works better, but the very first time I ran it, it hung after the first thread's system(). After that, I wasn't able to get it to hang readily-- it ran fine several times in a row... Seems like a timing problem, so I created a simple .bat file:

      :LOOP perl threadtest.pl goto LOOP

      and ran it. Ran for a minute or so running the complete test multiple times, then finally hung-- though this time about in the middle of the various tests-- several system/bactic/popen's ran ok then it hangs. This is with hyperthreading disabled.

      Appears to be a race or timing problem of some kind, possibly exacerbated by hyperthreading, but apparently not caused by it...

      Makes me wonder if system() might also have the problem but less so-- will run some loop tests with just that to see if it will ever hang...

      Update: Haven't seen the system() only version hang after looping for quite some time, while not definitive it's still a good sign as far as that is concerned. Also tried the full test on a 1.79GHZ box that doesn't have hyperthreading hardware, and after a dozen or so loops it hangs too. Yet to find a machine it doesn't hang on within a couple dozen loop iterations...

      -- Sync

        I can't reproduce your errors. My feeling is that the errors you are describing are so obvious that they would have been seen and reported long ago if this were a problem with Perl or AS perl. That leads me to conclude that the problem is something to do with your own environment.

        You will need to produce a short script that reliably reproduces the problems for other people before you will be able to take this further.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.