#!/usr/bin/perl use List::Util 'shuffle'; # standard from Perl 5.8 and later my $tempfile = "multigcc.out"; my @params; my @files; my $list = \@params; # parse command line arguments for my $a (@ARGV) { if ($a eq "--") { $list = \@files; next; } push @{$list}, $a; } my $command = join " ", @params; # shuffle the file list to spread the load as evenly as we can @files = shuffle(@files); # count number of cores my $cores = 1; if (open CPUINFO, "; close CPUINFO; } # don't run empty children if (scalar @files < $cores) { $cores = 1; } # fork children my @pids; my $slice = int((scalar @files + $cores) / $cores); for my $i (0 .. $cores-1) { my $pid = fork; if ($pid) { # mother $pids[$i] = $pid; } else { # get my slice of the files my @list = @files[$i * $slice .. $i * $slice + $slice - 1]; # run command system("$command @list > $tempfile.$$"); exit; } } for my $i (0 .. $cores - 1) { # wait for child to complete waitpid $pids[$i], 0; # read & print result if (open F, "<$tempfile.$pids[$i]") { print ; close F; unlink "$tempfile.$pids[$i]"; } }