cunixmmap

UNIX C "reverse mmap": mapping address space to disk without actually writing it


In my program, I generate some files, write it to disk, and then feed it to a second program, which again reads it from disk, in my parent application. I delete these files right after the second program has finished execution.

I was wondering whether there was something like a "reverse mmap" that would make an address space accessible for another program via the file system. I could then "map" my address space to the filesystem, and program B could read it from there, thinking of it as regular files.

Any pointers?


Solution

  • System V shared memory is what you're looking for. See the shmat, shmdt, shmget, and related syscalls. But note that, like mmap'ing a file, this does not provide a simple read()/write() interface. It is potentially more performant but is also considerably more complicated. I've used SysV shared memory many times. It requires mutexes, either embedded in the shared memory or the SysV equivalent (see the semget and semop syscalls).