in reply to Fork novice requests help...

There are lots of solutions. You could open a pipe to each child process (instead of forking explicitly) and pass back information from the child to the parent. You could open a filehandle to a temporary file (there's a CPAN module for good temp files) and use that for each child... when you catch SIGCHLD, open the temporary file and append it to the log. You could tie a filehandle to a scalar (Tie::Handle, I believe) and print to that, though you might have to mark it as shareable. (Who knows how far the magic extends across forks?)

If I were you, I'd go the temporary files route. Getting flocks to work well across forks seems tedious to me.

Update: As Abigail points out, flocking after the fork is indeed the way to go. I'm backwards on this one! heh

Replies are listed 'Best First'.
Re: Re: Fork novice requests help...
by thor (Priest) on Jun 20, 2001 at 00:45 UTC
    I was also thinking that getting flock to work might be a hassle. I have been working on this for a bit and may have come up with a reasonable solution. If I have a temporary variable that is reserved for the purpose of saying "filehandle is in use", then I could fork off at my leisure, and upon their return, check to see if this variable is 0 or 1. If it is 0, set it to 1, write the results, and set it back to 0. If it is 1, sleep for a bit and try again. What does everyone think of that?
      Well, that's easy. I don't think much of that. You would either have to work with threads, or shared memory segments to communicate the state of the variable between the various processes. But, since each of the processes can actually change the variable, you need to implement some form of locking mechanism on the variable.... So, to avoid the "hassle" of locking files (which is really trivial in Unix), you need threads (which don't really work in Perl), or System V shared memory segments (a hassle), and on top of that, you need some form of locking anyway.

      -- Abigail

        The caveat that I am a "novice at forking" is right in the title of this thread. I do appreciate the advice though.