-
-
Save cynovg/7ad54925d9be19feb931ba4d95bc332f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
my $file = $ARGV[0] || die "USAGE: $0 filename"; | |
my $processes = $ARGV[1] || 1; | |
my $block_size = 64 * 1024 * 1024; | |
my $fsize = -s $file or die 'no file'; | |
my @offsets; | |
my @sizes; | |
my @pids; | |
for my $part (0 .. $processes - 1) { | |
$offsets[$part] = int($fsize / $processes * $part); | |
$sizes[$part] = int($fsize / $processes * ($part + 1)) - $offsets[$part]; | |
if (not($pids[$part] = fork)) { | |
open my $fi, '<', $file or die; | |
my $read = 0; | |
for (my $pos = 0; $pos < $sizes[$part]; $pos += $read) { | |
my $rs = $pos + $block_size > $sizes[$part] ? $sizes[$part] - $pos : $block_size; | |
$read = read($fi, my $buff, $block_size); | |
} | |
exit; | |
} | |
} | |
for my $pid (@pids) { | |
waitpid($pid, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment