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

I have a program that spawns a child process. I'd love for the child to be able to send data periodically back to the parent. I don't want build a listener in the parent.

Are there any non-network inter-process communications in Perl?

Thanks as always,

D

Replies are listed 'Best First'.
Re: Perl IPC?
by BrowserUk (Patriarch) on Dec 04, 2002 at 22:42 UTC

    You could take a look at perlipc and IO::pipe.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Perl IPC?
by dbp (Pilgrim) on Dec 04, 2002 at 23:59 UTC
    What exactly do you mean by "I don't want to build a listener in the parent." Clearly the parent has got to listen in some way. There are couple of ways to do non-network IPC. You can use basic pipes:
    pipe(README, WRITEME); if (fork) { #parent code while (<README>) { # you can do other stuff in here besides reading $output .= $_; } close(README); } else { # child code print WRITEME "interesting stuff"; close(WRITEME); }
    There's also named pipes, which are nice in cases when there isn't a relationship between the communicating processes (although you could use them in this case if you want).
    # writer open(FIFO, "> /path/to/named.pipe"); print FIFO "Interesting stuff"; close(FIFO); #reader open(FIFO, "< /path/to/named.pipe"); while(<FIFO>) { $output .= $_; } close(FIFO);
    With named pipes, readers and writers will block until their counterpart appears at the other side unless you open the fifo using +<. All of these examples are out of the Perl Cookbook, chapter 16.

    Update: Forgot to mention you have to create the named fifo first, from the shell. On most systems this is

    mkfifo /path/to/named.pipe
    Older systems use mknod.

Re: Perl IPC?
by msergeant (Novice) on Dec 05, 2002 at 05:55 UTC
    I don't know if this is useful or not but I've just recentley finished a bit of code that contains both the parent and a bunch of children. I use Parallel::ForkManager to fork off my children & IPC::Shareable for interprocess communication. All in all I end up doing 50 simultaneous requets to a web server for different docs, parse the docs and push the results into an array which is shared by all processes. At the end I take my array and email the output. All in all a handy little solution. Let me know if you'd like some example code but most of what I got can be found in the perldoc pages for the above modules.

    Mark

      IPC::Shareable is what I was looking for, many thanks! I phrased my question badly, I was looking for non-network, or non-IP based IPC method. I should have specified that I didn't want to build an IP listener.

      Thanks Monks,
      D

Re: Perl IPC?
by rcaputo (Chaplain) on Dec 05, 2002 at 06:00 UTC