You have a script, say 'util', and you kick it off by typing a command something like:

$ util -x this that

Which runs perl, telling it to run the util script with the three arguments you specified. So, this code gets run:

if( @ARGV && $ARGV[0] eq "-child" ) { shift( @ARGV ); # child code here exit( 0 ); } # Am parent # ... system( 1, $^X, $0, "-child", @ARGV ); # run child # ...

And @ARGV is set to ( '-x', 'this', 'that' ). So the first line checks @ARGV in a scalar context which gives us its number of elements, 3, which isn't 0 so it is true and we try the expression after the &&.

$ARGV[0] is '-x', which isn't the same string as '-child' so the 'if' fails and the code in its block gets skipped.

So system( 1, $^X, $0, "-child", @ARGV ); gets run. The '1' tells system to not wait for the command that it starts. This only works in OS/2 and Win32 (and probably still isn't documented).

So the next argument says what program to run. $^X is the name of the file containing the Perl interpreter, something like '/usr/bin/perl' or just 'perl'. So the perl interpreter gets run in a new child process and we don't wait for it to finish.

The first argument passed to perl says what script to run so we put $0 so the child process runs our same script (again).

Next we pass in "-child" and our @ARGV and these end up in @ARGV in the child process.

So when the above code gets run in the child process it is true that $ARGV[0] eq "-child" so the 'if' block gets run.

shift( @ARGV ); # child code here exit( 0 );

The shift removes the '-child' so that @ARGV is now the same as in the parent.

The comment should be replaced with whatever code you want run in the child.

And the exit prevents the rest of the code (meant for the parent in this example) being run in the child.

So the effect is very much like fork.

- tye        


In reply to Re^3: How to multiprocess in Win32? (explained) by tye
in thread How to multiprocess in Win32? by Rex(Wrecks)

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.