Created
October 25, 2018 08:57
-
-
Save jef-sure/fe61ea3ea90e02298680b851603ae7a6 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 $processes = $ARGV[0] || 1; | |
my $block_size = 64 * 1024 * 1024; | |
my $file = 'very_big_file'; | |
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