[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Process ID when called from PerlChildInitHandler
From:       Torsten Foertsch <torsten.foertsch () gmx ! net>
Date:       2007-07-24 10:14:14
Message-ID: 200707241214.18872.torsten.foertsch () gmx ! net
[Download RAW message or body]


On Tuesday 24 July 2007 11:21, Gatsby wrote:
> The problem is, if I output $$, then it only ever outputs the process ID of
> the parent httpd process, not the actual child process itself.

This is a known feature (at least I have already met it). It is caused by 
apache doing the fork by itself in combination with perl's fork operator. 
Perl caches the result of the getpid and getppid syscalls. So if a process 
calls fork at C-level perl thinks its cache is still valid even in the child.

You can try this feature also this way:

$ perl -Mstrict \
       -e 'require "syscall.ph"; syscall &SYS_fork;
           print "pid=$$ ".syscall(&SYS_getpid).
                " ppid=".getppid." ".syscall(&SYS_getppid)."\n"'
pid=5835 5836 ppid=4260 5835
pid=5835 5835 ppid=4260 4260

So, to solve your problem you can either use the syscall interface or try my 
Perl::AfterFork or Rafael's Linux::Pid.

If you decide to use my module put this line in your ChildInit handler:

use Perl::AfterFork;		# or require
Perl::AfterFork::reinit();

Here the result:

$ perl -Mstrict -MPerl::AfterFork \
       -e 'require "syscall.ph"; syscall &SYS_fork; Perl::AfterFork::reinit();
           print "pid=$$ ".syscall(&SYS_getpid).
                " ppid=".getppid." ".syscall(&SYS_getppid)."\n"'
pid=6192 6192 ppid=6191 6191
pid=6191 6191 ppid=4260 4260

Torsten

[Attachment #3 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic