in reply to forking: retaining variables after the child exits

The only thing you can "return" from a child process is an exit code, which is simply the value sent from your exit call, but even then, you have to match this number up with the process in your signal handler, and so forth, so it's not very practical at all.

You will certainly want to read the perlipc documents, those that deal with Inter-Process Communication. This is a fairly in-depth document, which can be confusing, but there are some good examples.

Since the parent and the child are separate processes, they can't communicate with eachother through variables, like you can within a single program. Instead, you have to create some sort of mechanism for communication explicitly. Generally, this involves creating a pipe that is shared the parent and child. This data conduit is used by the child to send any required data back to the parent.

Basically, the parent creates a pipe, which is double-ended, and then forks. The child will use one end of the pipe to report, and the parent will use the other end to listen. It's kind of like a tin-can and string telephone.

Of course, the parent must check on these sockets for each child that is active. You can use select, or, if you're feeling rather spirited, IO::Select may be more to your liking since it is simpler. The idea is that while each child has a single end, the parent is going to have a whole lot of ends to check on, as data could come in at any time. IO::Select will let you know when something comes in on one of them.

There is a good example of creating the pipe in the section of perlipc entitled "Bidirectional Communication with Yourself". There

A simpler approach, which is more of a hack, really, is to just save some data in a temp file (i.e. "/tmp/$$.progname") and have the parent pick it up when the child is done. I would certainly have a go with the pipes before taking these comparatively drastic measures, though.
  • Comment on Re: forking: retaining variables after the child exits

Replies are listed 'Best First'.
Re: Re: forking: retaining variables after the child exits
by dws (Chancellor) on Nov 24, 2001 at 01:02 UTC
    If you're on a *nix platform, "shared memory" is an additional way that a child can communicate with a parent. This is definitely the "road less travelled," since shared memory isn't available on all platforms.

    Look in perlman:perlipc for the section on "SysV IPC". You'll find a short example, which might be enough to get you started.

    A number of folks recommend IPC::Shareable, with which I have no experience.

Re: Re: forking: retaining variables after the child exits
by thayer (Sexton) on Nov 24, 2001 at 09:06 UTC
    It sounds like the question comes from someone accustomed to an environment with threads, such as java. It might make sense to look at Thread.

    One small trick I'd add to the comments about IPC is that one can use the Data::Dumper or other persistence modules to pass the datastructures you've built up over a pipe, or in a file.