Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Target platform is GNU/Linux.


Let's say I have:

void *p

I want to be create entrypoint for that internal memory in filesystem like:

/tmp/my_entry_point

And I'd like to be able to read that memory from within another proccess.

fd = open("/tmp/my_entry_point", ...)
read(fd, ...)

Is it possible to create and read such pseudo-device?

share|improve this question
add comment

1 Answer

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
share|improve this answer
    
POSIX shared memory may have the effect the questioner wants, but he asked for the resulting object to appear in the filesystem. The name parameter for shm_open(2) does not correspond to any filesystem entry. Indeed, on Linux, it cannot have embedded slashes, so it cannot be called /tmp/my_entry_point, as the questioner specified. Again, it may be a matter of overspecification in the question, but the point needs to be raised. –  Warren Young Aug 24 '12 at 18:40
    
@WarrenYoung that is definitely true, it's just a token that can be passed to another call to shm_open. Of course, on Linux, it is a file (as I mentioned), but generally isn't in /tmp. But yeah, I've assumed the question is slightly overspecified. –  derobert Aug 24 '12 at 18:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.