如何清理 IPC 消息队列?
我在基于 IPC 的应用程序中使用 msgget()
函数。如何清理充满旧消息队列的队列?
I am using msgget()
function in my IPC based application. How can I clean up the queue filled up with old message queues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要删除队列,请使用以下命令:
系统调用:msgctl()
To delete a queue, use the following command:
SYSTEM CALL: msgctl()
您可以使用mq_setattr更改O_NONBLOCK的消息队列属性。
然后通过读取所有消息来清空队列,直到返回值指示队列为空。
现在恢复旧属性。
此方法不是运行时优化的,但它避免了关闭和打开消息队列的需要。
You can change the message queue attribute for O_NONBLOCK by using mq_setattr.
Then empty the queue by reading all of the messages, until the returned value indicates the queue is empty.
Now set back the old attributes.
This method is not a run time optimized, but it avoids the need to close and open the message queue.
解决方法是增加 MSGMNI 系统范围内消息队列的最大数量:取决于策略(在 Linux 上,可以通过 /proc/sys/kernel/msgmni 读取和修改此限制>)。
A work around is to increase MSGMNI System wide maximum number of message queues: policy dependent (on Linux, this limit can be read and modified via /proc/sys/kernel/msgmni).
这些持久的资源分配问题(共享内存也有类似的问题)是 System V API 通常被认为已弃用的原因。在这种情况下,您是否考虑过使用unix域套接字或FIFO来代替消息队列?它们出现在文件系统中,当不再使用 rm 等工具时可以“清理”。
These persistent resource allocation issues (there's a similar one with shared memory) are why the System V APIs are generally considered deprecated. In this case, have you considered using a unix domain socket or FIFO instead of a message queue? Those appear in the filesystem, and can be "cleaned up" when no longer used with tools like rm.