On Thu, Apr 10, 2003 at 04:48:10PM -0700, David Wuertele wrote: > If mmap maps a file or device into memory, how do I do the reverse? > I have some already-allocated memory, and I want to create a file > descriptor from it that I can pass around. > > Apparently Windows can do this: you can create a handle from some > arbitrary region of user-allocated memory. Can Linux? No. What you _can_ do is create the file with open(2) or creat(2), then write the contents of your buffer to file using write(2) or pwrite(2). However, once your buffer is written, it is _not_ tried to the file in any way, so it may become out of date if some other task modifies the file. If you don't mind race conditions, you could write a function to make it appear to your process that this is what happens, by creating the file, performing a write, calling free() on the space you've already got, mmaping the file back into memory, and then returning the new pointer. I hope this helps. -- "Soldiers quartered in a populous town will always occasion two mobs where they prevent one. They are wretched conservators of the peace." -- John Adams
Attachment:
pgp00365.pgp
Description: PGP signature