System call to map memory to file descriptor (mmap reverse)?

I want to be able to map memory to a file descriptor, so I can use some existing functions that need a file descriptor. Here, in essence, I'm looking for:

void do_operation1(int fd); char data[DATA_MAX] = { /* embedded binary data */ }; int fd = addr_to_fd(data, DATA_MAX); do_operation1(fd); /* ... operate on fd ... */ 

What system call or calls can I use to accomplish this?

+6
c unix posix system-calls mmap
source share
3 answers

You should check shm_open() .

+3
source share

Some implementations have fmemopen() . (Then, of course, you should call fileno() ).

If you do not, you can create it yourself using fork() and pipe() .

+7
source share

Of course, just open(argv[0], ...) , scan the file to find where your binary data starts, lseek() there. Of course, this file will not have the length of your binary data.

+2
source share

All Articles