将指针传递给由 exec() 生成的进程

发布于 2024-07-14 21:44:19 字数 618 浏览 7 评论 0原文

我想将一个指针(我使用 mmap 将一个包含数据的文件放入内存中)传递给使用 fork + exec 生成的进程,但我不知道如何将指针传递给 exec() 生成的进程?

UPDATE1:

感谢您的输入,我确实使用共享内存通过带有 MAP_INHERIT 标志的 mmap 创建它:

使用 mmap() 函数创建的每个映射文件和共享内存区域 通过成功调用任何 exec 函数(除了那些函数)取消映射 使用 MAP_INHERIT 选项映射的区域。 映射的区域 MAP_INHERIT 选项在新进程映像中保持映射。

来源: http://www.uwm .edu/cgi-bin/IMT/wwwman?topic=exec(2)&msection=

UPDATE2:

这是家庭作业练习,但我认为我必须停止考虑指针并考虑 IPC 本身。 我想我会尝试在子进程中映射相同的文件。

非常感谢简短的代码示例。

在此先感谢您的帮助。

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() spawned process?

UPDATE1:

Thanks for your inputs, I do use shared memory creating it with mmap with MAP_INHERIT flag:

Each mapped file and shared memory region created with the mmap() function
is unmapped by a successful call to any of the exec functions, except those
regions mapped with the MAP_INHERIT option. Regions mapped with the
MAP_INHERIT option remain mapped in the new process image.

source: http://www.uwm.edu/cgi-bin/IMT/wwwman?topic=exec(2)&msection=

UPDATE2:

This is homework excercise, but I think I must stop thinking about pointers and think about the IPC itself. I guess I will go with trying to mmap the same file in child process.

Short code example much appreciated.

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

写下不归期 2024-07-21 21:44: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.

痴者 2024-07-21 21:44:19

这是行不通的。 新进程也应该 mmap 文件本身。

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

逆光飞翔i 2024-07-21 21:44:19

生成的进程可能应该打开一个返回父进程的管道,并请求映射共享内存段所需的数据。

或者,您可以使用 boost::interprocess 为您创建一个共享内存段,并实际传递地址(它可以进行映射)。 不过,您需要自己阅读该文档: http:// /www.boost.org/doc/libs/1_38_0/doc/html/interprocess.html

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

瞄了个咪的 2024-07-21 21:44:19

考虑将文件内内存的偏移量传递给子进程。 如果偏移量为零,则不必打扰,但如果您需要传递一个“指针”到文件的一部分,则将其转换为距起始地址的偏移量,并将其传递给子级。 然后,子进程可以通过将偏移量添加到它为映射文件获得的地址来获取数据。

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.

撩动你心 2024-07-21 21:44:19

只需在命令行参数或环境变量中作为文本传递。

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

被翻牌 2024-07-21 21:44:19

这是一个很大的区域,你有很多选择。

找到这些解决方案的关键是搜索类似 Linux 处理器间通信 或者Linux IPC

IPC 的介绍也可以在《高级 Linux 编程》(ISBN:0-7357-1043-0)等书籍中找到。

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文