Passing a pointer to process spawned with exec()

后端 未结 6 1290
花落未央
花落未央 2021-01-23 14:44

I would like to pass a pointer (I am putting a file with data in memory with mmap) to processes spawned using fork + exec, but I am stuck on how to pass a pointer to the exec()

相关标签:
6条回答
  • 2021-01-23 14:58

    Consider passing the offset to the memory within the file to the child process. If the offset is zero, then don't bother, but if you need to pass a 'pointer' to part way through the file, then convert that to an offset from the start address, and pass that to the child. The child can then get to the data by adding the offset to the address it obtains for the mapped file.

    0 讨论(0)
  • 2021-01-23 14:59

    The spawned process should probably open a pipe back to the parent process and ask for the data it needs to map the shared memory segment.

    Alternatively you can use boost::interprocess to create a shared memory segment for you and actually pass around the address (it can do the mapping). You're on your own reading that documentation though: http://www.boost.org/doc/libs/1_38_0/doc/html/interprocess.html

    0 讨论(0)
  • 2021-01-23 15:03

    Just pass as text in command-line argument, or in environment variable.

    0 讨论(0)
  • 2021-01-23 15:14

    This can't work. The new process should mmap the file itself as well.

    0 讨论(0)
  • 2021-01-23 15:15

    This is a big area, and you have a lot to choose from.

    The key finding those solution is to search for something like Linux inter processor communication or maybe Linux IPC.

    A intro into IPC can also be found in books like, Advance Linux Programming (ISBN: 0-7357-1043-0)

    0 讨论(0)
  • 2021-01-23 15:19

    If you use shared memory, you can't pass the pointer. The pointer will contain the virtual address, which is different from one process to another. You have to exchange offset values, based on the start of the shared memory area.

    If you don't use shared memory, you can't exchange pointers of any kind: The other process won't be able to access the memory of your process.

    0 讨论(0)
提交回复
热议问题