7

I've started playing with mmap. I'm trying to create an example workspace that will be then extended to the real case.

This is what I want to achieve:

PROCESS 1:

  • mmap a file (actually a device, but it's okay to generate an example with a text file)

PROCESS 2: (not foked from process 1; just an independent process)

  • read the memory mapped by process 1
  • change some bits
  • write it to a new file

I've read several examples and documentations, but I still didn't find how to achieve this. What I'm missing is:

  • how can process 2 access the memory mapped by process 1, without knowing anything about the opened file?
  • how can I put the mmap content in a new file? I suppose I have to ftruncate a new file, mmap this file and memcpy the content of process 1 memory map to process 2 memory map (then msync)

Side info, I have a message queue opened between the two processes, so they can share some messages if needed (ex. the memory address/size, ...).

Any hints?

Thanks in advance!

MIX

il_mix
  • 553
  • 8
  • 20

1 Answers1

6

This answer considers you are trying to do this stuff on linux/unix.

how can process 2 access the memory mapped by process 1, without knowing anything about the opened file?

Process 1 passes to mmap[1] the flag MAP_SHARED.

You can:

  • A) Share the file descriptor using unix domain sockets[2].
  • B) Send the name of the file using the queues you mentioned at the end of your message.

Process 2 opens mmap with the flag MAP_SHARED. Modifications to the mmaped memory in Process 1 will be visible for Process 2. If you need fine control of when the changes from process 1 are shown to process 2 you should control it with msync[3]

how can I put the mmap content in a new file? I suppose I have to ftruncate a new file, mmap this file and memcpy the content of process 1 memory map to process 2 memory map (then msync)

Why just don't write the mmaped memory as regular memory with write?

[1]http://man7.org/linux/man-pages/man2/mmap.2.html

[2]Portable way to pass file descriptor between different processes

[3]http://man7.org/linux/man-pages/man2/msync.2.html

Community
  • 1
  • 1
  • Thanks for your answer. I'll try to pass the file descriptor via message queue and see if everything works. I suppose I have to pass the memory size, too. About memcpy/msync vs write, I'll go for the former since, in the actual application, I'll have to do several small write on the same mmapped output file, and msync on the end. I think this will perform better with memcpy. – il_mix Apr 19 '16 at 11:51
  • Be carefull, you cannot pass file descriptors through a message queue. It won't work. You should use unix domain sockets (it makes some black magic to assign the internal stuff attached to a file descriptor in the receiving process). – Jon Ander Ortiz Durántez Apr 19 '16 at 12:03
  • Sure, I didn't read your answer carefully about this. Since the message queue is already up and running, I'll try sharing the file name. – il_mix Apr 19 '16 at 12:14
  • 1
    Marked as answer. Passing the file name (device name in the final project) and memory size (and offset, if needed) via message queue, than open() file and mmap the memory worked like a charm. THANKS! – il_mix Apr 21 '16 at 13:19