Ubuntu,POSIX mq_receive应该阻止它

Vla*_*oiu 3 c posix

我陷入了POSIX队列的起点:

什么(我认为)应该阻止根本不阻塞,并且循环不断旋转.我试图阻止进程mq_receive直到某些消息到达,但看起来呼叫始终从队列中收到一条空消息(但是还没有任何客户端发送消息).使用默认设置正确打开队列mq_flags=0.

我正在运行Ubuntu 12.04.

这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>

int main(int argc, char **argv) {
    mqd_t qd;
    qd = mq_open("/tempqueue", O_RDONLY | O_CREAT, 0666, NULL);
    if (qd == (mqd_t) -1){
        printf("Problemz");
        return 1;
    }else{
        printf("Coda creata\n");
    }

    char buf[400];

    while(1){
        mq_receive(qd, buf, 400, NULL);
        printf("Ricevo: %s.\n", buf);
    }

    mq_close(qd);
    mq_unlink("/tempqueue");
}
Run Code Online (Sandbox Code Playgroud)

小智 5

检查返回值!

它几乎肯定是"-1"......你可以调用"perror()"或检查"errno"以查找底层错误:

  • 谢谢!我发现:“perror()”返回“消息太长”,然后将我带到缓冲区大小的问题:“buf[400]”应该是“buf[8192]”,因为队列的“msgsize”是8192。 (2认同)