Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Unix Message Queue if empty or not

Can some one tell me how to check if there is any message in message queue. the message queue is implemented in C in linux based operating system. I just want to check if there is any message in the message queue at a particular time.

like image 815
Aman Singhal Avatar asked Sep 22 '12 11:09

Aman Singhal


People also ask

How do I check if a queue is empty?

queue. isEmpty() returns true if the queue is empty; otherwise, it returns false .

How do I check the queue in Linux?

There are two ways: Use the Unix command ipcs to get a list of defined message queues, then use the command ipcrm to delete the queue.

Is Msgrcv blocked?

Operations to send and receive messages are performed by the msgsnd(2) and msgrcv(2) functions, respectively. When a message is sent, its text is copied to the message queue. The msgsnd(2) and msgrcv(2) functions can be performed as either blocking or non-blocking operations.

How do you test for an empty queue in C?

isEmpty/isFull- checks if the queue is empty or full.


1 Answers

Just checking the amount (if any) of messages is done using the

msgctl() 

function, and examining the msqid_ds structure on return, the msg_qnum in this structure is the amount of messages in the queue. Here is a link with an example: msgctl example, it does more then you want, but after the msgctl() call you just have to check that field in the structure I mentioned above.

#include <sys/msg.h>

main() {
  int msqid = 2;
  int rc;
  struct msqid_ds buf;
  int num_messages;

  rc = msgctl(msqid, IPC_STAT, &buf);
  num_messages = buf.msg_qnum;
}

This example should do what you want, and only do what you want.

like image 103
ikku Avatar answered Sep 29 '22 22:09

ikku