#!/usr/bin/perl
use warnings;
use strict;
use Sys::Mmap;
use constant SEGMENT_SIZE => 256;
use constant MAX_SEGMENTS => 8192/SEGMENT_SIZE;
my $file = shift;
my $mmap;
if (! -f $file)
{
die "'$file' is not a regular file\n";
}
open(F, "+< $file")
or die "Couldn't open '$file': $!\n";
mmap($mmap, 0, PROT_READ|PROT_WRITE, MAP_SHARED, F)
or die "mmap error: $!\n";
my $segment = 0;
while (<>)
{
if (length > (SEGMENT_SIZE-1))
{
warn "Line too long\n";
next;
}
$segment = ($segment + 1 ) % MAX_SEGMENTS;
substr($mmap,1+SEGMENT_SIZE*$segment,1)=chr(length);
substr($mmap,2+SEGMENT_SIZE*$segment,length)=$_;
substr($mmap,0,1)=chr($segment);
}
####
#!/usr/bin/perl
use warnings;
use strict;
use Sys::Mmap;
use constant SEGMENT_SIZE => 256;
use constant MAX_SEGMENTS => 8192/SEGMENT_SIZE;
my $file = shift;
my $mmap;
if (! -f $file)
{
die "'$file' is not a regular file\n";
}
open(F, "< $file")
or die "Couldn't open '$file': $!\n";
mmap($mmap, 0, PROT_READ, MAP_SHARED, F)
or die "mmap error: $!\n";
my $last_seg = -1;
while (1)
{
my $segment = ord(substr($mmap,0,1));
next if ($segment == $last_seg);
my $len = ord(substr($mmap,1+SEGMENT_SIZE*$segment,1));
my $line = substr($mmap,2+SEGMENT_SIZE*$segment,$len);
print $line;
$last_seg = $segment;
}
####
dd if=/dev/zero bs=1 count=8192 of=mm