IPC消息队列。 msgrcv 系统调用。系统五、如何跳出循环
我有一个消息队列,我从中循环获取消息。问题是我不知道如何退出循环。 msgrcv 返回 size_t 类型,因此它会继续运行。我应该比较返回值的什么值才能退出循环?
while(msgrcv(msqid, &msgreceived, sizeof(msgreceived), BUFFER_CHANGED, 0) != -1){
printf("%d %d %d %d ",msgreceived.value0,msgreceived.value1,msgreceived.value2,msgreceived.value3);
I have a message queue from which I am getting messages in a loop. The problem is that I don't know how to exit the loop. msgrcv returns type size_t so it keeps going. What value should I compare the return value so I can exit the loop?
while(msgrcv(msqid, &msgreceived, sizeof(msgreceived), BUFFER_CHANGED, 0) != -1){
printf("%d %d %d %d ",msgreceived.value0,msgreceived.value1,msgreceived.value2,msgreceived.value3);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的最后一个参数不包含
IPC_NOWAIT
,因此msgrcv
在等待未来消息时会阻塞。如果没有立即读取的消息,
msgrcv(..., ... | IPC_NOWAIT)
将返回(size_t)-1
并设置errno = ENOMSG。
Because your last parameter does not include
IPC_NOWAIT
,msgrcv
blocks while waiting for future messages.If there is no message to be read immediately,
msgrcv(..., ... | IPC_NOWAIT)
will return(size_t)-1
and seterrno = ENOMSG
.