Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Mmap question

by sgifford (Prior)
on Jan 31, 2005 at 02:15 UTC ( [id://426481]=note: print w/replies, xml ) Need Help??


in reply to Mmap question

You can certainly use mmap to share between unrelated processes. Create a file of the appropriate size, then mmap it from two processes; each will be able to see the other's changes.

Here's an example. mmshare writes lines from its STDIN to an mmap'd file, and mmlisten waits for that, then prints them. You have to be a bit careful how you write to the file; writing more than one machine word isn't atomic. So these programs divide the file up into segments, and use the first byte as a segment identifier which can be read and written to atomically.

mmshare:
#!/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); }

mmlisten:

#!/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; }

To use them, first create an empty file to mmap:

dd if=/dev/zero bs=1 count=8192 of=mm
then run mmlisten mm in one window, and mmshare mm in another. As you type lines into mmshare, they should show up in the mmlisten window.

Update: To answer Aristotle's question, AFAIK there's no way to share an anonymous mmap region between unrelated processes.

To answer leriksen's question, it's actually sharing the memory backed by the file, just as in some OS's all real memory is backed by swap (Solaris, IIRC). Changes are available immediately, at least as immediately as with any other shared memory scheme. It does periodically write the data out to disk, but changes are visible before it's written out.

Replies are listed 'Best First'.
Re^2: Mmap question
by leriksen (Curate) on Jan 31, 2005 at 12:14 UTC
    Well I guess thats true, but isnt that more a case of sharing the file, rather than the memory ?

    I believe the OP wanted to share the memory allocated my mmap (sharing the address returned by mmap, which isnt possible across unrelated processes).

    Maybe that just being pedantic - changes made to the mmap'ed region by one process are almost immediately visible by the other - if thats the level of control required, cool. shmXXX allows for finer grained control, by using data structures in memory - possible in a file but not without some serious work serialising to and from the file representation.

    ...it is better to be approximately right than precisely wrong. - Warren Buffet

      I don't understand this:
      shmXXX allows for finer grained control, by using data structures in memory - possible in a file but not without some serious work serialising to and from the file representation.
      How does a SysV shared memory segment support data structures differently than mmap? I've always used them pretty much interchangeably...
Re^2: Mmap question
by Aristotle (Chancellor) on Jan 31, 2005 at 07:29 UTC

    Right, but there's no way to share an mmap between two processes without using a file, is there?

    Update: without, not with. Way to look dumb, Aristotle.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://426481]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-26 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found