传递一个指向使用exec()生成的进程的指针 [英] Passing a pointer to process spawned with exec()

查看:83
本文介绍了传递一个指向使用exec()生成的进程的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向使用fork + exec生成的进程传递一个指针(我正在使用mmap将一个带有数据的文件放入内存中),但是我仍然坚持如何将指针传递给exec()生成的进程? /p>

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.

解决方案

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.

这篇关于传递一个指向使用exec()生成的进程的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆