Check Unix Message Queue if it is empty or not.

Can someone tell me how to check if there is any message in the message queue. Message Queuing is implemented in C on a Linux-based operating system. I just want to check if there is any message in the message queue at a specific time.

+4
source share
1 answer

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

msgctl() 

and by examining the msqid_ds structure upon return, msg_qnum in this structure represents the number of messages in the queue. Here is a link to an example: msgctl example , it does more than you want, but after calling msgctl () you just need to check that the 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 do only what you want.

+6
source

Source: https://habr.com/ru/post/1435653/


All Articles