My program essentially looks like this:

use strict; use warnings; use Config; # $Config use File::Spec::Functions qw(file_name_is_absolute rel2abs); INIT { if(!file_name_is_absolute $0) { print STDERR "Restarting $0 using absolute file name\n"; exec($Config{perlpath},rel2abs($0),@ARGV) or die "exec failed ($!) +"; } } # Main part replaced by dummy: while(1) { sleep(5); print "running $$\n"; }
The idea is the following: The program is supposed (for reasons which are not important here) to be called on Windows, using an absolute path name. The INIT section checks this, and if this is not the case, recursively starts the program by employing an absolute path. This works so far.

The problem is the behaviour of the (recursively) started program: I get every 5 seconds a "running..." message printed, but in addition, when I hit the ENTER key, I see the prompt of my Windows shell, and I can enter commands to this shell. These are executed in parallel to the while() loop of my program.

I had expected that exec() just replaces the current Perl process by a new one. This doesn't seem to happen, and BrowserUk explained in Re^7: exec() on Windows, that the implementation of exec under Windows is flawed, in that it would act like system: Call the external program and wait until it finishes.

However the behaviour I am experiencing, does not match this either. Instead, I get a CMD.EXE shell plus my Perl program in parallel! Any explanation for this? And, does someone know a better way to "restart myself"?

-- 
Ronald Fischer <ynnor@mm.st>

In reply to exec on Windows "halfway forks" by rovf

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.