Remember that at some point of the child's lifetime, it was the parent. Just do something along the lines of:
#!/usr/bin/perl -w
use strict;
our $Parent_PID = $$;
# ... Many interesting lines of code ...
my $pid;
# The mini-loop below will re-attempt the fork()
# in case of errors, that are usually transient
# resource shortages. You might not want to do this
# in your scenario...
while (($pid = fork()) < 0) { sleep 5; }
if ($pid) { # Father
print "father: the child is $pid\n";
# ... Interesting code to wait for the children ...
}
else { # Child
print "child: My parent is $Parent_PID\n";
print "child: I am $pid (or $$)\n";
}
Hope this helps you. | [reply] [d/l] |
jlongino -- much to my chagrin when working on Windows, Win32 doesn't have a (portable) getppid() equivalent. The closest equivalent is the Process32*() functions in the toolhelp library--these have a way of getting the parent pid. Unfortunately, they don't work on WinNT 4.0. Also, ActivePerl doesn't appear to use toolhelp (at least, a quick "grep -ir process32 *" from the root of my ActivePerl build 613 install didn't turn up anything). There might be a CPAN module that talks to toolhelp, I'm not sure. In any case, it's not portable.
I'd say your best bet is to either use some form of inter-process communication (if you have control of the parent process), or else use the Cygwin version of perl instead of ActiveState if you can. getppid() works in that version. Also, beware of ActiveState's fork() -- it doesn't play very well with their implementation of sockets. I usually end up either using Cygwin or, for C programs, writing IPC code. Hope this helps! | [reply] |
I don't do wintendo, so I can't answer as to how to find your
parent's pid there. But if you've got the pid available
you should be able to do kill 0 => $ppid to check.
If that returns true then the process is still running.
According to perldoc perlport this construct should
work on wintendo.
| [reply] |