문제

Motivation

I'd like to write a pair of programs where one program reads data and transfers it into an internal format and the other program transfers the internal format into something else. As an excercise, I want to code the interaction between these programs without the use of pipes. I rather like to use signals and shared memory.

What I want

I have programs A and B where A calls B. How can I

  1. Create a memory block from program A
  2. Call program B from program A, giving it the information about where to find the memory block
  3. Use the memory block from both programs.

More specific, A decodes a custom video format and places a single uncompressed frame into a shared buffer. B reads from the buffer and encodes it into an output strean. The decoder decodes up to 100 frames per second which is about 500 MiB/s memory traffic. Pipes turned out to be slow since the data has to be copyied much too often and the buffers aren't exactly big.

What I think might work

My idea is to use mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED|MAP_ANONYMOUS,-1,0) to create a section of shared memory. The problem is, that the manpage of execve(2) states:

Memory mappings are not preserved (mmap(2)).

So, how can I share that memory with the other program? Is it a better idea to put both functionality into one program and use fork instead?

도움이 되었습니까?

해결책

*nix systems have several APIs for shared memory:

  • BSD: use mmap(). To share memory between unrelated processes, you associate it to a file. You can also share memory between related processes using MAP_ANONYMOUS and fork()ing (but make sure to use -1 as fd for portability).
  • System V: (some people really dislike this one) use shmget() to create/get a reference to a shared memory area, attach to it via shmat(), detach via shmdt(), mark for deletion with shmctl(). You identify shared memory areas by a key, which should be unique.
  • POSIX: use shm_open() then mmap() from the returned file descriptor.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top