in reply to getting a child's process ID

I was wondering and maybe someone that replied earlier to this thread can tell me. Is there some way that the child can determine whether or not the parent process is still running (Active State Perl 5.6.x)? I'm familiar with doing this in a xnix environment with  getppid  but I know it's not available under ASP. I believe Seshouan was looking for similar information in a post yesterday as well.

Thanks,

--Jim

Replies are listed 'Best First'.
Re: Re: getting a child's process ID
by fokat (Deacon) on Oct 19, 2001 at 21:54 UTC
    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.
Re: Re: getting a child's process ID
by blackmateria (Chaplain) on Oct 19, 2001 at 21:27 UTC
    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!

Process still alive
by Fletch (Bishop) on Oct 19, 2001 at 21:08 UTC

    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.