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.
source share