共享内存 API,进程可以将共享内存附加到其他进程
任何人都可以研究一下这个并用 API 向我建议吗?
我们有用于进程的 API,可以创建共享内存和/或将共享内存附加到其自己的进程。但我没有找到一种 API 可以通过其他进程将共享内存附加到一个进程(例如,进程 A 应该调用一个 API(如 shmat())来将共享内存附加到进程 B)。
Can any one look into this and suggest me with an API.
We have APIs for a process which can create and/or attach a shared memory to its own process. But I don't find an API to attach a shared memory to one process by other process(for e.g., process A should call one API(like shmat()) to attach the shared memory to process B).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
共享内存不属于任何特定进程(除非您使用私有
IPC_PRIVATE
密钥创建它)。它属于系统。因此,当您将
shmget
与非私钥(以及IPC_CREAT
标志)一起使用时,您将创建一个共享内存块或附加到现有的共享内存块。您需要一种方法让两个进程使用相同的 IPC 密钥,这通常通过使用 ftok 来完成,它使用文件规范和标识符为您提供一个 IPC 密钥以在 shmget 中使用 调用(以及其他 IPC 类型调用,例如
msgget
或semget
)。例如,在程序
pax1
和pax2
中,您可能有这样的代码段:通过让两个进程使用相同的文件规范和 ID,它们将获得相同的文件规范和 ID。共享内存块。
您可以使用不同的 ID 为您提供基于同一文件的不同共享内存块(例如,您可能需要一个用于配置共享内存块,另一个用于存储共享状态)。
而且,考虑到它是 IPC 密钥所基于的你的配置文件,其他程序使用它的机会很小(我认为它可能为零,但我不能 100% 确定)。
您不能从进程外部强制将共享内存注入到该进程中(好吧,您也许可以,但这既危险又需要各种根级权限)。这会破坏受保护的进程模型,并使您的系统变得像 MS-DOS 一样安全:-)
Shared memory doesn't belong to any particular process (unless you create it with a private
IPC_PRIVATE
key). It belongs to the system.So, when you use
shmget
with a non-private key (and theIPC_CREAT
flag), you will either create a shared memory block or attach to an existing one.You need a way for both processes to use the same IPC key and this is often done by using
ftok
which uses a file specification and an identifier to give you an IPC key for use in theshmget
call (and other IPC type calls, such asmsgget
orsemget
).For example, in the programs
pax1
andpax2
, you may have a code segment like:By having both processes use the same file specification and ID, they'll get the same shared memory block.
You can use different IDs to give you distinct shared memory blocks all based on the same file (you may, for example, want one for a configuration shared memory block and another for storing shared state).
And, given that it's your configuration file the IPC key is based on, the chances of other programs using it is minuscule (I think it may be zero but I'm not 100% sure).
You can't forcefully inject shared memory into a process from outside that process (well, you may be able to but it would be both dangerous and require all sorts of root-level permissions). That would break the protected process model and turn you system into something about as secure as MS-DOS :-)
让我们看看,允许一个进程将共享内存段强制转移到另一个进程上?接收者将用它做什么?它如何知道它现在已经将该块映射到了对它的期望中。
您以错误的方式思考问题 - 简单地将一块内存提升到第二个进程不会让您做您想做的事。您还需要通知第二个进程,它现在已经映射了该块,因此可以开始使用它进行操作。我建议你退后一步,认真审视你的设计和你正在做的事情。我推荐的方法是
至于将共享内存包装在一个好的库中 - 考虑 boost::interprocess。
Let's see, allow one process to force a shared memory segment on to another? What is the receiver going to do with it? How will it know it now has mapped this block in - what is expected of it.
You're thinking about the problem the wrong way - simply hoisting a block of memory on to a second process is not going to allow you to do what you want. You need to notify the second process also that it has now mapped this block and so can start doing stuff with it. I suggest you take a step back and really look at your design and what you are doing. My recommended approach would be
As for wrapping shared memory in a nice library - consider boost::interprocess.
您要求附加其他进程的进程内存,对吧?
只需
open(2)
文件/proc//mem
并使用它。检查/proc//map
文件中可用地址的列表。You are asking to attach the process memory of other process, right?
Just
open(2)
the file/proc/<pid>/mem
and use it. Check the/proc/<pid>/map
for the list of usable address in the file.