简体   繁体   English

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

[英]Message queue using shared memory/semaphore, how to check if if there is no process connected to the queue?

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目前正在处理消息队列,我想删除队列但为此我需要销毁信号量(sem_destroy)然后 shm_unlink 但我需要确保当前没有进程连接到队列

How can I achieve this please ?请问我怎样才能做到这一点?

Maybe what you want is a UNIX socket instead of a message queue.也许您想要的是 UNIX 套接字而不是消息队列。 Check man 7 unix , there's an example of how to use it.检查man 7 unix ,有一个如何使用它的例子。 With UNIX sockets you get:使用 UNIX 套接字,您可以获得:

  1. Bidirectional communication (Your code suggests you need that).双向通信(您的代码表明您需要它)。
  2. Connection/Disconnection detection (even in error/abort situations).连接/断开检测(即使在错误/中止情况下)。
  3. Packet behaviour with (SOCK_SEQPACKET). (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.消息队列可以在没有任何进程附加的情况下存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM