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

I hope this is an easy question and I'll try to be as general as possible:

Generally, I have a script that will fork() n number of processes off. Within each child, I need to have a variable that can be shared amongst all the children, then later used in the parent again.

Given this, what is the best way of doing so?

For example:
#!/usr/bin/perl use strict; use warnings; my $var; my $pid = fork(); $var++; waitpid( $pid, 0 ); print "$var\n" if ( $pid );
Where I want $var to be 2, not 1.

Thank you in advance for any/all replies!!

Replies are listed 'Best First'.
Re: fork()ing and sharing data amongst children
by Corion (Patriarch) on Dec 22, 2005 at 22:13 UTC

    Your question has an easy answer. In general, it's not possible to access the memory of the parent process when using fork(). There are now some ways how you can pass data back and forth between your children and the parent:

    • Have all the children write out a datafile, and read the datafile in the parent
    • Have all children output their data through a TCP socket and listen to that socket
    • Use a database for passing forth and back information
Re: fork()ing and sharing data amongst children
by traveler (Parson) on Dec 22, 2005 at 22:58 UTC
Re: fork()ing and sharing data amongst children
by QM (Parson) on Dec 23, 2005 at 00:11 UTC
    If you're looking for an easy communication path, you can do this on Unix (but not DOS, apparently):
    if ( my $pid = open( $name, "-|" ) ) # fork a child {...}
    <shameless self promotion>
    See this node for the complete example.
    </shameless self promotion>

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: fork()ing and sharing data amongst children
by blazar (Canon) on Dec 23, 2005 at 11:24 UTC

    For the task that you describe, threads seem more appropriate than forked processes, since with the former you can share variables in the strictest sense and with the latter you just can't.

    But it may well be that depending on your actual application that forking is the way to go. In that case you can make your processes communicate and coordinate each other. This is called, not surprisingly, "interprocess communication" and you may find relevant info for Perl in perldoc perlipc

    Very lightweight and cheap forms of ipc are piped opens and named pipes (or FIFOs); the latter ones assume you're on *NIX.

Re: fork()ing and sharing data amongst children
by salva (Canon) on Dec 23, 2005 at 13:09 UTC
    you can also try using some of the modules on CPAN implemented around the mmap system call.
Re: fork()ing and sharing data amongst children
by zentara (Cardinal) on Dec 23, 2005 at 11:18 UTC
    When you want to share data between children and parents, it is easiest to use threads and "threads:shared". Alot of examples are floating around here. If you must use fork, and you don't want to write a temp file, you can use IPC::Sharable to share memory segments.

    I'm not really a human, but I play one on earth. flash japh