Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

getppid() returns cached value

by pratik (Initiate)
on May 26, 2005 at 12:06 UTC ( [id://460633]=perlquestion: print w/replies, xml ) Need Help??

pratik has asked for the wisdom of the Perl Monks concerning the following question:

Try out this sample code ( should work on Linux - be careful before trying on Solaris ) :
use strict; my $pid = $$; print "My PID is $pid\n"; fork and exit; sleep 1 while -e "/proc/$pid/cwd"; print "My PPID is ", getppid, " after my parent is killed\n";
It would return original parent's id that forked the child and pid of current parent. Any idea how to work around this problem ?

Thanks,
Pratik

Replies are listed 'Best First'.
Re: getppid() returns cached value
by wazoox (Prior) on May 26, 2005 at 12:45 UTC
    It works fine for me on Linux (MDK 9.1, RHEL 4) : it says
    My PID is 26131 My PPID is 1 after my parent is killed
    Which is right, because the parent doesn't exist anymore, so the PPID should be 1 (init).
Re: getppid() returns cached value
by jasonk (Parson) on May 26, 2005 at 14:04 UTC

    Starting with 5.8.1, Perl began caching ppid values in an attempt to hide the fact that Linux assigns different process ids to different threads in the same application, it was thought that getppid in perl should behave the same across platforms so this caching was introduced. There was a lot of argument over whether this was a good thing or not. I'm not sure when it was changed back (or fixed), but I can say that 5.8.6 behaves as you would expect. It's also possible that this behavior could be different between the same versions of perl, depending on whether you compiled with thread support or not.


    We're not surrounded, we're in a target-rich environment!
Re: getppid() returns cached value
by muntfish (Chaplain) on May 26, 2005 at 13:14 UTC
    Any idea how to work around this problem ?

    Sorry, maybe I'm missing the point, but you haven't actually said what "problem" you are experiencing.

    As wazoox said, getppid will return 1 once the original process has been killed. The orphaned child process gets "adopted" by pid 1 (init). So it all seems correct to me.

    If you can explain what you actually want the script to do, and what it's doing instead, that would help a lot.


    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
      Oops..here is the output I am getting if I use perl 5.8.2.
      My PID is 24958 My PPID is 24958 after my parent is killed
      It is probably returning the cached value of Parent PID.

      But it works as expected if I use perl 5.6.1
      My PID is 25020 My PPID is 1 after my parent is killed

      My OS info :
      Linux version 2.4.21-4.ELsmp (bhcompile@daffy.perf.redhat.com) (gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-20)) #1 SMP Fri Oct 3 17:52:56 EDT 2003

      Thanks,
      Pratik
Re: getppid() returns cached value
by Molt (Chaplain) on May 26, 2005 at 15:47 UTC

    I've been working on this same problem myself lately, and think I've found a work-around that looks like it works although I've not yet had time to try it.

    The kill function can be used to check if a given process ID is available to receive signals (With some limitations on OS, see the perlport docs for details), so the way I'm planning on checking for a parent process' death is something like:

    if (getppid==1 or !kill (0, getppid)) { # Parent is dead, long live the child! } else { # Parent still alive. }

    As I've said I've not had time to actually try this myself yet, but it's looking to be along the right lines and replace the Proc::ProcessTable hack I'm currently using for the job. Let me know if it does work for you.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://460633]
Approved by prasadbabu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-25 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found