在不相关的进程之间使用消息队列

发布于 2024-08-09 23:57:43 字数 271 浏览 2 评论 0原文

我正在尝试使用消息队列在 Linux 中两个不相关的进程之间进行通信。我知道使用公共键值将允许我们在两个不相关的进程中打开相同的消息队列。但问题是,有时与消息队列对应的键值可能已被某些系统实用程序使用。所以我使用 IPC_CREAT | IPC_EXEL 标志确保强制创建新的消息队列。

如果 msgget() 返回错误,我会增加键值并重试创建新的消息队列,直到成功。现在的问题是如何将这个成功的键值传递给想要在同一消息队列上操作的其他不相关的进程。

我错过了什么吗?以前有人遇到过类似的问题吗?

I am trying to use a message queue for communication between two unrelated processes in Linux. I am aware that using a common key value will allow us to open the same message queue in both the unrelated processes. But the problem is that at times a key value corresponding to a message queue might already be used by some system utility. So I am using IPC_CREAT | IPC_EXEL flags to ensure that a new message queue is compulsorily created.

In case msgget() returns an error, I increment the key value and retry to create a new message queue, till it succeeds. Now, the problem is how do I communicate this successful key value to the other unrelated process which wants to operate on the same message queue.

Am I missing something? Has anyone faced similar issues before?

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

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

发布评论

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

评论(3

◇流星雨 2024-08-16 23:57:43

使用 ftok(3)。它需要一个路径名和一个任意整数 id(仅使用低 8 位,因此通常使用字符)并生成一个可能唯一的 key_t。它不保证是唯一的,但使用 id,您可以使用相同的路径名,并在发生冲突时为 id 指定增量。

典型的执行是:

Primary 使用 ftok(3) 来获取密钥。

Primary 使用标志来指定它需要一个新的消息队列。出错时,增加 id 并调用 ftok,直到获得合适的消息队列。消息队列是使用特定于辅助进程运行的用户/组的权限创建的(尽可能窄,最好是进程唯一的)

辅助进程使用 ftok 获取起始密钥,然后使用 msgget,递增 id 并重用 ftok按照事先确定的方式,直到成功获取消息队列密钥。

Use ftok(3). It takes a pathname and an arbitrary integer id (uses only lower 8 bits, so a character is usually used) and generates a probably unique key_t. It's not guaranteed to be unique, but using the id you can use the same pathname and have prescribed increments for the id if there is a collision.

Typical execution would be:

Primary uses ftok(3) to obtain a key.

Primary uses the flags to specify it wants a new message queue. On error, increments the id and recalls ftok until it gets a suitable message queue. Message queue is created with permissions specific to the user/group that the secondary process runs as (as narrow as is possible, preferably unique to the process)

Secondary uses ftok to obtain the starting key, then uses msgget, incrementing the id and reusing ftok as determined beforehand until it successfully gets the message queue key.

等数载,海棠开 2024-08-16 23:57:43

IIRC,这正是 ftok(3)就是用来解决的。需要通信的程序只需使用公共路径和键值即可。我们过去常常在命令行上将 FIFO 的名称传递给我们的程序。他们会将其传递给 ftok() 来生成我们共享内存段的密钥。

IIRC, this is exactly the problem that the ftok(3) is used to solve. The programs that need to communicate simply use a common path and key value. We used to pass the name of a FIFO on the command line to our programs. They would pass this into ftok() to generate the key for our shared memory segments.

小苏打饼 2024-08-16 23:57:43

您可以简单地进入 21 世纪并使用 Posix 消息队列而不是 sysV 消息队列。 Posix 队列有名称而不是晦涩的数字。在 Linux 下,您可以挂载文件系统来查看它们,并使用“rm”删除它们。

You could simply move into the 21st century and use a Posix message queue rather than a sysV one. Posix queues have names rather than obscure numbers. Under linux you can mount a filesystem to see them and remove them with "rm".

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