我对在 C/C++ 程序中删除消息队列的方式感到很困惑。 我看到了here那个
Removing a Message Queue
You can remove a message queue using the ipcrm command (see the
ipcrm(1)
reference page), or by callingmsgctl()
and passing theIPC_RMID
command code. In many cases, a message queue is meant for use within the scope of one program only, and you do not want the queue to persist after the termination of that program. Callmsgctl()
to remove the queue as part of termination.
然后是其他东西 mq_unlink
我很困惑现在有什么方法可以彻底去除消息队列
现在让我谈谈我面临的问题。
我在我的应用程序中创建了 2 个消息队列
现在突然有信号传来并将控制传递给信号处理程序。在信号处理程序中,我正在重新启动服务,在该服务中我遇到了一条错误消息:“Resource temporary Unavailable”
。我已经使用 mq_close()
关闭了队列之一的信号处理程序。可能是问题来了,因为我没有关闭另一个。但我的疑问是:
- 我需要关闭它吗?
- 我需要删除它吗?
- 如果必须删除它,是否需要使用
msg_ctl
或mq_unlink
?
最佳答案
首先,有两个不相关的消息队列实现,旧的 UNIX 系统 V,它使用 msgget()
、msgsnd()
和 msgrcv()
和较新的 POSIX 兼容代码 described here .
如果您使用的是 POSIX 版本,要仅在您的程序中关闭它,请使用 mq_close
,或者为所有可能打开它的程序完全销毁它,请使用 mq_unlink
.
如果您使用 System V 版本来关闭您必须使用的队列:
msgctl(MessageQueueIQ,IPC_RMID,NULL);
其中 MessageQueueIQ 是您队列的 ID。
回答您的其他问题,如果您使用的是 System V 消息队列,关闭它就足够了,如果您使用的是 POSIX 消息队列,则必须取消链接(这也会关闭它)。
关于c - 删除消息队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31671016/