Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re: Mmap questionby sgifford (Prior) |
on Jan 31, 2005 at 02:15 UTC ( [id://426481]=note: print w/replies, xml ) | Need Help?? |
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:
mmlisten:
To use them, first create an empty file to mmap: 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.
In Section
Seekers of Perl Wisdom
|
|