1

I'd like to be able to share a pre-allocated memory between processes.

Searching for an example to do it, I can only find a way to create a new file with shm_open, then use mmap and memcpy. This is a problem since the buffers are quite large and I don't control their allocation (API of a camera).

Is there a way to take an existing, pre allocated buffer, and share its content with another process, without memcpy, using mmap? or using another method other than mmap?

miluz
  • 1,353
  • 3
  • 14
  • 22
  • If you expect the memory buffer to be shared, why not create it as a shared memory segment to begin with? Even if it's not actually shared with anyone, you can still use it like any other in-memory buffer. – Some programmer dude Mar 03 '21 at 15:10
  • The call for creating the buffer is out of my control: an external API for a camera allocates the buffer. If it was possible I'd do it exactly as you suggest: create the shared memory and pass the virtual address to the camera API. Unfortunately the API does not support working with a pre allocated buffer. – miluz Mar 03 '21 at 15:30
  • Have you tried to use IPC Sockets or IPC Pipes. If you are not familiar with the concept, [check here](https://www.mtholyoke.edu/courses/dstrahma/cs322/ipc.htm). The socket will allow you to send buffers across to another process. – jordanvrtanoski Mar 03 '21 at 15:43
  • 2
    Could the other process be made into a thread instead? – Nate Eldredge Mar 03 '21 at 16:14
  • @jordanvrtanoski: This still involves copying the data at least once, and requires system calls to boot. – Nate Eldredge Mar 03 '21 at 16:15
  • yes, it involves a system call to the kernel. Some new kernels are smart and they can create a "copy on write" buffer to save on the copy part, which if you intend to modify the buffer will not help much. Any IPC requires a system call to the kernel. Even if you use shared memory, you need to have mutex to prevent one process writing wile the other one is reading which requires system call. As @NateEldredge mentioned, can you make them a threads under same process, then you can use same buffer. – jordanvrtanoski Mar 03 '21 at 16:33
  • I guess the other question that should be asked is, can you actually demonstrate that the copy is a bottleneck and that the time it takes is significant. – Nate Eldredge Mar 03 '21 at 17:21
  • I get the feeling that the answer to the question is 'no' or 'no easy way'. This is also a valid answer :) As for alternatives suggestions, it's not yet demonstrated to be a bottleneck, so I'm keeping the memcpy's. Thanks for your feedback, I appreciate it! – miluz Mar 04 '21 at 13:16

0 Answers0