简体   繁体   English

如果为空,请检查Unix Message Queue

[英]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. 消息队列在基于Linux的操作系统的C中实现。 I just want to check if there is any message in the message queue at a particular time. 我只想检查特定时间消息队列中是否有任何消息。

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. 函数,并在返回时检查msqid_ds结构,此结构中的msg_qnum是队列中的消息量。 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. 这是一个带有示例的链接: msgctl示例 ,它可以执行更多操作,但在msgctl()调用之后,您只需检查上面提到的结构中的该字段。

#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. 这个例子应该做你想要的,只做你想做的事。

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

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