On 23 Mar 2004, Daniel Berger wrote:

> Hi all,
> 
> Ruby 1.8.1
> Solaris 9
> mmap 0.2.4
> 
> I was just futzing with mmap and I tried this:
> 
> require "mmap"
> 
> # test.txt contains:
> # "The quick brown fox jumped over the lazy dog's back"
> # three times, one per line.
> file = "test.txt"
> 
> m = Mmap.new(file,"rw")
> 
> pid = fork
> 
> fork{
>    m.gsub!(/fox/,"badger")
> }
> 
> Process.wait
> m.gsub!(/badger/,"sloth")
> m.unmap
> 
> This mostly works as expected, except that the 3rd line of the text
> file ends up with the last 7 characters chopped off.  What did I do
> wrong?

i know the system call is safe across forking - but it seems like there might
be some shared state in the ruby binding that doesn't work too well across
forking.  this works though:

require "mmap"
require "tempfile"

txt = <<-txt
The quick brown fox jumped over the lazy dog's back
three times, one per line.
txt

tmp = Tempfile.new __FILE__
tmp.puts txt
tmp.close
file = tmp.path

unless fork
  m = Mmap.new(file,"rw")
  m.gsub!(/fox/,"badger")
  m.unmap
else
  Process.wait
  m = Mmap.new(file,"rw")
  m.gsub!(/badger/,"sloth")
  m.unmap
  puts(IO.read(file))
  exit
end


> 
> While I'm at it, does anyone have a "really cool tricks with mmap"
> paper laying around somewhere?

i manipulate satelite images with it all the time.  contact me offline and
i'll send you some code.

-a
-- 
===============================================================================
| EMAIL   :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE   :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL     :: http://www.ngdc.noaa.gov/stp/
| TRY     :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done 
===============================================================================