[tpm] fork and blessed hash references
Abram Hindle
abram.hindle at softwareprocess.es
Thu Jan 14 08:03:13 PST 2010
Madison Kelly wrote:
> Hi all,
>
> I want to put a shutdown timer on a spawnable daemon (specifically, a
> script spawned by dbus). To do this, I created a bless'ed "self" hash
> reference in my module's constructor, set a few time values in it and
> the call a timer method. In the timer method, I 'fork' and the child
> starts a loop and the main script returns and waits for method calls
> over dbus.
>
> The problem is that, in the child process, changes to the values in
> 'self' made by the parent do not appear in the child. Specifically, each
> method call changes a '$self->{SHUTDOWN_TIME}' value. I can understand
> this behavior to an extent, but when I print out the hash reference
> itself both in the parent and the child, they have the same reference
> string. Obviously though, they aren't actually using the same memory space.
They are clones of each other, their pointers are of the same value but
the memory space each resides in belongs to a different process.
Processes do not share pages.
> So two questions;
>
> 1. Why do the self references match when they don't (apparently) use the
> same memory space
Fork produces a new process that is a copy of the old one. From your
point of view they don't share the same the same memory, they are just
clones of each other. (There's a bunch of other lameness and limitations
but you can read 'man fork').
> 2. What is the best way to "share" variables between children and parents?
You don't. You have to communicate. Generally you open a socket or a
pipe between the processes and then when you want to share values you
make up a protocol to do so.
You can also used shared memory, like mmap, but even then you need to
work out what you want to do.
Perhaps you want to use threads?
Other solutions are:
RPC, CORBA, DBUS, etc.
Forking is often easiest if you have 1 way communication, then you just
write to the pipe from the parent and read from it as the child. You can
use select (perldoc select) to periodically check if anything new has
been sent down.
perldoc perlipc
abram
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100114/6609550b/attachment.bin>
More information about the toronto-pm
mailing list