Skip to content
Advertisement

Why does msgrcv returns ENOMSG

System V message queue is created successfully, command ipcs -q outputs:

 ------ Message Queues --------<
 key        msqid      owner      perms      used-bytes   messages    
 0x080b2fb4 0          hel         600        0            0          

But the program to receive the message returns:

exit: msgrcv error, No message of desired type

This is my code:

/* create key for message queue */
key = ftok("/home/hel/messageQueueSystemV", 8);
if (key == -1) {
    printf("exit: ftok error, %sn", strerror(errno)); // error
    exit(0);
}

/* open existed message queue */
mqFd = msgget(key, 0600);
if (mqFd < 0) {
    printf("exit: msgget error, %sn", strerror(errno)); // error
    exit(0);
}

/* receive a message */
if (msgrcv(mqFd, &buf, 1 + sizeof(short), 0, IPC_NOWAIT) < 0) { // type is 0
    printf("exit: msgrcv error, %sn", strerror(errno)); // error
    exit(0);
}

Advertisement

Answer

Transferring comments to an answer.

I suggested:

Maybe there isn’t a message waiting? Where did you write the code to send the message to the queue?

The response was:

Firstly, I run a program to create message queue and send a message. Then use command ipcs -q to check whether it succeeds. Then I start another process to receive that message.

And quibbling began:

The output of ipcs says there are no messages queued. You told msgrcv() not to wait, so it didn’t, so it returned “there are no messages queued”. What am I not understanding?

And, it appears, this was the correct diagnosis.

Advertisement