rovf has asked for the wisdom of the Perl Monks concerning the following question:
My program essentially looks like this:
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.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 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"?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: exec on Windows "halfway forks"
by Yary (Pilgrim) on Jun 14, 2010 at 16:47 UTC | |
by ikegami (Patriarch) on Jun 14, 2010 at 17:46 UTC | |
by Yary (Pilgrim) on Jun 14, 2010 at 17:58 UTC | |
by rovf (Priest) on Jun 15, 2010 at 11:18 UTC | |
|
Re: exec on Windows "halfway forks"
by BrowserUk (Patriarch) on Jun 14, 2010 at 17:54 UTC | |
by JavaFan (Canon) on Jun 14, 2010 at 18:27 UTC | |
by BrowserUk (Patriarch) on Jun 14, 2010 at 18:38 UTC | |
by rovf (Priest) on Jun 15, 2010 at 11:28 UTC | |
by BrowserUk (Patriarch) on Jun 15, 2010 at 11:38 UTC | |
|
Re: exec on Windows "halfway forks"
by JavaFan (Canon) on Jun 14, 2010 at 16:53 UTC | |
by tye (Sage) on Jun 14, 2010 at 17:42 UTC | |
by rovf (Priest) on Jun 15, 2010 at 11:22 UTC |