Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Trouble using system() on Win32 machines

by ton (Friar)
on Apr 17, 2001 at 04:14 UTC ( [id://73013]=perlquestion: print w/replies, xml ) Need Help??

ton has asked for the wisdom of the Perl Monks concerning the following question:

I'm having some trouble using the system call in my ActiveState distribution of Perl (build 623). The system call seems to believe that its child process has finished when the child execs another command. Here's some illustrative code (saved to a file called 'test.pl'):

use strict; my $num = shift; if ($num) { if ($num % 2) { print "$num is calling system...\n"; system("perl test.pl " . ($num - 1)); print "$num is finished calling system.\n"; } else { print "$num is calling exec...\n"; exec("perl test.pl " . ($num - 1)); print "$num finished calling exec.\n"; } }
The program should be invoked with a number after it; the program then recursively calls itself with the next smaller number; using system for odds and exec for evens. On my Linux box, I get the following output:
$ perl test.pl 5 5 is calling system... 4 is calling exec... 3 is calling system... 2 is calling exec... 1 is calling system... 1 finished calling system. 3 finished calling system. 5 finished calling system.
which is exactly what I expect. I get this output when I try to call the same script on my Win2K machine:
D:\PerlCode>perl test.pl 5 5 is calling system... 4 is calling exec... 5 is finished calling system. 3 is calling system... D:\PerlCode>2 is calling exec... 3 is finished calling system. 1 is calling system... 1 is finished calling system.
D'oh! This is really messing up one of my programs, which is started by another program (through system) and has the power to update and restart itself (through exec); the parent program thinks that the child is done when it really just re-execed itself.

Is this a bug in the ActiveState distribution? Can anyone think of a good way to get around this?

Any help would be appreciated.

-Ton
-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

Replies are listed 'Best First'.
(tye)Re: Trouble using system() on Win32 machines
by tye (Sage) on Apr 17, 2001 at 05:35 UTC

    Win32 doesn't have a native exec() so Perl's exec does the equivalent of system(1,@_);exit(0);.

    So, yes, the subprocess does exit when it does an exec.

    Update: I originally forgot to write a non-blocking system (an initial "1," prevents the parent from waiting for the system subprocess to finish on several versions of Perl).

    So you can work around the problem you are having by creating a module that replaces exec with something that does:

    sub exec { exit( system(@_) ); }

    Update2: And here is such a module:

    package Exec; sub import { *CORE::GLOBAL::exec= \&exec; } sub exec { system( @_ ); exit( 0 ); } 1;
    Just put that in Exec.pm and add -MExec to your perl command line (or do SET PERL5OPT=-MExec) and your problem will go away.

            - tye (but my friends call me "Tye")
      A simple way to get the parent process to block on the child on a Win2K box would be to do this:
      system 'start /wait perl test.pl';
Re: Trouble using system() on Win32 machines
by OzzyOsbourne (Chaplain) on Apr 17, 2001 at 23:58 UTC

    What about forgoing system() and exec() for win32::process?

    -OzzyOsbourne

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://73013]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 22:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found