消息队列使用共享内存/信号量,如何检查是否没有连接到队列?

发布于 2025-02-10 15:32:50 字数 270 浏览 1 评论 0原文

typedef struct
{
    char data[MESSAGE_SIZE];
} MESSAGE;

typedef struct
{
    sem_t sem_send;
    sem_t sem_receive;
    MESSAGE messages[];
} MQ;

目前正在处理消息队列,我想删除队列,但要这样做 我需要销毁信号量(sem_destroy)然后shm_unlink 但是我需要确保目前没有连接到队列的过程

如何实现?

typedef struct
{
    char data[MESSAGE_SIZE];
} MESSAGE;

typedef struct
{
    sem_t sem_send;
    sem_t sem_receive;
    MESSAGE messages[];
} MQ;

Currently working on message queue, I want to delete the queue but in order to do so
I need to destroy the semaphore (sem_destroy ) then shm_unlink
but I need to be sure that there is no process currently connected to the queue

How can I achieve this please ?

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

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

发布评论

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

评论(1

霞映澄塘 2025-02-17 15:32:50

也许您想要的是UNIX插座而不是消息队列。检查MAN 7 UNIX,有一个如何使用它的示例。使用UNIX插座,您会得到:

  1. 双向通信(您的代码建议您需要)。
  2. 连接/断开连接检测(即使在错误/流产情况下)。
  3. 带有(sock_seqpacket)的数据包行为。就像消息队列一样。

消息队列没有任何有关连接到它的过程的信息。消息队列可以存在,而无需附加任何过程。

Maybe what you want is a UNIX socket instead of a message queue. Check man 7 unix, there's an example of how to use it. With UNIX sockets you get:

  1. Bidirectional communication (Your code suggests you need that).
  2. Connection/Disconnection detection (even in error/abort situations).
  3. Packet behaviour with (SOCK_SEQPACKET). Like a message queue.

The message queues don't tell anything about other processes connecting to it. Message queues can exist without any process attach to it.

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