in reply to anyone have some good mmap examples?

I often use mmap for very fast file I/O. Here's an example of a simplistic grep, which is not quite twice as fast as a simple perl -ne 'BEGIN { $pat = shift } print if /$pat/o' on my system.
#!/usr/bin/perl use warnings; use strict; use Sys::Mmap; our $pat = shift; foreach my $a (@ARGV) { my $mmap; if (! -f $a) { warn "'$a' is not a regular file, skipping\n"; next; } open(F, "< $a") or die "Couldn't open '$a': $!\n"; mmap($mmap, 0, PROT_READ, MAP_SHARED, F) or die "mmap error: $!\n"; while ($mmap =~ m/($pat)/omg) { my $pos = pos($mmap)-length($1); # Find the beginning and end of this line my $first = rindex($mmap,"\n",$pos)+1; my $last = index($mmap,"\n",$pos)+1; print substr($mmap,$first,$last-$first); } munmap($mmap) or die "mmunmap error: $!\n"; close(F) or die "Couldn't close '$a': $!\n"; }

That doesn't really address sharing, though. Basically, if two programs mmap in the same file, when one makes changes the other will be able to see them, which provides a simple shared memory region.

Update: Fix bug matching first line of file.