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

Hi,

Is there a way to share objects between processes (on Linux)? I have a parent process which create objects, and forks off a child process. This child process manipulates the object and needs to return it to the parent when finished, so the parent can continue with the modified object and pass it on the another child process in time.

Can I use a pointer to the object in the child process? And how?

TIA

Marcel

Replies are listed 'Best First'.
Re: Sharing objects between processes
by broquaint (Abbot) on May 20, 2003 at 15:33 UTC
    This could be a case for threads if perl were able to cope with shared objects, which it can't, anyhow ... Once you fork you're dealing with two separate processes, so the object in question is no longer shared. This will meaan you will have to communicate back to the parent process the any changes made in the child process to the object. This can be done in a variety of ways from socket to named pipes or even Pixie, which you choose is up to you. See. perlipc for more info on inter-process communication.
    HTH

    _________
    broquaint

Re: Sharing objects between processes
by BrowserUk (Patriarch) on May 20, 2003 at 15:41 UTC

    You could probably use Storable to serialise the object and pass it through a pipe, but the idea that you were 'sharing an instance' would only be notional. You would need to ensure that the a serialised instance 'went away' in the process that serialised it and was re-instanciated when it come back.

    This is one of those cases where you would be better off having your objects live in a DB and allow it to take care of sharing them for you. This is especially advantageous if your objects can be long lived.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
Re: Sharing objects between processes
by halley (Prior) on May 20, 2003 at 15:36 UTC

    In general, processes don't share memory. There are methods which may pull it off, but I prefer to stick to "threads share, processes keep private."

    At fork time, the child owns its own copy of the stuff in memory. Let it work on whatever it likes. The child should serialize (save) a copy of an object to be handed back to the parent if necessary. The parent then discovers these saved object (filename, socket connect, etc.) and deserializes (loads) the copy into its own memory space.

    There are a number of approaches to serialization. I suggest using Data::Dumper/eval until you're familiar with the process, then look for more efficient storage methods later.

    --
    [ e d @ h a l l e y . c c ]

Re: Sharing objects between processes
by Joost (Canon) on May 20, 2003 at 15:40 UTC
    If you can serialize your object and restore it (for instance using Storable), you can send it back and forth using pipes or use something like IPC::ShareLite to share it. You might want to take a look at the IPC modules on CPAN to see if you can find something that does this automatically for you.

    Joost

    -- #!/usr/bin/perl -np BEGIN{@ARGV=$0}s(^([^=].*)|=)()s; =Just another perl hacker