简体   繁体   中英

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

How can I achieve this please ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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