Linux共享内存

发布于 2024-08-31 22:39:03 字数 94 浏览 2 评论 0原文

*inux 编程中创建共享内存的函数将一个键作为其参数之一。

这个键的含义是什么?我该如何使用它?

编辑:

不是共享内存 ID

The function which creates shared memory in *inux programming takes a key as one of its parameters..

What is the meaning of this key? And How can I use it?

Edit:

Not shared memory id

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

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

发布评论

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

评论(2

胡大本事 2024-09-07 22:39:03

它只是一个 System V IPC(进程间通信)密钥,因此不同的进程可以创建或附加到同一共享内存块。该密钥通常使用 ftok() 创建,它将完整指定的文件名和项目 ID 转换为可用的密钥。

由于应用程序通常可以在其所有不同进程中使用相同的文件名(该文件名通常是与您的应用程序关联的配置文件),因此每个不同的进程都会获得相同的密钥(或者,如果您使用项目 ID 指定多个共享进程,则更有可能)内存段,同一组键)。

例如,我们曾经有一个应用程序,它使用由 lex/yacc 代码处理的配置文件,因此我们只为每个不同的共享内存块使用该路径名和一个项目 ID(根据进程的目的,有 3 个或 4 个)问题)。这实际上很有意义,因为它是从存储在共享内存块中的配置文件中解析和评估的数据。

由于系统上没有其他应用程序应该使用我们的配置文件来创建密钥,因此不存在冲突。密钥本身不仅限于共享内存,它还可以用于信号量和其他 IPC 机制。

It's just a System V IPC (inter-process communications) key so different processes can create or attach to the same block of shared memory. The key is typically created with ftok() which turns a fully-specified filename and project ID into a usable key.

Since an application can generally use the same filename in all its different processes (the filename is often a configuration file associated with your application), each different process gets the same key (or, more likely if your using the project ID to specify multiple shared memory segments, the same set of keys).

For example, we once had an application that used a configuration file processed by our lex/yacc code so we just used that pathname and one project ID for each different shared memory block (there were three or four depending on the purpose of the process in question). This actually made a lot of sense since it was the parsed and evaluated data from that configuration file that was stored in the shared memory blocks.

Since no other application on the system should be using our configuration file for making a key, there was no conflict. The key itself is not limited to shared memory, it could be used for semaphores and other IPC mechanisms as well.

夏了南城 2024-09-07 22:39:03

posix 共享内存函数(shm_open 等)具有更加用户友好的界面,因为它们可以接受应用程序必须使用的唯一文件名来打开相同的共享内存块。

话虽如此,通常也可以在 Linux 下打开 /dev/shm 中的文件,然后使用 MAP_SHARED 对其进行 mmap,其效果大致相同。

The posix shared memory functions (shm_open and friends) have a more user-friendly interface in that they can accept a unique filename which must be used by the applications to open the same shared memory block.

Having said that, it is generally also feasible to open a file in /dev/shm under Linux and then mmap it with MAP_SHARED which achieves much the same.

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