in reply to meddling with forces I can't understand

What sort of merging are you trying to do? How big are the files your primary program writes to disk?, are the individual files already sorted? Do you need to merge them in sorted order? I think, outside of the fork/exec pointers and discussion, some requirements about what the program needs to do might help with the overall answer.
  • Comment on Re: meddling with forces I can't understand

Replies are listed 'Best First'.
Re^2: meddling with forces I can't understand
by downer (Monk) on Oct 25, 2007 at 02:00 UTC
    I am merging files for an index, I worked around a hack to allow logarithmic merging without any heap structures just by clever tracking of numbers.

    I'd have the in-memory index construction worked out, and the merging of 2 sub-indexes also works nicely, this is the last step in getting it to work, i'd like to have merging of the old indexes going on while new indexes are made. I can also try adding this code to the separate merging file, calling itself recursively or iteratively.
      So, it looks like you're confident in the merging algorithm, so the real question is spawning the second process to merge the old indexes. Again, the pointers above from other monks are necessary reading if you're going to use fork() and exec(), but in general the following idiom is common:
      use POSIX ":sys_wait_h"; my %kids = (); # Signal Handler for SIGCHLD sub sigchld_handler{ while (($pid = waitpid(-1, &WNOHANG)) > 0) { delete $kids{$pid}; } $SIG{CHLD} = \&sigchld_handler; } $SIG{CHLD} = \&sigchld_handler; # You can repeat this for as many secondary # processes as you need to merge the old indexes for (1..3) { if (my $pid = fork) { # Parent Process keep track of forked children $kids{$pid} = 1; } else { # Child process # ... do merging of old indexes here sleep(3); exit 0; # MUST EXIT HERE } } while (keys %kids > 0) { sleep(1); # wait for all children to finish } exit 0;
      The SIGCHLD handler is there to let you know which of your child processing doing the merging completed successfully (or not), giving you a way to handle them properly. See the previously pointed to documentation about getting the actual exit value, signal and so forth from the completed child process. But, this framework should be enough to get you going if you have the merging algorithm down pat.