2 use List
::Util
'shuffle'; # standard from Perl 5.8 and later
4 my $tempfile = "multigcc.out";
9 # parse command line arguments
19 my $command = join " ", @params;
21 # shuffle the file list to spread the load as evenly as we can
22 @files = shuffle
(@files);
24 # count number of cores
26 if (open CPUINFO
, "</proc/cpuinfo") {
27 $cores = scalar grep /^processor/i, <CPUINFO
>;
31 # don't run empty children
32 if (scalar @files < $cores)
39 my $slice = int((scalar @files + $cores) / $cores);
40 for my $i (0 .. $cores-1)
50 # get my slice of the files
51 my @list = @files[$i * $slice .. $i * $slice + $slice - 1];
54 system("$command @list > $tempfile.$$");
60 for my $i (0 .. $cores - 1)
62 # wait for child to complete
66 if (open F
, "<$tempfile.$pids[$i]")
70 unlink "$tempfile.$pids[$i]";