Question

I'm trying to send message "test" with mqueue, but message receiving fails with EMSGSIZE. Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <sys/types.h>
#include <sys/wait.h>
#include <mqueue.h>
#include <errno.h>

using namespace std;

int main() {
mqd_t mqdes;
mqdes = mq_open("/mq.12345", O_RDWR | O_CREAT, (S_IRWXU | S_IRWXG | S_IRWXO), NULL);

if (mqdes == -1) {
    switch (errno) {
    case EACCES:
        cout << "EACCESS" << endl;
        break;
    case EEXIST:
        cout << "EEXIST" << endl;
        break;
    case EINTR:
        cout << "EINTR" << endl;
        break;
    case EINVAL:
        cout << "EINVAL" << endl;
        break;
    case EMFILE:
        cout << "EMFILE" << endl;
        break;
    case ENAMETOOLONG:
        cout << "TOOLONG" << endl;
        break;
    case ENFILE:
        cout << "ENFILE" << endl;
        break;
    case ENOENT:
        cout << "ENOENT" << endl;
        break;
    case ENOSPC:
        cout << "ENOSPC" << endl;
        break;
    }
} else {
    cout << "Success" << endl;
}

pid_t pid;
pid = fork();
if (pid > 0) {
    //  Child process
    string serialized = "test";
    if (mq_send(mqdes, serialized.c_str(), strlen(serialized.c_str()), 0) != 0){
        switch (errno) {
        case EAGAIN:
            cout << "EAGAIN" << endl;
            break;
        case EBADF:
            cout << "EBADF" << endl;
            break;
        case EINTR:
            cout << "EINTR" << endl;
            break;
        case EINVAL:
            cout << "EINVAL" << endl;
            break;
        case EMSGSIZE:
            cout << "EMSGSIZAE" << endl;
            break;
        case ENOSYS:
            cout << "ENOSYS" << endl;
            break;
        }
    } else {
        cout << "Sent" << endl;
    }

    exit(1);
} else {
    //  Parent process
}

int status;
while (wait(&status) > 0) {
}

ssize_t size;
char* buf;
cout << mq_receive(mqdes, buf, 8, NULL) << endl;

switch (errno) {
case EAGAIN:
    cout << "EAGAIN" << endl;
    break;
case EBADF:
    cout << "EBADF" << endl;
    break;
case EMSGSIZE:
    cout << "EMSGSIZAE" << endl;
    break;
case EINTR:
    cout << "EINTR" << endl;
    break;
case ENOSYS:
    cout << "ENOSYS" << endl;
    break;
}

return 0;
}

On receive it prints -1 and EMSGSIZE errno. What's wrong?

EDIT: I'm using Fedora 15

Was it helpful?

Solution

From The mq_receive page

[EMSGSIZE] The specified message buffer size, msg_len, is less than the message size attribute of the message queue.

Most likely the message buffer is too small to contain the complete message.

char* buf;
cout << mq_receive(mqdes, buf, 8, NULL) << endl;

should be something like

char buf[1024];
cout << mq_receive(mqdes, buf, 1024, NULL) << endl;

Also is good practice to put default: in your switch cases.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top