Forking in Perl 2

Build up an index of files to process, e.g. SAM files. Fork out 16 child processes, each time processing and eliminating one file from the index. As with all my code, use at your own risk. Comments and suggestions always welcome.

#!/usr/bin/perl

use strict;
use warnings;

my $fork_process = '16';
my @file_for_processing = ();
my @child = ();

opendir(DIR,'.') || die "Could not open current directory: $!\n";
while(my $file = readdir(DIR)){
   next unless $file =~ /\.sam.gz$/;
   push(@file_for_processing,$file);
}
closedir(DIR);

while(scalar(@file_for_processing) > 0){
   for (1 .. $fork_process){
      my $pid = fork();
      if ($pid) {
         # parent
         push(@child, $pid);
         pop(@file_for_processing);
      } elsif ($pid == 0) {
         # child
         if (scalar(@file_for_processing) > 0){
            print "CHILD: processing $file_for_processing[-1]\n";
         }
         exit(0);
      } else {
         die "Couldn't fork: $!\n";
      }
   }
   foreach my $pid (@child) {
      waitpid($pid, 0);
   }
}

exit(0);

For more information see Forking in Perl.

Print Friendly, PDF & Email



Creative Commons License
This work is licensed under a Creative Commons
Attribution 4.0 International License
.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.