I tried using fork(), it returns the PID of the fork, but not the process within the fork, that's what I want to exit. This is what I have:
#!/usr/bin/perl -w
use POSIX;
if ($pid = fork)
{
# parent
open(OUT,">Test.txt");
print OUT "The process: $pid\n";
close(OUT);
}
else
{
# child
exec("emulator.exe");
exit(0);
}
the pid that is printed in the file is not that of the exec() call, that's what I'm interested in getting so that I can quit it.
| [reply] [d/l] |
#!/usr/bin/perl -w
# parent.pl
use strict;
if (my $pid = fork) {
print "parent: child pid is $pid\n";
} else {
exec('./child.pl');
}
#!/usr/bin/perl -w
# child.pl
use strict;
print "Child: pid is $$\n";
And the output I got was:
parent: child pid is 26534
Child: pid is 26534
--
<http://www.dave.org.uk>
"The first rule of Perl club is you don't talk about
Perl club."
| [reply] [d/l] [select] |
your exemple would work for Unix systems, but not for win32. Using what you gave me, the child reports the accurate PID, but the parent reports something totally different. Thanks though.
| [reply] |
Seshouan, I just re-read the posts in this thread again, and enlightenment struck me like a ton of bricks (I think ;) Are you using ActivePerl on Windows? Because in that version of perl, process ID's don't stay the same across an exec() call. The PID is supposed to stay the same, and on Unix, it does; that's why davorg's example works and yours doesn't -- he tried his on Unix.
The problem is, Win32 doesn't have a way to launch a new program without changing the PID. On Unix, exec() is an actual system call that loads a new program into a pre-existing process. On Win32, exec() is implemented by having the exec-ing process call CreateProcess, and then exit. So on Unix the $pid=fork trick works because the program you are exec-ing ends up in the child you created with fork, but on Windows, the program you exec ends up as a grandchild of the original process. Oops. That will make things a lot harder.
I can think of two ways to solve this problem: (1) use Cygwin's Win32 perl instead of ActivePerl, or (2) hack together some kind of inter-process communication to let the parent process find out the child's pid. For example, pass the child a command-line argument that's a filename where it can write its pid once it starts up. Unfortunately, both these solutions require that you're in control of both the exec'd child and the parent. If not, there's probably still something you can do to get the child's pid, but I can't think of it off the top of my head. And of course all of this is irrelevant if you're not actually running on Win32. Anyway, good luck.
| [reply] |