It actually sounds like you're describing POSIX shared memory.
Here is a quick pair of example programs to show how it works. On my system, the files get created in /run/shm (which is a tmpfs). Other systems use /dev/shm. Your program doesn't need to care, shm_open takes care of that.
server.c:
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
int fd;
long pagesize;
char *region;
if (-1 == (pagesize = sysconf(_SC_PAGE_SIZE))) {
perror("sysconf _SC_PAGE_SIZE");
exit(1);
}
if (-1 == (fd = shm_open("/some-name", O_CREAT|O_RDWR|O_EXCL, 0640))) {
perror("shm_open");
exit(1);
}
if (-1 == ftruncate(fd, pagesize)) {
perror("ftruncate");
shm_unlink("/some-name");
exit(1);
}
region = mmap(NULL, pagesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (!region) {
perror("mmap");
shm_unlink("/some-name");
exit(1);
}
// PAGESIZE is guaranteed to be at least 1, so this is safe.
region[0] = 'a';
sleep(60);
shm_unlink("/some-name");
}
client.c
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
int fd;
long pagesize;
char *region;
if (-1 == (pagesize = sysconf(_SC_PAGE_SIZE))) {
perror("sysconf _SC_PAGE_SIZE");
exit(1);
}
if (-1 == (fd = shm_open("/some-name", O_RDONLY, 0640))) {
perror("shm_open");
exit(1);
}
region = mmap(NULL, pagesize, PROT_READ, MAP_SHARED, fd, 0);
if (!region) {
perror("mmap");
shm_unlink("/some-name");
exit(1);
}
// PAGESIZE is guaranteed to be at least 1, so this is safe.
printf("The character is '%c'\n", region[0]);
}
Makefile
LDFLAGS += -lrt
all: server client